I find it strange that I am writing this post as it would never have occurred to me that I would run into this particular issue. I’ve seen several articles on getting SharePoint to host a Windows Communications Foundation (WCF) service, but I have not seen very much on issues when SharePoint needs to invoke a WCF service.

I had a WCF service running with a SharePoint farm. Lets call that “Farm A”. I had a SharePoint event receiver running on a remote SharePoint farm. Lets call that “"Farm B”. I wanted a very simple procedure to happen: when a list item gets added to a list in Farm B, I wanted to invoke the WCF service running on Farm A.

Simple right? I certainly thought so. I followed excellent article series on hosting WCF services in SharePoint by Sahil Malik, a fellow SharePoint MVP. I deployed the service to Farm A and tested it using a simple Windows Forms application test harness. Everything worked as expected.

So then I copied over my WCF client code from my working test harness to my event receiver running in Farm B. And then the errors started.

When I have the IIS in Farm A configured to use NTLM authentication, I got the error:

The HTTP request is unauthorized with client authentication scheme ‘Ntlm’. The authentication header received from the server was ‘NTLM’

When I have the IIS in Farm A configured to use Basic authentication, I got the error:

The HTTP request is unauthorized with client authentication scheme ‘Basic’. The authentication header received from the server was ‘Basic realm="192.168.161.135"’

Now before jumping to the conclusion that there must have been something wrong with my WCF service configuration in Farm A or my client code in Farm B, let me again say that I had been able to successfully invoke the service from my test harness using both Ntlm and Basic authentication schemes using the IP address. So clearly, there was something happening between the combination of the WCF client code and the SharePoint event receiver framework that was invoking the WCF client.

Unfortunately I could not fix the problem as I would have liked, but I was able to find a workaround. Rather than use the WCF client proxy (Add Service Reference in Visual Studio), I used a Web Service client proxy (Add Web Reference in Visual Studio). The only thing I had to change in the WCF service configuration on Farm A was change the message encoding (messageEncoding) format from Mtom to Text. On the Web Service client running in the event receiver on Farm B, I had to specify that the message format was SOAP 1.1 (System.Web.Services.Protocols.SoapProtocolVersion.Soap11).

Certainly this is not an ideal solution, but it allows me to proceed with my project. Here’s hoping that SharePoint 2010 and WCF play a little more nicely together 🙂