Monday, July 23, 2012

SharePoint and WCF

I recently upgraded an ASMX project to WCF. It took me a couple of days (investigating, implementing and testing) to figure out, and although I'm sure my solution can still be polished, here are the steps so far to make it work:


1) Copy your .svc file to ISAPI


2) Make sure the markup of the .svc files is as follows


<%@ ServiceHost Language="C#" Debug="true" Service="MyApp.Service.TestService,  MyApp.Service , Version=1.0.0.0, Culture=neutral, PublicKeyToken=3333333333" %>


3) We are accessing the WCF services using jQuery, so we had to enable the webHttpBinding. Security needs to be enabled like this:



<security mode="TransportCredentialOnly">
  <transport clientCredentialType="Ntlm" /> 
</security> 



4) Create an endpoint behavior that includes


<webHttp />


5) Allow Anonymous Authentication on your IIS site.


6) Make sure you use the DataContract and DataMember directives where appropriate. You might get hard-to-debug errors when a type cannot be serialized correctly.


Along the way I learned a lot about Ntlm. At first I was getting a lot of "Unauthorized Access" responses, and I thought maybe using Ntlm was the problem. The real problem was that my WCF configuration was incorrect. Also, Ntlm is a protocol that uses a handshake, so you'll notice the same request three times at the beginning of the communication. Any subsequent request should only appear once.

No comments: