Hosting WCF soap and rest endpoints side by side

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have written a service that I would like expose both via rest and soap. Everything I read about WCF 4.0 says that I just need to expose 2 endpoints with differing behaviors to do this. But I cannot get it to work.

Here is my service contract:

[ServiceContract]
public interface MyService
{
    [OperationContract]
    [WebGet(UriTemplate="data/{value}")]
    string GetData(string value);
}

Here is my web.config:

<?xml version="1.0"?>
<configuration>

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>

        <services>
            <service name="MyService">
                <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>
                <endpoint address="rest" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="MyService" />
                <endpoint address="soap" behaviorConfiguration="soapBehavior" binding="basicHttpBinding" contract="MyService" />
            </service>
        </services>

        <behaviors>

            <serviceBehaviors>
                <behavior>
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>

            <endpointBehaviors>
                <behavior name="restBehavior">
                    <webHttp automaticFormatSelectionEnabled="true" helpEnabled="true" />
                </behavior>
                <behavior name="soapBehavior" />
            </endpointBehaviors>

        </behaviors>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

    </system.serviceModel>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
    </system.webServer>

</configuration>

I am using routing to define my service url:

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("dns", new ServiceHostFactory(), typeof(MyService)));
        }
    }

Is there something that I am doing wrong here? I could really use some help.

Answers

I never found the "right" way to do this in configuration but was able to use the routing engine to accomplish this.

My global asax file now looks like this:

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("my/soap", new ServiceHostFactory(), typeof(MyService)));
            RouteTable.Routes.Add(new ServiceRoute("my/rest", new WebServiceHostFactory(), typeof(MyService)));
        }
    }

and my config like this: (to enable the rest help pages)




<system.serviceModel>

    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint automaticFormatSelectionEnabled="true" helpEnabled="true"/>
        </webHttpEndpoint>
    </standardEndpoints>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

</system.serviceModel>

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>


I like that this is in line with the asp.net MVC model more and requires little config. Additionally doing it this way allowed me to remove the .svc files from my project entirely which is also a plus IMO.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/3366103/hosting-wcf-soap-and-rest-endpoints-side-by-side

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils