Configuration for WCF with Multiple Endpoints

I am new to WCF and am trying to get some ideas I have.

Basically, I have a WCF application web project with its web.config:

<system.serviceModel>
    <services>
        <service name="WcfService1.ServiceContract.IDirectorySearchService" behaviorConfiguration="defaultServiceBehavior"> 
            <endpoint name="restxml" address="xml" binding="webHttpBinding" contract="WcfService1.ServiceContract.IDirectorySearchServiceXml" behaviorConfiguration="xmlRestBehavior"/>
            <endpoint name="restjson" address="json" binding="webHttpBinding" contract="WcfService1.ServiceContract.IDirectorySearchServiceJson" behaviorConfiguration="jsonRestBehavior"/>
            <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="WcfService1.ServiceContract.IDirectorySearchService"/>
            <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="defaultServiceBehavior"> 
                <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="xmlRestBehavior">
                <webHttp/>
            </behavior>
            <behavior name="jsonRestBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

      

My interfaces look like this:

[ServiceContract]
public interface IDirectorySearchServiceXml  
{
    [OperationContract]
    [WebGet(UriTemplate = "Search/")]
    SearchResults Search();
}

[ServiceContract]
public interface IDirectorySearchServiceJson  
{ 
    [OperationContract]
    [WebGet(UriTemplate = "Search/")]
    SearchResults Search();
}

[ServiceContract]
public interface IDirectorySearchService
{
    [OperationContract]
    SearchResults Search(int? sportId, int? instituteId, DateTime? startDate, DateTime? endDate);
}

      

Now I have a little problem with what else I need to get it up and running ... How and given which .svc files I need and I have the correct order ... Also which addresses should I use this to run this through a browser or through a WCF test client. Note. I am currently using 3.5.

Cheers Anthony

UPDATE:

So, if I have something like the following, I will need 3 different svc files ... If so, then there is no point in specifying the address portion at the endpoint ...

public class DirectorySearchServiceXml : IDirectorySearchServiceXml  
{
    ...
}

public class DirectorySearchServiceJson : IDirectorySearchServiceJson  
{ 
    ...
}

public class DirectorySearchService : IDirectorySearchService
{
    ...
}

      

But I could create 1 class that explplictly imports all 3 interfaces, then I only have 1 svc and then the address becomes relevant ... Is that correct?

+2


a source to share


3 answers


It depends: -)

If you want to host your WCF services in IIS (check MSDN How to: Host WCF Service in IIS ), as I assume from your question, you need three things:

  • a virtual directory (and possibly its subdirectory) where you put your service file (yourservice.svc) in
  • service file - short one-line
  • relevant config section in your web.config

A service file (* .svc) is just a tiny text file on one line to instruct IIS on how to create a service. It looks like this:

<%@ServiceHost language=c# Debug="true" 
               Service="Microsoft.ServiceModel.Samples.CalculatorService"%>

      

The attribute language

defines the language of the WCF service, debug

allows you to debug (for dev and test, disable it for production), and Service=

defines the class (completely matching the namespace and everything) actually implements your services.

Then you either need to put these service implementations in a file with code in * .svc (not recommended), or, much better, compile your WCF service implementation into a class library and insert that class library .\bin

into your virtual directory.



And in the end, you need the appropriate configuration on the server-side server.config - from what I can tell, you already have this in place and I think everything should be fine.

Your business addresses will be identified using

  • Server
  • virtual directory (and possible subdirectories)
  • the service file itself

and any additional settings you may have in your configuration for individual service endpoints.

So in your case you would have

  • http://yourserver:port/YourVirtualDirectory/YourService.svc/restxml

  • http://yourserver:port/YourVirtualDirectory/YourService.svc/restjson

  • http://yourserver:port/YourVirtualDirectory/YourService.svc/soap

for your real functions and * http://yourserver:port/YourVirtualDirectory/YourService.svc/mex

for the exchange of metadata (which you won't be using directly).

+2


a source


I also had some problems with multiple endpoints within the same service. I always got 400 error. My mistake was not to use different addresses in web.config. Therefore it is important to use different addresses = configurations for each endpoint (example is in 1st post). One endpoint can omit it or leave it blank. Everyone else needs it.



+1


a source


Just add to this discussion.

I faced the same problem and I ended up using a partial class.

Each endpoint can be implemented with a different interface as you did, but your svc file refers to the same class name, then you split your class into partial classes and here's the smart bit, each partial class can implement a different interface.

You can then separate your implementations and interfaces, but still work around the need for the svc file to reference the same class.

0


a source







All Articles