Making a WCF Web Service work with GET requests Making a WCF Web Service work with GET requests asp.net asp.net

Making a WCF Web Service work with GET requests


Looking at your web.config serviceModel section, I can see that you need to add a webHttpBinding and associate an endPointBehavior that includes webHttpGet.

Your operation contract is correct. Here's how your system.serviceModel config section should look in order for you to be able to consume the service from a GET HTTP request.

<system.serviceModel>         <behaviors>        <serviceBehaviors>            <behavior name="MyServiceBehavior">                <serviceMetadata httpGetEnabled="true"    />                <serviceDebug includeExceptionDetailInFaults="true"/>                      </behavior>        </serviceBehaviors>      <endpointBehaviors>        <behavior name="WebBehavior">          <webHttp />        </behavior>      </endpointBehaviors>    </behaviors>        <services>            <service behaviorConfiguration="MyServiceBehavior" name="MyService">        <endpoint address="ws" binding="wsHttpBinding" contract="IMyService"/>        <endpoint address="" behaviorConfiguration="WebBehavior"                  binding="webHttpBinding"                  contract="IMyService">        </endpoint>        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>      </service>    </services>    </system.serviceModel>

Be sure to assign a different address to your wsHttpBinding endpoint, otherwise you will get an error saying that you have two endpoints listening on the same URI.

Another option is to leave the address blank in the wsHttpBinding, but assign a different address to the webHttpBinding service. However, that will change your GET address as well.

For example, if you assign the address as "asmx", you would call your service with the address "MyService.svc/asmx/MyMethod?MyParam=xxxx".


The normal WCF requests are always SOAP requests - you won't be able to get this going with just your browser, you'll need the WCF Testclient for that.

There is an add-on for WCF called the WCF REST Starter Kit (which will also be included in WCF 4.0 with .NET 4.0), which allows you to use GET/POST/PUT/DELETE HTTP commands to query WCF services and such. You need to write your services specifically for REST, though - you can't have SOAP and REST on the same service call.

Marc


As marc_s says, the REST Starter Kit can help, but you should also be aware that .NET 3.5 has support for REST services directly in it. It's not quite as complete as what you can do with the starter kit, but it is useful.

The way it works is that you put a [WebGet] attribute on your operations to indicate where in the URL the various parameters should come from:

[WebGet(UriTemplate = "helloworld/{name}")]string Helloworld(string name);

See this portal for tons of information.

Note, you can have the same service exposed as both SOAP and REST if you specify multiple endpoints/bindings in the configuration.