To host a WCF service into SharePoint 2010 and surface its data using JSON serialization. After setting the service up and deploying it to SharePoint I run into a few issues before I could get any joy. This post will show you the problems I run across.
NOTE: This post won't go into details about how to deploy a WCF service into SharePoint 2010. I'm planing to cover this in further detail in my next post.
Okay so, to start with here's the interface of the WCF method. The implementation of GetMessageJSON() is simply the almighty "Hello World!" hence why it's omitted.
When I tried to browse the JSON method I got nothing but a blank HTML page. Fiddler helped me to discover there was exception there 400 Bad Request Exception.
http://YourServer/_vti_bin/JqueryWCFRestSP/CustomerService.svc/GetJOSNMessage
However I could browse to the service WSDL with no problem and I could see its definition which appeared to be fine.
http://YourServer/_vti_bin/JqueryWCFRestSP/CustomerService.svc?wsdl
Strange, could get to the WSDL but can't get to the JSON method? Mmmmm, I knew there was nothing wrong with the WCF method (return "Hello World!" come on!) nor with its interface. So the actual problem was in the web.config this time as I was using basicHttpBinding which does not support JSON. Point taken, changed the binding to webHttpBinding which does, redeployed the solution and problem gone. Next!
This time the WebRequest when through however I was getting the following response.
Later on I found out this can be due to the following reasons:
- Using different contracts between client and sender.
- Different binding between client and sender.
- The message security settings are not consistent between client and sender.
As Brian McNamara correctly pointed out when web-hosted, the EndpointAddress of the service comes from the IIS base address (host headers). In this case, it appears to be the "our-server" address. By default, WCF ensures that the To of each Message matches the intended address. If the service just has this one endpoint, a quick workaround would be to use
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
which turns off the address filter. Otherwise, ensure the IIS host headers contain the 'public facing base address' that you want clients to connect to, so that messages will be properly addressed to the service.
Unfortunately, Brian's logical input only fixed part of the problem (Thanks Brian!) So what about the other part? Well, the credit this time goes to Kiyoshi Kusachi.He introduced a "nested" multi behaviour approach which turned to be the key of success. After updating the web.config accordingly the AddressFilter mismatch at EndPointDispatcher vanished completely.
Finally, I was able to serialize it successfully to JSON using the snippet (Console App).
WebClient proxy = new WebClient();
byte[] data = proxy.DownloadData("http://YourServer/_vti_bin/JqueryWCFRestSP/ CustomerService.svc/GetJOSNMessage");
Stream stream = new MemoryStream(data);DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));string result = obj.ReadObject(stream).ToString();
Console.WriteLine(result.ToString());
Console.ReadKey(true);
Time for lunch now. Happy WCFing!