CoreWCF / CoreWCF/CoreWCF

Host multiple contracts in one CoreWCF service

Open
#618 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1.8k
Forks
320
PR merge metrics
No merged PRs in 30d

Description

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](https://user-images.githubusercontent.com/20375854/163882325-42edd21d-40e0-4d1c-8a43-ed24b7d593b3.png)

Code:
[CoreWCF_TestMultipleContracts.zip](https://github.com/CoreWCF/CoreWCF/files/8508344/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 :)

``` C#
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();
builder.AddServiceEndpoint(netTcpBinding, "/ServiceCaja");

builder.AddService();
builder.AddServiceEndpoint(netTcpBinding, "/ServiceCategoria");
});
}
```
In Program:

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

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

return builder;
}
```
In the Client solution, only the first call is recognized.

``` C#
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(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(netTcpBinding,
new System.ServiceModel.EndpointAddress(new Uri($"{expectedBaseAddress}/ServiceCategoria")));
var channel1 = factory1.CreateChannel();
var result1 = channel1.ListarCategorias();

Console.ReadLine();
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.