Friday 27 November 2009

SilverLight RESTing on Windows Communication Foundation (WCF)

1. Build the WCF service
A few things to keep in mind when building the service using REST:
a. Use the WebHttpBinding to expose the service

<endpoint address="services/rest" binding="webHttpBinding" bindingConfiguration="MyRestServiceBinding"

contract="_Corp_.Services.Rest.IRestService"

behaviorConfiguration="webHttpBehavior"/>


b. Use appropriate [WebGet] and [WebInvoke] attributes on operations of your service contract

[OperationContract]

[WebGet(UriTemplate = "/uppercase?value={value}")]

string GetUpperCaseString(string value);


c. To avoid cross domain issues while consuming the WCF service from the Silverlight, the WCF service must somehow define its client access policy.
i. If the service is hosted in IIS, this client access policy defined in a ClientAccessPolicy.xml file must be placed at the ROOT of the web site. Placing this file at the same level as the web application does not work.

ii. During development, most of the time the service is hosted by Visual Studio. In this case, you don’t have a web server where you can simply drop the client access policy file at the root of the web site hosting your web service. One solution to this problem is to expose a service endpoint with a WebHttpBinding which will provide the client access policy to Silverlight client applications.

<!-- The client access policy SHOULD be exposed at the ROOT of the HOST (Web server / VS studio host , etc) -->

<endpoint address="" binding="webHttpBinding" bindingConfiguration="MyRestServiceBinding" contract="_Corp_.Services.Rest.IClientAccessPolicy" behaviorConfiguration="webHttpBehavior" />


[ServiceContract(Namespace = Constant.ServiceNamespace200911)]

public interface IClientAccessPolicy

{

[OperationContract]

[WebGet(UriTemplate = "/clientaccesspolicy.xml")]

Stream GetClientAccessPolicy();

}

2. Consume the WCF service in REST mode from a Silverlight application

The REST WCF service can be consumed from a Silverlight application without generating a service proxy. The WebClient class can be used to directly to invoke a service operation by knowing the operation (resource) URI.


WebClient webClient = new WebClient();

webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);

webClient.DownloadStringAsync(new Uri("http://localhost:9090/services/rest/uppercase?value=sHaHmOhAmOd")));


A running sample can be made available on request. This sample has been constructed using:
- Visual Studio 2010 Beta 2
- .Net Framework 4.0 Beta 2
- Windows Communication Foundation (WCF) 4 Beta 2
- Silverlight 3

Enjoy.

Sh@h

Sunday 31 May 2009

Duplicate types generated in WCF proxy

The other day I ran into a problem where I was generating a WCF proxy which without any modification was not compiling. The error message was that some class "C" with property "P" was already defined.

When verifying the generated proxy I noticed that I indeed has 2 versions of a fault contract in my proxy code:
- one decorated with the DataContractSerializer
- and the other one was present to be used by the XmlSerializer

I was getting this problem because I was using a fault contract shared by 2 service operations. The return type of one these operations was of type "Stream". Apparently the "Stream" class is not compliant with the DataContractSerializer and that is why WCF was generating XmlSerializable faut contract. And as my second service operation was DataContractSerializable, WCF was generating a another version of my fault contract which was DataContractSerializable.

Tuesday 10 March 2009

Creating an ASMX service with WCF

A few things to know when making your WCF service compatible with ASMX clients:

1. The service must expose an endpoint using the basicHttpBinding.

2. When generating the service proxy, use the Visual Studio "Add Web Service Reference" option instead of the "Add Service Reference" option.

  • In fact, the "Add Web Service Reference" uses the wsdl.exe command to generate proxies compatible with ASMX clients. Moreover, WCF will use the XmlSerializer rather than the DataContractSerializer while communicating with these clients.
  • The "Add Service Reference" option uses the svcutil.exe command to generate WCF proxies.

3. BasicHttpBinding only supports UserName and Certificate credentials. So for example, you will not be able to use Integrated Windows Authentication to secure for authenticating your WCF service ASMX client.