3

I know that similar question was asked here :

Running SOAP and RESTful on the same URL

Hosting WCF soap and rest endpoints side by side

but didn't find an answer to my problem.

I have two custom servicehostfactories that enables Dependency Injection :

public class StructureMapSoapServiceHostFactory : ServiceHostFactory 
public class StructureMapRestServiceHostFactory : WebServiceHost2Factory

The implementation details are not important here.

Then I definied two endpoints in web.config

<system.serviceModel>
    <behaviors>
   <serviceBehaviors>
    <behavior name="mexGet">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="jsonBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<bindings>
  <mexHttpBinding>
    <binding name="mexHttpBinding" />
  </mexHttpBinding>
</bindings>
      <services>
     <service behaviorConfiguration="mexGet" name="ServiceImplementation.ServiceCategory">
        <endpoint address="rest" 
                  binding="webHttpBinding" 
                  contract="Contracts.ServiceContracts.Mobile.IServiceCategory"
                  behaviorConfiguration ="jsonBehavior"/>
        <endpoint address="soap"
                  binding="basicHttpBinding"
                  contract="Contracts.ServiceContracts.Mobile.IServiceCategory" />
        <endpoint name="mexHttpBinding"
                  address="mex"
                  binding="mexHttpBinding" bindingConfiguration="mexHttpBinding"
                  contract="IMetadataExchange" />
     </service>
  </services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

Then I created two .svc files for each custom host factories : ServiceCategoryRest.svc ServiceCategorySoap.svc

I don't like it. What I would like to do is to have URL in that style :

REST : http://server:port/rest/categories/{id} which mapps to the implementation of my ServiceCategory.GetCategory(int id)

SOAP : http://server:port/soap/GetCategory?id=someId

My questions are. Do i need different svc files to activate host services ? If I need there two .svc files, how can I achieve the URI above ? I'm afraid that I should configure IIS rewriting or something but would like to avoid that.

Thanks in advance for your help.

Thomas

Community
  • 1
  • 1
Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73

2 Answers2

3

You can achieve what you're looking for with service routes - part of ASP.NET routing, available from ASP.NET 3.5 SP1 on up.

Check out these resources:

In .NET 3.5 SP1, you need to add some extra infrastructure to your web.config (web routing module etc.) - while in .NET 4, this is all already built in.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thanks for your answer. I read articles you provided with links and have more questions : Which binding will be used when registering my custom serviceHostfactory that derives from ServiceHostFactory ? The one that derives from WebServiceHostFactory2 will use webHttpBinding and that Ok. The next question is, can I pass complex types as arguments to operations in WCF as RESTful service ? I thought it was not possible but after reading articles you provided it seems that it is. – Tomasz Jaskuλa Apr 19 '11 at 07:38
  • Also, all examples are for Framework 4.0 and nothing for 3.5 where for example there is no ServiceRoute class etc. – Tomasz Jaskuλa Apr 19 '11 at 08:53
  • @Thomas Jaskula: sorry, couldn't find any blog post for NET 3.5 anymore - but I know, with some additional setup, it is possible in .NET 3.5 SP1, too. – marc_s Apr 19 '11 at 09:33
  • I think I'll follow Rick Strahl blog post http://www.west-wind.com/weblog/posts/570695.aspx to achieve it – Tomasz Jaskuλa Apr 19 '11 at 10:23
0

After few searches I found out that in fact I don't need two different .svc files and two different ServiceHostFactories.

I kept only the StructureMapRestServiceHostFactory : WebServiceHost2Factory and ServiceCategoryRest.svc which handles well requests in REST mode and call in RPC-SOAP mode.

So if you want to run side by side the REST and the SOAP you can do it only with WebServiceHost2Factory.

If then you want to get rid of the .svc part from the URL, please read the Rick Strahl post west-wind.com/weblog/posts/570695.aspx.

Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73