WCF, Silverlight, and SharePoint… Oh my!

I was recently working on a project and needed to have Silverlight make a WCF call.  That is pretty straight forward until you place the Silverlight code inside SharePoint and want it to call a WCF that is also inside SharePoint.

The first problem that I had to solve was getting Silverlight to be able to call the WCF correctly.  Since in Visual Studio when you make the connection, it will have the local server for the path.  Like http://localhost:95559/MyService.svc and for SharePoint I wanted it to point to the local server’s name and in the _layouts directory.  Luckily I came across the following blog that helped with getting that to work: Silverlight ServiceReferences.ClientConfig Alternatives

The only change I had to make was to the BasicHttpBinding:

    1: BasicHttpBinding binding = new BasicHttpBinding(
    2:             Application.Current.Host.Source.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase) 
    3:             ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly);

Then I had to figure out how to get WCF hosted in SharePoint correctly.  For that, I turned to this post: Hosting a WCF service in Windows Sharepoint Services v3.0

So I now had a httpModule that would handle the *.svc requests that start with a ~.  That is important to make sure because you will get called again without the ~ so make sure you check for that before removing the first character.  I also had Silverlight setup where it could call the SharePoint machine regardless of what Visual Studio wanted to do.  I followed the “Dynamic Configuration” setup.

When I had this setup, I then got problems still.  The only way I could see the problem was running something like Network Monitor or Web Development Helper.  In there I saw a 500 error being returned from the server.  I attached a debugger and saw that it was giving me this message:

Security settings for this service require ‘Anonymous’ Authentication but it is not enabled for the IIS application that hosts this service.

From this error, I realized that WCF uses anonymous by default.  So I went into my web.config file and changed the binding to support Windows:

    1: <bindings>
    2:     <basicHttpBinding>
    3:         <binding name="MyBinding">
    4:             <security mode="TransportCredentialOnly">
    5:                 <transport clientCredentialType="Windows"/>
    6:             </security>
    7:         </binding>
    8:     </basicHttpBinding>
    9: </bindings>

Then you just add a bindingConfiguration=”MyBinding” on your service endpoint.

After I made that change, I ran it again and this time I got the following error:

Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service.

So I changed my binding to:

    1: <bindings>
    2:     <basicHttpBinding>
    3:         <binding name="basicHttpBinding">
    4:             <security mode="TransportCredentialOnly">
    5:                 <transport clientCredentialType="Ntlm"/>
    6:             </security>
    7:         </binding>
    8:     </basicHttpBinding>
    9: </bindings>

Note: If you have a ”mex” endpoint you may get another error about ‘anonymous’ because that uses anonymous by default.  If you do, you can delete the mex endpoint unless you need discoverability.  Then you will need to remove anonymous from that as well.

After making these changes, Silverlight was able to call back into the ASP.NET project that it was loaded from and call a WCF service that was running inside SharePoint.