Home > Development > Basic GET REST based support in WCF

Basic GET REST based support in WCF


I have recently just added support for REST for a client I am doing some work for at the moment. Their was some okay documentation on the net, but I found a lot of it quite fragmented. This is not an introduction into what REST is but just what you need to get it working in a read only manner in WCF. I will also not be covering security or anything along those lines along with WCF basics.

So the first thing you need to get this working is ensuring that you have enabled data contract support on your DTOs. Then you need to add ASPNETCompatability to your service host. You need the following in you app config file inside the services section.

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

You also need to tell your class it can use this mode but decorating the CONCRETE class with the following snippet
[AspNetCompatibilityRequirements(RequirementsMode =aspNetCompatibilityRequirementsMode.Allowed)]

You also need a webHttpBinding endpoint as shown here

<endpoint address="RestTestService" binding="webHttpBinding"     contract="Test.WebServices.Services.ITestService" behaviorConfiguration="testWebEndpointBehaviour" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>

Create a endpoint behavior to enable web http as shown here:

 <endpointBehaviors>
        <behavior name="testWebEndpointBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

Lastly on the service interfaces add the WebGet attributes with the URITemplate you want.

[OperationContract]
[WebGet(UriTemplate = "Projects",ResponseFormat = WebMessageFormat.Json)]

Finally use a browser to test.

Blair….

Advertisements
Categories: Development Tags: , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: