Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF how to bind multiple service contracts?

First off I'll apologise as this is probably a duplicate but everything I read seems to be either incomplete or confusing as I'm very new to WCF.

I basically am looking to deploy a WCF service in IIS with 2 endpoints accessible and have been going around in circles all day :(

I have a service library WCF dll with the following structure

App.config
TestSvc1.cs
ITestSvc1.cs
TestSvc2.cs
ITestSvc2.cs

This works fine in the VS WCF test client and now im deploying to IIS so I created a WCF Service Application and referenced the dll. This project has the following structure

Service1.svc
Web.config

Service1.svc contains this

<%@ ServiceHost Language="C#" Debug="true" 
    Service="WCFServices.TestServices.ITestSvc1" CodeBehind="Service1.svc.cs" %>

and my web.config has the following

<services>
   <service name="WCFServices.TestServices.TestSvc2">
      <endpoint 
          address="" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc2">
          <identity>
            <dns value="localhost" />
          </identity>
      </endpoint>
      <endpoint 
           address="mex" 
           binding="mexHttpBinding"  
           contract="IMetadataExchange" />
      <host>
         <baseAddresses>
            <add baseAddress="http://localhost:8080/wcf2/TestServices/TestSvc2/" />
         </baseAddresses>
      </host>
   </service>
   <service name="WCFServices.TestServices.TestSvc1">
      <endpoint 
          address="" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc1"  
          listenUri="http://localhost:8080/wcf2/service1.svc">
          <identity>
            <dns value="" />
          </identity>
      </endpoint>
      <endpoint 
          address="" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc2" 
          listenUri="http://localhost:8080/wcf2/service1.svc">
          <identity>
            <dns value="" />
          </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding"  contract="IMetadataExchange" />
      <host>
         <baseAddresses>
            <add baseAddress="http://localhost:8080/wcf2/TestServices/TestSvc1/" />
         </baseAddresses>
      </host>
   </service>     
</services>

Any help would be greatly appreciated. As you can see I've tried to add an additional endpoint in the web.config but this doesn't work, I get an error saying that TestSvc2 calls can't be found in TestSvc1 which I guess relates to the entry in Service1.svc

I also read about creating a class that inherits these interfaces but I am not sure exactly how to implement it based on what I have above. Do I need to modify the Service1.svc?

public interface MultipleSvc : ITestSvc1, ITestSvc2
{
   // What exactly do I put here? I have no idea, do I leave it blank? This didn't work for me
}

Any help would be greatly appreciated guys thanks :)

like image 314
JazziJeff Avatar asked Jan 15 '13 11:01

JazziJeff


1 Answers

Golden rule: one .svc = one service (or more specifically: one service implementation class)

So if you do have two separate, distinct services (in classes WCFServices.TestServices.TestSvc1 and WCFServices.TestServices.TestSvc2), then you need two .svc files (one each, for each service)

What you could do is have one service implementation class that implements both service contracts:

public class MultipleServices : ITestSvc1, ITestSvc2
{
   // implement the service logic here, for both contracts
}

In that case, one svc file will be enough (the .svc file is per implementation class and can host multiple service contracts). But then you'd need to change your config, too - since you really only have one service class (therefore: one <service> tag) and multiple contracts hosted inside it:

<services>
   <service name="WCFServices.TestServices.MultipleServices">
      <endpoint 
          address="Service1" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc1">
          <identity>
            <dns value="" />
          </identity>
      </endpoint>
      <endpoint 
          address="Service2" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc2">
          <identity>
            <dns value="localhost" />
          </identity>
      </endpoint>
      <endpoint 
           address="mex" 
           binding="mexHttpBinding"  
           contract="IMetadataExchange" />
   </service>     
</services>

Also: since you seem to be hosting your WCF service in IIS, there's no point in defining any <baseAddress> values - the "base" address of your service is the location (URI) where the *.svc file lives.

like image 82
marc_s Avatar answered Nov 10 '22 22:11

marc_s