Two WCF services, hosted in one console applicatio

2019-09-05 04:09发布

问题:

I have one console app as a hosting for WCF service. Now, I'm going to add another one WCf service for administer purposes. So, here is my code:

[ServiceContract]
public interface IServiceAdmin
{
    [OperationContract]
    int GetCount();
}

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public class ServiceAdmin : IServiceAdmin
{        
    public int GetCount()
    {
        // It's just a stub to go on
        return 1;
    }
}

Here is excerpt of App.config applied to services:

<serviceBehaviors>
     <behavior name="MyService.ServBehavior">
       <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
       <serviceMetadata httpGetEnabled="false" />
       <serviceDebug includeExceptionDetailInFaults="true" />
     </behavior>
   </serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyService.ServBehavior" name="MyService.MyServ">
  <endpoint address="MyServ" behaviorConfiguration="" binding="netTcpBinding" contract="MyService.IMyServ"  isSystemEndpoint="false" />
  <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="MyService.ServBehavior" name="MyService.MyServAdmin">
  <endpoint address="MyServ" behaviorConfiguration="" binding="netTcpBinding" contract="MyService.IServiceAdmin"  isSystemEndpoint="false" />
  <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />  
</service>

And here is how I get services started:

static void Main(string[] args)
{
ServiceHost myserviceHost = new ServiceHost(typeof(MyServ), new Uri("net.tcp://192.168.1.40:8730/"));
myserviceHost.Open();

ServiceHost myAdminHost = new ServiceHost(typeof(AServiceAdmin), new Uri("net.tcp://192.168.1.40:31337/");
myAdminHost.Open();

Console.ReadLine();
}

The issue is that regular service working good (metadata exchanging can provide info on service methods) and another service (which I mentioned at the beginning, administer service) is not working at all. That is the reason of it?

Thanks in advance!

回答1:

The issue was in wrong name of service in App.Config. The right line is

<service behaviorConfiguration="MyService.ServBehavior" name="MyService.ServiceAdmin">