From f3203555f3e3c6b5c0662172fb3782f9c478c499 Mon Sep 17 00:00:00 2001 From: Abdelwahab Afifi Date: Sat, 17 Sep 2022 06:41:27 +0200 Subject: [PATCH] Add NetTcp Sample --- .../Net/Tcp/Default/Binding.Net.Tcp.sln | 31 +++ .../Net/Tcp/Default/Client/Client.csproj | 19 ++ .../ConnectedService.json | 17 ++ .../CoreWcf.Samples.NetTcp/Reference.cs | 178 ++++++++++++++++++ .../Binding/Net/Tcp/Default/Client/Program.cs | 41 ++++ .../Tcp/Default/Service/CalculatorService.cs | 29 +++ .../Tcp/Default/Service/ICalculatorService.cs | 19 ++ .../Net/Tcp/Default/Service/Program.cs | 42 +++++ .../Service/Properties/launchSettings.json | 13 ++ .../Net/Tcp/Default/Service/Service.csproj | 21 +++ .../Service/appsettings.Development.json | 8 + .../Net/Tcp/Default/Service/appsettings.json | 9 + 12 files changed, 427 insertions(+) create mode 100644 Basic/Binding/Net/Tcp/Default/Binding.Net.Tcp.sln create mode 100644 Basic/Binding/Net/Tcp/Default/Client/Client.csproj create mode 100644 Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/ConnectedService.json create mode 100644 Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/Reference.cs create mode 100644 Basic/Binding/Net/Tcp/Default/Client/Program.cs create mode 100644 Basic/Binding/Net/Tcp/Default/Service/CalculatorService.cs create mode 100644 Basic/Binding/Net/Tcp/Default/Service/ICalculatorService.cs create mode 100644 Basic/Binding/Net/Tcp/Default/Service/Program.cs create mode 100644 Basic/Binding/Net/Tcp/Default/Service/Properties/launchSettings.json create mode 100644 Basic/Binding/Net/Tcp/Default/Service/Service.csproj create mode 100644 Basic/Binding/Net/Tcp/Default/Service/appsettings.Development.json create mode 100644 Basic/Binding/Net/Tcp/Default/Service/appsettings.json diff --git a/Basic/Binding/Net/Tcp/Default/Binding.Net.Tcp.sln b/Basic/Binding/Net/Tcp/Default/Binding.Net.Tcp.sln new file mode 100644 index 0000000..359b903 --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Binding.Net.Tcp.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32422.2 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Service", "Service\Service.csproj", "{BF126326-3393-407C-B24A-8FCCC388BE27}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{B533CADA-93BB-40E1-8FBA-FE37100062C3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BF126326-3393-407C-B24A-8FCCC388BE27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BF126326-3393-407C-B24A-8FCCC388BE27}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BF126326-3393-407C-B24A-8FCCC388BE27}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BF126326-3393-407C-B24A-8FCCC388BE27}.Release|Any CPU.Build.0 = Release|Any CPU + {B533CADA-93BB-40E1-8FBA-FE37100062C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B533CADA-93BB-40E1-8FBA-FE37100062C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B533CADA-93BB-40E1-8FBA-FE37100062C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B533CADA-93BB-40E1-8FBA-FE37100062C3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AD996EFD-70DC-4431-B411-5A2771DD02D3} + EndGlobalSection +EndGlobal diff --git a/Basic/Binding/Net/Tcp/Default/Client/Client.csproj b/Basic/Binding/Net/Tcp/Default/Client/Client.csproj new file mode 100644 index 0000000..02c2479 --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Client/Client.csproj @@ -0,0 +1,19 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + diff --git a/Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/ConnectedService.json b/Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/ConnectedService.json new file mode 100644 index 0000000..f0740f5 --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/ConnectedService.json @@ -0,0 +1,17 @@ +{ + "ExtendedData": { + "inputs": [ + "http://localhost:5000/CalculatorService" + ], + "collectionTypes": [ + "System.Array", + "System.Collections.Generic.Dictionary`2" + ], + "namespaceMappings": [ + "*, CoreWcf.Samples.NetTcp" + ], + "sync": true, + "targetFramework": "net6.0", + "typeReuseMode": "All" + } +} diff --git a/Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/Reference.cs b/Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/Reference.cs new file mode 100644 index 0000000..a1cd04c --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Client/Connected Services/CoreWcf.Samples.NetTcp/Reference.cs @@ -0,0 +1,178 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace CoreWcf.Samples.NetTcp +{ + + + [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")] + [System.ServiceModel.ServiceContractAttribute(ConfigurationName="CoreWcf.Samples.NetTcp.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 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 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 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 DivideAsync(double n1, double n2); + } + + [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")] + public interface ICalculatorServiceChannel : CoreWcf.Samples.NetTcp.ICalculatorService, System.ServiceModel.IClientChannel + { + } + + [System.Diagnostics.DebuggerStepThroughAttribute()] + [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")] + public partial class CalculatorServiceClient : System.ServiceModel.ClientBase, CoreWcf.Samples.NetTcp.ICalculatorService + { + + /// + /// Implement this partial method to configure the service endpoint. + /// + /// The endpoint to configure + /// The client credentials + static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials); + + public CalculatorServiceClient() : + base(CalculatorServiceClient.GetDefaultBinding(), CalculatorServiceClient.GetDefaultEndpointAddress()) + { + this.Endpoint.Name = EndpointConfiguration.NetTcpBinding_ICalculatorService.ToString(); + ConfigureEndpoint(this.Endpoint, this.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 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 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 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 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.ServiceModel.ICommunicationObject)(this)).EndOpen)); + } + + private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration) + { + 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.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)); + } + + private static System.ServiceModel.Channels.Binding GetDefaultBinding() + { + return CalculatorServiceClient.GetBindingForEndpoint(EndpointConfiguration.NetTcpBinding_ICalculatorService); + } + + private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress() + { + return CalculatorServiceClient.GetEndpointAddress(EndpointConfiguration.NetTcpBinding_ICalculatorService); + } + + public enum EndpointConfiguration + { + + NetTcpBinding_ICalculatorService, + } + } +} diff --git a/Basic/Binding/Net/Tcp/Default/Client/Program.cs b/Basic/Binding/Net/Tcp/Default/Client/Program.cs new file mode 100644 index 0000000..bc1308e --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Client/Program.cs @@ -0,0 +1,41 @@ +// 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 to terminate client."); +Console.ReadLine(); diff --git a/Basic/Binding/Net/Tcp/Default/Service/CalculatorService.cs b/Basic/Binding/Net/Tcp/Default/Service/CalculatorService.cs new file mode 100644 index 0000000..607d3ff --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Service/CalculatorService.cs @@ -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.NetTcp +{ + // 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; + } + } +} diff --git a/Basic/Binding/Net/Tcp/Default/Service/ICalculatorService.cs b/Basic/Binding/Net/Tcp/Default/Service/ICalculatorService.cs new file mode 100644 index 0000000..7af9ca6 --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Service/ICalculatorService.cs @@ -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.NetTcp +{ + // 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); + } +} \ No newline at end of file diff --git a/Basic/Binding/Net/Tcp/Default/Service/Program.cs b/Basic/Binding/Net/Tcp/Default/Service/Program.cs new file mode 100644 index 0000000..7230026 --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Service/Program.cs @@ -0,0 +1,42 @@ +// 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 builder = WebApplication.CreateBuilder(); + +builder.WebHost +.UseKestrel(options => +{ + options.ListenAnyIP(HttpPort); +}) +.UseNetTcp(NetTcpPort); + +//Enable CoreWCF Services, with metadata (WSDL) support +builder.Services.AddServiceModelServices() + .AddServiceModelMetadata() + .AddSingleton() + .AddSingleton(); + +var app = builder.Build(); + +app.UseServiceModel(builder => +{ + // Add the Calculator Service + builder.AddService(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 NetTcpBinding endpoint + .AddServiceEndpoint(new NetTcpBinding(), "netTcp"); + + // Configure WSDL to be available + var serviceMetadataBehavior = app.Services.GetRequiredService(); + serviceMetadataBehavior.HttpGetEnabled = true; +}); + +app.Run(); diff --git a/Basic/Binding/Net/Tcp/Default/Service/Properties/launchSettings.json b/Basic/Binding/Net/Tcp/Default/Service/Properties/launchSettings.json new file mode 100644 index 0000000..494de7c --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Service/Properties/launchSettings.json @@ -0,0 +1,13 @@ +{ + "profiles": { + "Service": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "http://localhost:5000/CalculatorService", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Basic/Binding/Net/Tcp/Default/Service/Service.csproj b/Basic/Binding/Net/Tcp/Default/Service/Service.csproj new file mode 100644 index 0000000..72e3f87 --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Service/Service.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + true + InProcess + + + + + + + + + + + + + + diff --git a/Basic/Binding/Net/Tcp/Default/Service/appsettings.Development.json b/Basic/Binding/Net/Tcp/Default/Service/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Service/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Basic/Binding/Net/Tcp/Default/Service/appsettings.json b/Basic/Binding/Net/Tcp/Default/Service/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/Basic/Binding/Net/Tcp/Default/Service/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}