2013-05-11 34 views
13

Có thể lưu trữ nhiều hợp đồng dịch vụ trong một dịch vụ WCF không? Nếu vậy, làm thế nào? Tôi đã googling và một số bài viết nói rằng bạn có thể làm điều đó (nhưng không phải như thế nào) và những người khác đã nói rằng nó chỉ là không thể.Lưu trữ nhiều hợp đồng trong một dịch vụ WCF

Khi tôi chạy máy chủ, tôi nhận được lỗi này:

The contract name 'ConsoleAppWcfCommon.IBarService' could not be found in the list of contracts implemented by the service 'ConsoleAppWcfServer.FooService'.

Đây là mã máy chủ của tôi:

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(); 
    } 

Và đây là mã khách hàng:

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()); 
    } 

Mục tiêu của tôi là để khách hàng thực hiện cuộc gọi đến Foo (hoặc Bar) và chỉ xem các phương thức có sẵn cho mỗi. Trong ứng dụng thực sự của tôi, tôi có khoảng 10 thực thể miền với khoảng bốn hoạt động trên mỗi ứng dụng. Tôi đang cố gắng không có một giao diện với 40 phương pháp trong đó. Và tôi không muốn phải tổ chức 10 dịch vụ WCF khác nhau để làm điều này.

+3

Cách duy nhất để làm điều này là có ** lớp thực hiện một dịch vụ ** mà thực hiện ** cả ** giao diện trong câu hỏi . Bạn có điều đó không? Vì vậy, bạn sẽ cần phải có 'lớp công khai FooService: IFooService, IBarService {....}' –

+1

http://stackoverflow.com/a/334554/352101 – Bolu

+0

@marc_s Tôi không có điều đó, nhưng tôi có thể như tôi sở hữu tất cả mã. Trong ví dụ trên của tôi, bạn có nói rằng mã máy chủ nên hoạt động như là-nếu FooService thực hiện cả hai giao diện? –

Trả lời

21

Như marc_s đã chỉ ra, câu trả lời là có một lớp triển khai dịch vụ triển khai cả hai giao diện. Dưới đây là mã làm việc đầy đủ.

Server:

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(); 
    } 

Chủ đầu tư:

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(); 
    } 

Foo hợp đồng:

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

    [OperationContract] 
    string FooMethod2(); 
} 

Bar hợp đồng:

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

    [OperationContract] 
    string BarMethod2(); 
} 

Dịch vụ 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"; 
    } 
} 
+1

Tôi thực sự thích rằng bạn đã đăng mã kết quả cuối cùng của mình - điều này giúp ích cho người khác, bao gồm tôi. Tôi thấy các nhận xét như tôi sẽ đăng mã cuối cùng; nhưng mọi người không làm điều đó và tôi đang đập đầu tôi! ++ – Stix

+0

Mã đẹp, nhưng nếu tôi đang ở WCF được lưu trữ trên IIS, tôi sẽ đặt những thứ nào trong Main? – Rhyous

+0

Nó sẽ là trong khu vực của dịch vụ WCF của bạn mà khởi tạo mọi thứ khi dịch vụ bắt đầu. Bạn hiện đang thiết lập các ràng buộc và điểm cuối ở đâu? Đó là nơi mã này sẽ đi. –

Các vấn đề liên quan