WCF Service Reference generates its own contract interface, won't reuse mine

后端 未结 4 1556
离开以前
离开以前 2020-12-02 15:31

My first question so hope it is suitable:

Shared interface assembly - I have a \'shared\' assembly which has an interface, let\'s call it IDocRepository

4条回答
  •  Happy的楠姐
    2020-12-02 16:16

    "Reuse types in referenced assemblies" only allows you to reuse Data Contracts, not Service Contracts. If you want to share Service Contracts, you don't need to use "Add Service Reference" at all. You can just use ChannelFactory directly.

    // Supply the binding and address in code
    Binding binding = new BasicHttpBinding();
    EndpointAddress address = new EndpointAddress("http://tempuri.org/address");
    IServiceContract channel = ChannelFactory.CreateChannel(binding, address);
    
    // Or read them from the config file
    ChannelFactory channelFactory = new ChannelFactory();
    IServiceContract channel = channelFactory.CreateChannel();
    

    The channel object will also implement ICommunicationObject, so you can cast it if you need to call methods like Open() or Close().

提交回复
热议问题