Run WCF ServiceHost with multiple contracts

前端 未结 8 2218
长发绾君心
长发绾君心 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:16

    I found another solution to for this issue by using a the RoutingService class. Each contract must still be hosted in it's own ServiceHost, but there can be a RoutingService sitting on top of all of them - and presenting them over an unified "endpoint". I've also written a codeproject article about it. The example code is also available on Bitbucket.

    0 讨论(0)
  • 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<SomeOject> GetSomeObjects();
    }
    
    [ServiceContract]
    public interface ISomeOjectService2
    {
        [OperationContract]
        void DoSomethingElse();
    }
    

    Then the service just has the Meta interface.

    public class SomeObjectService : IMetaSomeObjectService
    {
       public List<SomeOject> GetSomeObjects()
       {
           // code here
       }
    
       public void DoSomethingElse()
       {
           // code here
       }
    }
    
    0 讨论(0)
  • 2020-11-27 14:26

    chili's answer will work if you are ok with the contracts being shared by the service. If you want them to be separated try this:

    host1 = new ServiceHost(typeof(MyService1));
    host2 = new ServiceHost(typeof(MyService2));
    
    host1.Open();
    host2.Open();
    
    public class MyService1 : IMyService1
    {
        #region IMyService1
        #endregion
    }
    
    public class MyService2 : IMyService2
    {
        #region IMyService2
        #endregion
    }
    

    Edit: As Matt posted, this would require multiple endpoints for each service/contract

    0 讨论(0)
  • 2020-11-27 14:27

    No-one documented enpoints. Whe used more than one (as a group, from common url, for example http) must use the same binding instance (not more), i.e.

    Your sample:

    servicehost = new ServiceHost(typeof(MyService1));
    servicehost.AddServiceEndpoint(typeof(IMyService1), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService1");
    servicehost.AddServiceEndpoint(typeof(IMyService2), new NetTcpBinding(), "net.tcp://127.0.0.1:800/MyApp/MyService2");
    servicehost.Open();
    

    should be only one new Binding(), I tested over http.

    servicehost = new ServiceHost(typeof(MyService1));
     BasicHttpBinding binding = new BasicHttpBinding();
    servicehost.AddServiceEndpoint(typeof(IMyService1),binding , "http://127.0.0.1:800/MyApp/MyService1");
    servicehost.AddServiceEndpoint(typeof(IMyService2), binding, "http://127.0.0.1:800/MyApp/MyService2");
    servicehost.Open();
    

    I agree totally with partial class implementing few contracts in few files.

    0 讨论(0)
  • 2020-11-27 14:31

    You need to implement both services (interfaces) in the same class.

    servicehost = new ServiceHost(typeof(WcfEntryPoint));
    servicehost.Open(); 
    
    public class WcfEntryPoint : IMyService1, IMyService2
    {
        #region IMyService1
        #endregion
    
        #region IMyService2
        #endregion
    }
    

    FYI: I frequently use partial classes to make my host class code easier to read:

    // WcfEntryPoint.IMyService1.cs
    public partial class WcfEntryPoint : IMyService1
    {
        // IMyService1 methods
    }
    
    // WcfEntryPoint.IMyService2.cs
    public partial class WcfEntryPoint : IMyService2
    {
        // IMyService2 methods
    }
    
    0 讨论(0)
  • 2020-11-27 14:33

    What about splitting it up with a base address and multiple services/contracts below it? I am not behind a developmachine right now but something like:

    http://myserver/myservices/serviceA
    http://myserver/myservices/serviceB
    http://myserver/myservices/serviceC

    Each service implementing its own ServiceContract.

    You can change
    public class WcfEntryPoint : IMyService1, IMyService2
    to
    public partial class WcfEntryPoint : IMyService1
    public partial class WcfEntryPoint : IMyService2

    Example

    0 讨论(0)
提交回复
热议问题