Wednesday, July 15, 2009

WCF errors from SharePoint event receiver

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 :)

6 comments:

Steve said...

Hi Eugene...
I have the same problem.
When a item is added in a SharePoint list, I want to add a record in a specific SQL Server table.
My WCF works correctly when it's called by a Silverlight application but doesn't work when it's called by an SharePoint Event Receiver...
I get the following error : "Could not find default endpoint element that references contract ‘PamelaWCF.IPamelaServices’ in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."
Did you finally fix your problem ?
Thanks for your help...

Eugene Rosenfeld [MOSS MVP] said...

Steve, no, I was never able to get a fix for this problem. However, I also ran into a similar problem to yours. There are two ways to address your problem:
1. Create a feature receiver that adds the WCF endpoint data to the web.config file using code
2. Create the WCF endpoint structure using code. No web.config settings required.
I ultimately chose option 2.

DkmS said...

Hi Eugene.
The same problem occurs when using Web services

Eugene Rosenfeld [former MOSS MVP] said...

Константин, thanks for the comment. Yes, SharePoint has many inconsistancies in how different parts of the system treat anonymous users. Reza Alirezaei [SharePoint MVP] has a nice article about enabling a named Guest account on SharePoint as an alternative to the more problematic anonymous access.

DkmS said...

But how to do it on someone other's server? :-(

Eugene Rosenfeld [former MOSS MVP] said...

You can not. It is a server-level extension that must be activated on each web application that will get the Guest account support.