tags:

views:

53

answers:

1

Hi guys

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]
    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?

+2  A: 

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).

marc_s
Thanks for the feedback... big question I have is, for the interfaces I have and the config I want, what svc's do i need and what are their names? Do I only need the 1 svc which points to default service?
vdh_ant
@vdh_ant: you need **one SVC file per service class** that you have. If you have a single service class that implements all three service contracts, then you need **one SVC file** - when you have three separate classes each implementing one of the service contracts, then you need **three SVC files** - one per service class. What you name it is totally up to you - whatever you like - but be aware - the name and the .svc extension become part of your service's URL! Don't go too crazy on those names....
marc_s
Thanks for the update... Please see update above.
vdh_ant