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 🙂
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…
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.
Hi Eugene.
The same problem occurs when using Web services
Константин, 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.
But how to do it on someone other's server? 🙁
You can not. It is a server-level extension that must be activated on each web application that will get the Guest account support.
Hi Eugene:
I am not sure if you have the problems fixed. I had the same exact issues, but I was able to solve a couple of them.
Here are my suggestions.
1. turn off all your firewall.
2. Make sure all your IIS services are up and running.
3. Make sure your WCF Host app.config is actually getting read. (this should fix your end-point issue)
I am having a problem where my Event Receiver project is not reading my configuration files that contain my WCF Client information. I am using ConfigurationManager.
4.
Howie, in regards to items 1-3, the service invocation was working from the test harness. I only saw the problems when doing the invocation from the SharePoint event receiver, as it appears you are as well. That's why I went the rout of bypassing the configuration files and initializing the WCF proxies solely through code.
I see.
do you have any sample code to create the WCF endpoint structure?
It annoys me that the configuration files are not getting read tho. How do you plan to configure the IPs of the Host in your WCF client app??
Hi Eugene:
Just an update, I gave up trying to read the web.config file. and took your second approach as well.
btw, did you get into a problem with System.WebPermission?
It seems that I can't run my WCF client inside of the itemAdded method because my app doesn't have enough trust. if you had the same issue, how did you fix it?
Thank You
I made sure that all of application and supporting DLLs were deployed to the Global Assembly Cache (GAC). That should take care of all code access security errors (CAS).