Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Basic/Services/Hosting/WindowsService/Client/Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Using Include="CoreWcf.Samples.WindowsService" />
<Using Include="System.ServiceModel" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.ServiceModel.Duplex" Version="4.10.*" />
<PackageReference Include="System.ServiceModel.Federation" Version="4.10.*" />
<PackageReference Include="System.ServiceModel.Http" Version="4.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.*" />
<PackageReference Include="System.ServiceModel.Security" Version="4.10.*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"ExtendedData": {
"inputs": [
"http://localhost:5000/CalculatorService"
],
"collectionTypes": [
"System.Array",
"System.Collections.Generic.Dictionary`2"
],
"namespaceMappings": [
"*, CoreWcf.Samples.WindowsService"
],
"sync": true,
"targetFramework": "net6.0",
"typeReuseMode": "All"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CoreWcf.Samples.WindowsService
{


[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="CoreWcf.Samples.WindowsService.ICalculatorService")]
public interface ICalculatorService
{

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Add", ReplyAction="http://tempuri.org/ICalculatorService/AddResponse")]
double Add(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Add", ReplyAction="http://tempuri.org/ICalculatorService/AddResponse")]
System.Threading.Tasks.Task<double> AddAsync(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Subtract", ReplyAction="http://tempuri.org/ICalculatorService/SubtractResponse")]
double Subtract(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Subtract", ReplyAction="http://tempuri.org/ICalculatorService/SubtractResponse")]
System.Threading.Tasks.Task<double> SubtractAsync(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Multiply", ReplyAction="http://tempuri.org/ICalculatorService/MultiplyResponse")]
double Multiply(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Multiply", ReplyAction="http://tempuri.org/ICalculatorService/MultiplyResponse")]
System.Threading.Tasks.Task<double> MultiplyAsync(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Divide", ReplyAction="http://tempuri.org/ICalculatorService/DivideResponse")]
double Divide(double n1, double n2);

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Divide", ReplyAction="http://tempuri.org/ICalculatorService/DivideResponse")]
System.Threading.Tasks.Task<double> DivideAsync(double n1, double n2);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
public interface ICalculatorServiceChannel : CoreWcf.Samples.WindowsService.ICalculatorService, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
public partial class CalculatorServiceClient : System.ServiceModel.ClientBase<CoreWcf.Samples.WindowsService.ICalculatorService>, CoreWcf.Samples.WindowsService.ICalculatorService
{

/// <summary>
/// Implement this partial method to configure the service endpoint.
/// </summary>
/// <param name="serviceEndpoint">The endpoint to configure</param>
/// <param name="clientCredentials">The client credentials</param>
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);

public CalculatorServiceClient(EndpointConfiguration endpointConfiguration) :
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), CalculatorServiceClient.GetEndpointAddress(endpointConfiguration))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}

public CalculatorServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}

public double Add(double n1, double n2)
{
return base.Channel.Add(n1, n2);
}

public System.Threading.Tasks.Task<double> AddAsync(double n1, double n2)
{
return base.Channel.AddAsync(n1, n2);
}

public double Subtract(double n1, double n2)
{
return base.Channel.Subtract(n1, n2);
}

public System.Threading.Tasks.Task<double> SubtractAsync(double n1, double n2)
{
return base.Channel.SubtractAsync(n1, n2);
}

public double Multiply(double n1, double n2)
{
return base.Channel.Multiply(n1, n2);
}

public System.Threading.Tasks.Task<double> MultiplyAsync(double n1, double n2)
{
return base.Channel.MultiplyAsync(n1, n2);
}

public double Divide(double n1, double n2)
{
return base.Channel.Divide(n1, n2);
}

public System.Threading.Tasks.Task<double> DivideAsync(double n1, double n2)
{
return base.Channel.DivideAsync(n1, n2);
}

public virtual System.Threading.Tasks.Task OpenAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}

private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ICalculatorService))
{
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
return result;
}
if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_ICalculatorService))
{
System.ServiceModel.NetTcpBinding result = new System.ServiceModel.NetTcpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.Security.Mode = System.ServiceModel.SecurityMode.None;
return result;
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}

private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ICalculatorService))
{
return new System.ServiceModel.EndpointAddress("http://localhost:5000/CalculatorService/basicHttp");
}
if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_ICalculatorService))
{
return new System.ServiceModel.EndpointAddress("net.tcp://localhost:8089/CalculatorService/netTcp");
}
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
}

public enum EndpointConfiguration
{

BasicHttpBinding_ICalculatorService,

NetTcpBinding_ICalculatorService,
}
}
}
40 changes: 40 additions & 0 deletions Basic/Services/Hosting/WindowsService/Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// there is an issue in client code generation where the Security.Mode = None
// override client binding with Transport Security Mode to match service definition
NetTcpBinding binding = new NetTcpBinding();
var endpointAddress = new EndpointAddress("net.tcp://localhost:8089/CalculatorService/netTcp");

CalculatorServiceClient client = new CalculatorServiceClient(binding, endpointAddress);

// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);

// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);

// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);

//Closing the client gracefully closes the connection and cleans up resources
client.CloseAsync();

Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
29 changes: 29 additions & 0 deletions Basic/Services/Hosting/WindowsService/Service/CalculatorService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace CoreWcf.Samples.WindowsService
{
// Service class which implements the service contract interface.
public class CalculatorService : ICalculatorService
{
public double Add(double n1, double n2)
{
return n1 + n2;
}

public double Subtract(double n1, double n2)
{
return n1 - n2;
}

public double Multiply(double n1, double n2)
{
return n1 * n2;
}

public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace CoreWcf.Samples.WindowsService
{
// Define a service contract.
[ServiceContract]
public interface ICalculatorService
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
}
56 changes: 56 additions & 0 deletions Basic/Services/Hosting/WindowsService/Service/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

const int HttpPort = 5000;
const int NetTcpPort = 8089;
var options = new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService()
? AppContext.BaseDirectory : default
};

var builder = WebApplication.CreateBuilder(options);

// Enable CoreWCF Services, with metadata (WSDL) support
builder.Services.AddHostedService<WindowsServiceWorker>()
.AddServiceModelServices()
.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>()
.AddServiceModelMetadata();

builder.WebHost.UseKestrel(options =>
{
options.ListenAnyIP(HttpPort);
})
.UseNetTcp(NetTcpPort);

builder.Host.UseWindowsService(options =>
{
// Set service name (Optional)
options.ServiceName = "CoreWCF Windows Service";
});

var app = builder.Build();

// Configure the bindings and endpoints
app.UseServiceModel(builder =>
{
// Add the Calculator Service
builder.AddService<CalculatorService>(serviceOptions =>
{
serviceOptions.BaseAddresses.Clear();
// Set the default host name:port in generated WSDL and the base path for the address
serviceOptions.BaseAddresses.Add(new Uri("http://localhost/CalculatorService"));
serviceOptions.BaseAddresses.Add(new Uri($"net.tcp://localhost:{NetTcpPort}/CalculatorService"));
})
// Add BasicHttpBinding endpoint
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new BasicHttpBinding(), "basicHttp")
// Add NetTcpBinding endpoint
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new NetTcpBinding(), "netTcp");

// Configure WSDL to be available
var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
serviceMetadataBehavior.HttpGetEnabled = true;
});

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"profiles": {
"Service": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "http://localhost:5000/CalculatorService",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading