tutorial started start servicecontract implement getting c# wcf

c# - started - Alojar mĂșltiples contratos en un servicio WCF



wcf service tutorial (1)

Esta pregunta ya tiene una respuesta aquí:

¿Es posible alojar múltiples contratos de servicio en un servicio WCF? ¿Si es así, cómo? He estado buscando en Google y algunos mensajes dicen que puedes hacerlo (pero no cómo) y otros han dicho que simplemente no es posible.

Cuando ejecuto el servidor, me sale este error:

El nombre del contrato ''ConsoleAppWcfCommon.IBarService'' no se pudo encontrar en la lista de contratos implementados por el servicio ''ConsoleAppWcfServer.FooService''.

Este es mi código de servidor:

static void Main(string[] args) { string serviceAddress = "net.tcp://localhost:8088/FooBarService"; // I''m stuck here as I have to pick *one* service ServiceHost selfServiceHost = new ServiceHost(typeof(FooService)); // I can add both endpoints here, but this is what gives me the error. selfServiceHost.AddServiceEndpoint(typeof(IFooService), new NetTcpBinding(), serviceAddress); selfServiceHost.AddServiceEndpoint(typeof(IBarService), new NetTcpBinding(), serviceAddress); selfServiceHost.Open(); Console.ReadLine(); selfServiceHost.Close(); }

Y este es el código del cliente:

static void Main(string[] args) { NetTcpBinding netTcpBinding = new NetTcpBinding(); EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8088/FooBarService"); // Call IFooService var channelFactoryFoo = new ChannelFactory<IFooService>(netTcpBinding, endpointAddress); IFooService channelFoo = channelFactoryFoo.CreateChannel(); Debug.WriteLine(channelFoo.FooMethod1()); // Call IBarService var channelFactoryBar = new ChannelFactory<IBarService>(netTcpBinding, endpointAddress); IBarService channelBar = channelFactoryBar.CreateChannel(); Debug.WriteLine(channelBar.BarMethod1()); }

Mi objetivo es permitir que el cliente realice una llamada a Foo (o barra) y solo vea los métodos disponibles para cada uno. En mi aplicación real, tengo aproximadamente 10 entidades de dominio con aproximadamente cuatro operaciones en cada una. Estoy tratando de no tener una interfaz con 40 métodos en ella. Y no quiero tener que alojar 10 servicios WCF diferentes para hacer esto.


Como señaló marc_s, la respuesta fue tener una clase de implementación de servicio que implemente ambas interfaces. A continuación se muestra el código de trabajo completo.

Servidor:

static void Main(string[] args) { string serviceAddress = "net.tcp://localhost:8088/FooBarService"; ServiceHost selfServiceHost = new ServiceHost(typeof(FooService)); // The endpoints need to share this binding. var binding = new NetTcpBinding(); selfServiceHost.AddServiceEndpoint(typeof(IFooService), binding, serviceAddress); selfServiceHost.AddServiceEndpoint(typeof(IBarService), binding, serviceAddress); selfServiceHost.Open(); Console.WriteLine("The service is ready."); Console.WriteLine("Press any key to terminate service."); Console.WriteLine(); Console.ReadKey(); selfServiceHost.Close(); }

Cliente:

static void Main(string[] args) { NetTcpBinding netTcpBinding = new NetTcpBinding(); EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8088/FooBarService"); // Call IFooService var channelFactoryFoo = new ChannelFactory<IFooService>(netTcpBinding, endpointAddress); IFooService channelFoo = channelFactoryFoo.CreateChannel(); Console.WriteLine(channelFoo.FooMethod1()); // Call IBarService var channelFactoryBar = new ChannelFactory<IBarService>(netTcpBinding, endpointAddress); IBarService channelBar = channelFactoryBar.CreateChannel(); Console.WriteLine(channelBar.BarMethod1()); Console.ReadKey(); }

Contrato de Foo:

[ServiceContract] public interface IFooService { [OperationContract] string FooMethod1(); [OperationContract] string FooMethod2(); }

Contrato de bar:

[ServiceContract] public interface IBarService { [OperationContract] string BarMethod1(); [OperationContract] string BarMethod2(); }

Servicio de Foo:

public class FooService : IFooService, IBarService { public string FooMethod1() { return "FooMethod1"; } public string FooMethod2() { return "FooMethod2"; } public string BarMethod1() { return "BarMethod1"; } public string BarMethod2() { return "BarMethod2"; } }