CoreWCF icon indicating copy to clipboard operation
CoreWCF copied to clipboard

Host multiple contracts in one CoreWCF service

Open DarioSalcedo opened this issue 3 years ago • 0 comments

Is it possible to host multiple service contracts in one CoreWCF service? If so, how? I've been googling and some posts say you can do it (but not how)

GITHU

Code: CoreWCF_TestMultipleContracts.zip

I'm trying not to have one interface with N methods in it. And I don't want to have to host X different WCF services to do this. Please, I humbly request your support :)

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddServiceModelServices();
        }
        public void Configure(IApplicationBuilder app)
        {
            NetTcpBinding netTcpBinding = new NetTcpBinding
            {
                TransferMode = TransferMode.Streamed
            };
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                netTcpBinding.Security.Mode = SecurityMode.None;
            else
            {
                netTcpBinding.Security.Mode = SecurityMode.Transport;
                netTcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
            }

            app.UseServiceModel(builder =>
            {
                builder.AddService<ServiceCaja>();
                builder.AddServiceEndpoint<ServiceCaja, Contracts.IServiceCaja>(netTcpBinding, "/ServiceCaja");

                builder.AddService<ServiceCategoria>();
                builder.AddServiceEndpoint<ServiceCategoria, Contracts.IServiceCategoria>(netTcpBinding, "/ServiceCategoria");
            });
        }

In Program:

 static void Main(string[] args)
        {
            int port = 11808;
            IWebHost host = CreateWebHostBuilder<Startup>(IPAddress.Loopback, port).Build();
            host.Run();
        }
     public static IWebHostBuilder CreateWebHostBuilder<TStartup>(IPAddress? ipAddress = null, int port = 0) where TStartup : class
        {
            if (ipAddress == null)
                ipAddress = IPAddress.Loopback;

            IWebHostBuilder builder = WebHost.CreateDefaultBuilder(Array.Empty<string>())
                .UseNetTcp(ipAddress, port)
                .UseStartup<TStartup>();

            return builder;
        }        

In the Client solution, only the first call is recognized.

    static void Main(string[] args)
        {
            NetTcpBinding netTcpBinding = new NetTcpBinding
            {
                TransferMode = TransferMode.Streamed
            };

            int port = 11808;
            string expectedBaseAddress = $"net.tcp://{IPAddress.Loopback}:{port}";

            var factory = new System.ServiceModel.ChannelFactory<Server.Contracts.IServiceCaja>(netTcpBinding,
                new System.ServiceModel.EndpointAddress(new Uri($"{expectedBaseAddress}/ServiceCaja")));
            var channel = factory.CreateChannel();
            var result = channel.ListarCajas();

ERROR HERE CALL

            var factory1 = new System.ServiceModel.ChannelFactory<Server.Contracts.IServiceCategoria>(netTcpBinding,
                new System.ServiceModel.EndpointAddress(new Uri($"{expectedBaseAddress}/ServiceCategoria")));
            var channel1 = factory1.CreateChannel();
            var result1 = channel1.ListarCategorias();

            Console.ReadLine();
        }

DarioSalcedo avatar Apr 18 '22 21:04 DarioSalcedo