Run WCF ServiceHost with multiple contracts

前端 未结 8 2230
长发绾君心
长发绾君心 2020-11-27 14:09

Running a ServiceHost with a single contract is working fine like this:

servicehost = new ServiceHost(typeof(MyService1));
servicehost.AddServiceEndpoint(typ         


        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 14:18

    Did I miss something, or is the simplest solution not mentioned here? The simplest solution is this: Don't use multiple interfaces for the Web Service.

    But that doesn't mean you can still have your interfaces separated. This is why we have Interface inheritance.

    [ServiceContract]
    public interface IMetaSomeObjectService : ISomeObjectService1, ISomeObjectService2
    {
    }
    

    The Meta interface inherits from all the other interfaces.

    [ServiceContract]
    public interface ISomeOjectService1
    {
        [OperationContract]
        List GetSomeObjects();
    }
    
    [ServiceContract]
    public interface ISomeOjectService2
    {
        [OperationContract]
        void DoSomethingElse();
    }
    

    Then the service just has the Meta interface.

    public class SomeObjectService : IMetaSomeObjectService
    {
       public List GetSomeObjects()
       {
           // code here
       }
    
       public void DoSomethingElse()
       {
           // code here
       }
    }
    

提交回复
热议问题