Skip to content

Commit 5e46882

Browse files
committed
Add NetTcp to Windows Service
add NetTcp and convert to top level statement Add license Update service config Add WindowsService Sample Updates: 1. Remove CS folder in folder heirarchy 2. Rename .sln file to Binding.Basic.Http.sln 3. Remove .config folder (validate this doesn't break the build) Update Client Connected Service URL Update LaunchSettings and remove .UseIIS (WebHost default) Update Client NuGet Packages Update NuGet pPackages Add BasicHttpBinding Sample - IIS Hosted
1 parent 2078717 commit 5e46882

File tree

13 files changed

+478
-0
lines changed

13 files changed

+478
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Using Include="CoreWcf.Samples.WindowsService" />
12+
<Using Include="System.ServiceModel" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="System.ServiceModel.Duplex" Version="4.10.*" />
17+
<PackageReference Include="System.ServiceModel.Federation" Version="4.10.*" />
18+
<PackageReference Include="System.ServiceModel.Http" Version="4.*" />
19+
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.10.*" />
20+
<PackageReference Include="System.ServiceModel.Security" Version="4.10.*" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"ExtendedData": {
3+
"inputs": [
4+
"http://localhost:5000/CalculatorService/basicService"
5+
],
6+
"collectionTypes": [
7+
"System.Array",
8+
"System.Collections.Generic.Dictionary`2"
9+
],
10+
"namespaceMappings": [
11+
"*, CoreWcf.Samples.WindowsService"
12+
],
13+
"sync": true,
14+
"targetFramework": "net6.0",
15+
"typeReuseMode": "All"
16+
}
17+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by a tool.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
namespace CoreWcf.Samples.WindowsService
11+
{
12+
13+
14+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
15+
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="CoreWcf.Samples.WindowsService.ICalculatorService")]
16+
public interface ICalculatorService
17+
{
18+
19+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Add", ReplyAction="http://tempuri.org/ICalculatorService/AddResponse")]
20+
double Add(double n1, double n2);
21+
22+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Add", ReplyAction="http://tempuri.org/ICalculatorService/AddResponse")]
23+
System.Threading.Tasks.Task<double> AddAsync(double n1, double n2);
24+
25+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Subtract", ReplyAction="http://tempuri.org/ICalculatorService/SubtractResponse")]
26+
double Subtract(double n1, double n2);
27+
28+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Subtract", ReplyAction="http://tempuri.org/ICalculatorService/SubtractResponse")]
29+
System.Threading.Tasks.Task<double> SubtractAsync(double n1, double n2);
30+
31+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Multiply", ReplyAction="http://tempuri.org/ICalculatorService/MultiplyResponse")]
32+
double Multiply(double n1, double n2);
33+
34+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Multiply", ReplyAction="http://tempuri.org/ICalculatorService/MultiplyResponse")]
35+
System.Threading.Tasks.Task<double> MultiplyAsync(double n1, double n2);
36+
37+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Divide", ReplyAction="http://tempuri.org/ICalculatorService/DivideResponse")]
38+
double Divide(double n1, double n2);
39+
40+
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICalculatorService/Divide", ReplyAction="http://tempuri.org/ICalculatorService/DivideResponse")]
41+
System.Threading.Tasks.Task<double> DivideAsync(double n1, double n2);
42+
}
43+
44+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
45+
public interface ICalculatorServiceChannel : CoreWcf.Samples.WindowsService.ICalculatorService, System.ServiceModel.IClientChannel
46+
{
47+
}
48+
49+
[System.Diagnostics.DebuggerStepThroughAttribute()]
50+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.1.0")]
51+
public partial class CalculatorServiceClient : System.ServiceModel.ClientBase<CoreWcf.Samples.WindowsService.ICalculatorService>, CoreWcf.Samples.WindowsService.ICalculatorService
52+
{
53+
54+
/// <summary>
55+
/// Implement this partial method to configure the service endpoint.
56+
/// </summary>
57+
/// <param name="serviceEndpoint">The endpoint to configure</param>
58+
/// <param name="clientCredentials">The client credentials</param>
59+
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
60+
61+
public CalculatorServiceClient(EndpointConfiguration endpointConfiguration) :
62+
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), CalculatorServiceClient.GetEndpointAddress(endpointConfiguration))
63+
{
64+
this.Endpoint.Name = endpointConfiguration.ToString();
65+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
66+
}
67+
68+
public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
69+
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
70+
{
71+
this.Endpoint.Name = endpointConfiguration.ToString();
72+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
73+
}
74+
75+
public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
76+
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
77+
{
78+
this.Endpoint.Name = endpointConfiguration.ToString();
79+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
80+
}
81+
82+
public CalculatorServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
83+
base(binding, remoteAddress)
84+
{
85+
}
86+
87+
public double Add(double n1, double n2)
88+
{
89+
return base.Channel.Add(n1, n2);
90+
}
91+
92+
public System.Threading.Tasks.Task<double> AddAsync(double n1, double n2)
93+
{
94+
return base.Channel.AddAsync(n1, n2);
95+
}
96+
97+
public double Subtract(double n1, double n2)
98+
{
99+
return base.Channel.Subtract(n1, n2);
100+
}
101+
102+
public System.Threading.Tasks.Task<double> SubtractAsync(double n1, double n2)
103+
{
104+
return base.Channel.SubtractAsync(n1, n2);
105+
}
106+
107+
public double Multiply(double n1, double n2)
108+
{
109+
return base.Channel.Multiply(n1, n2);
110+
}
111+
112+
public System.Threading.Tasks.Task<double> MultiplyAsync(double n1, double n2)
113+
{
114+
return base.Channel.MultiplyAsync(n1, n2);
115+
}
116+
117+
public double Divide(double n1, double n2)
118+
{
119+
return base.Channel.Divide(n1, n2);
120+
}
121+
122+
public System.Threading.Tasks.Task<double> DivideAsync(double n1, double n2)
123+
{
124+
return base.Channel.DivideAsync(n1, n2);
125+
}
126+
127+
public virtual System.Threading.Tasks.Task OpenAsync()
128+
{
129+
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
130+
}
131+
132+
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
133+
{
134+
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ICalculatorService))
135+
{
136+
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
137+
result.MaxBufferSize = int.MaxValue;
138+
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
139+
result.MaxReceivedMessageSize = int.MaxValue;
140+
result.AllowCookies = true;
141+
return result;
142+
}
143+
if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_ICalculatorService))
144+
{
145+
System.ServiceModel.NetTcpBinding result = new System.ServiceModel.NetTcpBinding();
146+
result.MaxBufferSize = int.MaxValue;
147+
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
148+
result.MaxReceivedMessageSize = int.MaxValue;
149+
result.Security.Mode = System.ServiceModel.SecurityMode.None;
150+
return result;
151+
}
152+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
153+
}
154+
155+
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
156+
{
157+
if ((endpointConfiguration == EndpointConfiguration.BasicHttpBinding_ICalculatorService))
158+
{
159+
return new System.ServiceModel.EndpointAddress("http://localhost:5000/CalculatorService/basicService/CalculatorService/basicHttp");
160+
}
161+
if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_ICalculatorService))
162+
{
163+
return new System.ServiceModel.EndpointAddress("net.tcp://localhost:8089/CalculatorService/netTcp");
164+
}
165+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
166+
}
167+
168+
public enum EndpointConfiguration
169+
{
170+
171+
BasicHttpBinding_ICalculatorService,
172+
173+
NetTcpBinding_ICalculatorService,
174+
}
175+
}
176+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// there is an issue in client code generation where the Security.Mode = None
5+
// override client binding with Transport Security Mode to match service definition
6+
NetTcpBinding binding = new NetTcpBinding();
7+
var endpointAddress = new EndpointAddress("net.tcp://localhost:8089/CalculatorService/netTcp");
8+
9+
CalculatorServiceClient client = new CalculatorServiceClient(binding, endpointAddress);
10+
11+
// Call the Add service operation.
12+
double value1 = 100.00D;
13+
double value2 = 15.99D;
14+
double result = client.Add(value1, value2);
15+
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
16+
17+
// Call the Subtract service operation.
18+
value1 = 145.00D;
19+
value2 = 76.54D;
20+
result = client.Subtract(value1, value2);
21+
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
22+
23+
// Call the Multiply service operation.
24+
value1 = 9.00D;
25+
value2 = 81.25D;
26+
result = client.Multiply(value1, value2);
27+
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
28+
29+
// Call the Divide service operation.
30+
value1 = 22.00D;
31+
value2 = 7.00D;
32+
result = client.Divide(value1, value2);
33+
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
34+
35+
//Closing the client gracefully closes the connection and cleans up resources
36+
client.CloseAsync();
37+
38+
Console.WriteLine();
39+
Console.WriteLine("Press <ENTER> to terminate client.");
40+
Console.ReadLine();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace CoreWcf.Samples.WindowsService
5+
{
6+
// Service class which implements the service contract interface.
7+
public class CalculatorService : ICalculatorService
8+
{
9+
public double Add(double n1, double n2)
10+
{
11+
return n1 + n2;
12+
}
13+
14+
public double Subtract(double n1, double n2)
15+
{
16+
return n1 - n2;
17+
}
18+
19+
public double Multiply(double n1, double n2)
20+
{
21+
return n1 * n2;
22+
}
23+
24+
public double Divide(double n1, double n2)
25+
{
26+
return n1 / n2;
27+
}
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace CoreWcf.Samples.WindowsService
5+
{
6+
// Define a service contract.
7+
[ServiceContract]
8+
public interface ICalculatorService
9+
{
10+
[OperationContract]
11+
double Add(double n1, double n2);
12+
[OperationContract]
13+
double Subtract(double n1, double n2);
14+
[OperationContract]
15+
double Multiply(double n1, double n2);
16+
[OperationContract]
17+
double Divide(double n1, double n2);
18+
}
19+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
const int HttpPort = 5000;
5+
const int NetTcpPort = 8089;
6+
var options = new WebApplicationOptions
7+
{
8+
Args = args,
9+
ContentRootPath = WindowsServiceHelpers.IsWindowsService()
10+
? AppContext.BaseDirectory : default
11+
};
12+
13+
var builder = WebApplication.CreateBuilder(options);
14+
15+
// Enable CoreWCF Services, with metadata (WSDL) support
16+
builder.Services.AddHostedService<WindowsServiceWorker>()
17+
.AddServiceModelServices()
18+
.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>()
19+
.AddServiceModelMetadata();
20+
21+
builder.WebHost.UseKestrel(options =>
22+
{
23+
options.ListenAnyIP(HttpPort);
24+
})
25+
.UseNetTcp(NetTcpPort);
26+
27+
builder.Host.UseWindowsService(options =>
28+
{
29+
// Set service name (Optional)
30+
options.ServiceName = "CoreWCF Windows Service";
31+
});
32+
33+
var app = builder.Build();
34+
35+
// Configure the bindings and endpoints
36+
app.UseServiceModel(builder =>
37+
{
38+
// Add the Calculator Service
39+
builder.AddService<CalculatorService>(serviceOptions =>
40+
{
41+
// Set the default host name:port in generated WSDL and the base path for the address
42+
serviceOptions.BaseAddresses.Add(new Uri($"http://localhost:{HttpPort}/CalculatorService/basicService"));
43+
})
44+
// Add BasicHttpBinding endpoint
45+
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new BasicHttpBinding(), "/CalculatorService/basicHttp")
46+
// Add NetTcp endpoint
47+
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new NetTcpBinding(), $"net.tcp://localhost:{NetTcpPort}/CalculatorService/netTcp");
48+
49+
// Configure WSDL to be available
50+
var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
51+
serviceMetadataBehavior.HttpGetEnabled = true;
52+
});
53+
54+
app.Run();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"profiles": {
3+
"Service": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"launchBrowser": true,
7+
"launchUrl": "http://localhost:5000/CalculatorService/basicService",
8+
"environmentVariables": {
9+
"ASPNETCORE_ENVIRONMENT": "Development"
10+
}
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)