开发者

Config for WCF with multiple endpoints

开发者 https://www.devze.com 2022-12-28 07:39 出处:网络
I\'m new to WCF and am trying to get some ideas I have off the ground. Basically I have a web WCF Application project with the following in its web.config:

I'm new to WCF and am trying to get some ideas I have off the ground.

Basically I have a web WCF Application project with the following in 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]
  开发者_JS百科  SearchResults Search(int? sportId, int? instituteId, DateTime? startDate, DateTime? endDate);
}

Now the part I am having a little trouble with is what else I need to get this up and running... Like given this what .svc files do I need and do I have the config right... Also what addresses do I need to use to get this running either through the browser or through the WCF test client. Note I am currently using 3.5.

Cheers Anthony

UPDATE:

So If I have something like the following, I would need 3 different svc files... If this is the case then there isn't much point in having address part in the end point...

public class DirectorySearchServiceXml : IDirectorySearchServiceXml  
{
    ...
}

public class DirectorySearchServiceJson : IDirectorySearchServiceJson  
{ 
    ...
}

public class DirectorySearchService : IDirectorySearchService
{
    ...
}

But I could create 1 class that exsplictly implments all 3 interfaces, then I would only have 1 svc and then the address becomes relevent... Is that correct?


It depends :-)

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

  • a virtual directory (and possibly a subdirectory thereof) where you'll put your service file (yourservice.svc) into
  • a service file - a short one-liner
  • an appropriate config section in your web.config

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

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

The language attribute defines the language of the WCF service, debug enables debugging (for dev and test, disable it for production), and the Service= defines which class (fully qualified with namespace and all) actually implements your service(s).

Next, you either need to put those service implementations into a code-behind file of the *.svc (not recommended), or - much better - compile your WCF service implementation into a class library and stick that class library into the .\bin directory under your virtual directory.

And in the end, you need appropriate config in your server-side web.config - from what I can tell, you already have that in place, and I think it should be just fine.

Your service addresses will be determined by the

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

and any additional settings you might have in your config for the individual service endpoints.

So in your case, you'd 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 a * http://yourserver:port/YourVirtualDirectory/YourService.svc/mex for the metadata exchange (which you won't use directly).


I also had some problems with multiple endpoints within one service. I always got error 400. My error was not to use different addresses in the web.config. So it´s important to use different address= configurations for every endpoint (example is in the 1st post). One endpoint can omit it or leave it empty. All others need it.


Just to add to this discussion.

I was faced with the same problem and I ended up going with the use of a partial class.

Each endpoint can be implemented by a different interface as you have done, but your svc file refers to a single class name, you then split your class into partial classes and here's the clever bit, each partial class can implement a different interface.

You can then keep your implementations and interfaces separate but still get around the need for the svc file to refer to one class.

0

精彩评论

暂无评论...
验证码 换一张
取 消