Skip to content

Commit f320355

Browse files
committed
Add NetTcp Sample
1 parent 2078717 commit f320355

File tree

12 files changed

+427
-0
lines changed

12 files changed

+427
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32422.2
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Service", "Service\Service.csproj", "{BF126326-3393-407C-B24A-8FCCC388BE27}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{B533CADA-93BB-40E1-8FBA-FE37100062C3}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{BF126326-3393-407C-B24A-8FCCC388BE27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{BF126326-3393-407C-B24A-8FCCC388BE27}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{BF126326-3393-407C-B24A-8FCCC388BE27}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{BF126326-3393-407C-B24A-8FCCC388BE27}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{B533CADA-93BB-40E1-8FBA-FE37100062C3}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {AD996EFD-70DC-4431-B411-5A2771DD02D3}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.NetTcp" />
12+
<Using Include="System.ServiceModel" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.*" />
17+
</ItemGroup>
18+
19+
</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"
5+
],
6+
"collectionTypes": [
7+
"System.Array",
8+
"System.Collections.Generic.Dictionary`2"
9+
],
10+
"namespaceMappings": [
11+
"*, CoreWcf.Samples.NetTcp"
12+
],
13+
"sync": true,
14+
"targetFramework": "net6.0",
15+
"typeReuseMode": "All"
16+
}
17+
}
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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.NetTcp
11+
{
12+
13+
14+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
15+
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="CoreWcf.Samples.NetTcp.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.0.3")]
45+
public interface ICalculatorServiceChannel : CoreWcf.Samples.NetTcp.ICalculatorService, System.ServiceModel.IClientChannel
46+
{
47+
}
48+
49+
[System.Diagnostics.DebuggerStepThroughAttribute()]
50+
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
51+
public partial class CalculatorServiceClient : System.ServiceModel.ClientBase<CoreWcf.Samples.NetTcp.ICalculatorService>, CoreWcf.Samples.NetTcp.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() :
62+
base(CalculatorServiceClient.GetDefaultBinding(), CalculatorServiceClient.GetDefaultEndpointAddress())
63+
{
64+
this.Endpoint.Name = EndpointConfiguration.NetTcpBinding_ICalculatorService.ToString();
65+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
66+
}
67+
68+
public CalculatorServiceClient(EndpointConfiguration endpointConfiguration) :
69+
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), CalculatorServiceClient.GetEndpointAddress(endpointConfiguration))
70+
{
71+
this.Endpoint.Name = endpointConfiguration.ToString();
72+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
73+
}
74+
75+
public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
76+
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
77+
{
78+
this.Endpoint.Name = endpointConfiguration.ToString();
79+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
80+
}
81+
82+
public CalculatorServiceClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
83+
base(CalculatorServiceClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
84+
{
85+
this.Endpoint.Name = endpointConfiguration.ToString();
86+
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
87+
}
88+
89+
public CalculatorServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
90+
base(binding, remoteAddress)
91+
{
92+
}
93+
94+
public double Add(double n1, double n2)
95+
{
96+
return base.Channel.Add(n1, n2);
97+
}
98+
99+
public System.Threading.Tasks.Task<double> AddAsync(double n1, double n2)
100+
{
101+
return base.Channel.AddAsync(n1, n2);
102+
}
103+
104+
public double Subtract(double n1, double n2)
105+
{
106+
return base.Channel.Subtract(n1, n2);
107+
}
108+
109+
public System.Threading.Tasks.Task<double> SubtractAsync(double n1, double n2)
110+
{
111+
return base.Channel.SubtractAsync(n1, n2);
112+
}
113+
114+
public double Multiply(double n1, double n2)
115+
{
116+
return base.Channel.Multiply(n1, n2);
117+
}
118+
119+
public System.Threading.Tasks.Task<double> MultiplyAsync(double n1, double n2)
120+
{
121+
return base.Channel.MultiplyAsync(n1, n2);
122+
}
123+
124+
public double Divide(double n1, double n2)
125+
{
126+
return base.Channel.Divide(n1, n2);
127+
}
128+
129+
public System.Threading.Tasks.Task<double> DivideAsync(double n1, double n2)
130+
{
131+
return base.Channel.DivideAsync(n1, n2);
132+
}
133+
134+
public virtual System.Threading.Tasks.Task OpenAsync()
135+
{
136+
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
137+
}
138+
139+
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
140+
{
141+
if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_ICalculatorService))
142+
{
143+
System.ServiceModel.NetTcpBinding result = new System.ServiceModel.NetTcpBinding();
144+
result.MaxBufferSize = int.MaxValue;
145+
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
146+
result.MaxReceivedMessageSize = int.MaxValue;
147+
result.Security.Mode = System.ServiceModel.SecurityMode.None;
148+
return result;
149+
}
150+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
151+
}
152+
153+
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
154+
{
155+
if ((endpointConfiguration == EndpointConfiguration.NetTcpBinding_ICalculatorService))
156+
{
157+
return new System.ServiceModel.EndpointAddress("net.tcp://localhost:8089/CalculatorService/netTcp");
158+
}
159+
throw new System.InvalidOperationException(string.Format("Could not find endpoint with name \'{0}\'.", endpointConfiguration));
160+
}
161+
162+
private static System.ServiceModel.Channels.Binding GetDefaultBinding()
163+
{
164+
return CalculatorServiceClient.GetBindingForEndpoint(EndpointConfiguration.NetTcpBinding_ICalculatorService);
165+
}
166+
167+
private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
168+
{
169+
return CalculatorServiceClient.GetEndpointAddress(EndpointConfiguration.NetTcpBinding_ICalculatorService);
170+
}
171+
172+
public enum EndpointConfiguration
173+
{
174+
175+
NetTcpBinding_ICalculatorService,
176+
}
177+
}
178+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
5+
// there is an issue in client code generation where the Security.Mode = None
6+
// override client binding with Transport Security Mode to match service definition
7+
NetTcpBinding binding = new NetTcpBinding();
8+
var endpointAddress = new EndpointAddress("net.tcp://localhost:8089/CalculatorService/netTcp");
9+
10+
CalculatorServiceClient client = new CalculatorServiceClient(binding, endpointAddress);
11+
12+
// Call the Add service operation.
13+
double value1 = 100.00D;
14+
double value2 = 15.99D;
15+
double result = client.Add(value1, value2);
16+
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
17+
18+
// Call the Subtract service operation.
19+
value1 = 145.00D;
20+
value2 = 76.54D;
21+
result = client.Subtract(value1, value2);
22+
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
23+
24+
// Call the Multiply service operation.
25+
value1 = 9.00D;
26+
value2 = 81.25D;
27+
result = client.Multiply(value1, value2);
28+
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
29+
30+
// Call the Divide service operation.
31+
value1 = 22.00D;
32+
value2 = 7.00D;
33+
result = client.Divide(value1, value2);
34+
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
35+
36+
//Closing the client gracefully closes the connection and cleans up resources
37+
client.CloseAsync();
38+
39+
Console.WriteLine();
40+
Console.WriteLine("Press <ENTER> to terminate client.");
41+
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.NetTcp
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.NetTcp
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
7+
var builder = WebApplication.CreateBuilder();
8+
9+
builder.WebHost
10+
.UseKestrel(options =>
11+
{
12+
options.ListenAnyIP(HttpPort);
13+
})
14+
.UseNetTcp(NetTcpPort);
15+
16+
//Enable CoreWCF Services, with metadata (WSDL) support
17+
builder.Services.AddServiceModelServices()
18+
.AddServiceModelMetadata()
19+
.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>()
20+
.AddSingleton<CalculatorService>();
21+
22+
var app = builder.Build();
23+
24+
app.UseServiceModel(builder =>
25+
{
26+
// Add the Calculator Service
27+
builder.AddService<CalculatorService>(serviceOptions =>
28+
{
29+
serviceOptions.BaseAddresses.Clear();
30+
// Set the default host name:port in generated WSDL and the base path for the address
31+
serviceOptions.BaseAddresses.Add(new Uri("http://localhost/CalculatorService"));
32+
serviceOptions.BaseAddresses.Add(new Uri($"net.tcp://localhost:{NetTcpPort}/CalculatorService"));
33+
})
34+
// Add NetTcpBinding endpoint
35+
.AddServiceEndpoint<CalculatorService, ICalculatorService>(new NetTcpBinding(), "netTcp");
36+
37+
// Configure WSDL to be available
38+
var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
39+
serviceMetadataBehavior.HttpGetEnabled = true;
40+
});
41+
42+
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",
8+
"environmentVariables": {
9+
"ASPNETCORE_ENVIRONMENT": "Development"
10+
}
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)