Simple Steps to Deploy WCF Service to SharePoint 2010

I am trying to deploy my own project which I think is much easier. I make the Visual Studio project that allows WCF services more easily added in SharePoint.

This is the quick step to build WCF service as a project item.

Add SVC file to Layouts Folder

To make a Windows Communication Framework service available we need to host it somewhere. Since SharePoint runs on IIS, we need to create a .svc file with details of the service implementation. Of course before we create the file we need somewhere to put it and for the purposes of this demonstration we'll use a custom subfolder within the %sproot%\TEMPLATE\Layouts folder. We can set up this folder automatically using our Visual Studio project.

1.       From the Project Menu select Add SharePoint “Layouts” Mapped Folder

2.       Add your MVC project, I use MyWCFService.svc file. In the Layouts\<MyProjectName> folder. Add a new XML File named MyWCFService.svc.

3.       Replace the contents of the file with the following code:


<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%> 
<% @ServiceHost Service="MyProject.MyService" %>

Token Replacement in Visual Studio

Visual Studio 2010 allows the use of replaceable tokens when creating SharePoint solution packages. Our .svc file makes use of the token $SharePoint.Project.AssemblyFullName$ that will be replaced when the package is built, by the 4 part assembly name for the associated assembly. However, tokens are not automatically replaced in files with an .svc extension. Thankfully this is a simple problem to resolve.

1.       Navigate to C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools

2.       Open the Microsoft.VisualStudio.SharePoint.targets file. You'll find that this is an Xml format file that defines various configuration settings for building SharePoint projects.

3.       Find the TokenReplacementFileExtensions element and append svc to the list of file extensions as shown:

<TokenReplacementFileExtensions>$(TokenReplacementFileExtensions);xml;aspx;ascx;webpart;dwp;svc </TokenReplacementFileExtensions>

Adding WCF service configuration to SharePoint

As well as an .svc file, IIS also needs to reads the configuration of the WCF service from the web.config file. For the purposes of this quick how-to we'll make the necessary changes manually.

1.       Open the web.config file for our application (this will be found at C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config if the application is the first application running on port 80).

2.       In the system.serviceModel element add the following configuration details:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
<bindings> 
    <basicHttpBinding> 
        <binding name="MyDemoBinding"> 
            <security mode="TransportCredentialOnly"> 
                <transport clientCredentialType="Ntlm" /> 
            </security> 
        </binding> 
    </basicHttpBinding> 
</bindings> 
<behaviors> 
    <serviceBehaviors> 
        <behavior name="MyDemoBehavior"> 
            <serviceMetadata httpGetEnabled="true" /> 
            <serviceDebug includeExceptionDetailInFaults="false" /> 
        </behavior> 
    </serviceBehaviors> 
</behaviors> 
<services> 
    <service behaviorConfiguration="MyDemoBehavior" name="MyProject.MyService"> 
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="MyDemoBinding" contract="MyProject.IMyService"> 
        <identity> 
            <dns value="localhost" /> 
        </identity> 
    </endpoint> 
    <host> 
        <baseAddresses> 
            <add baseAddress=”http://localhost/_layouts/MyProjectName” /> 
        </baseAddresses> 
    </host> 
    </service> 
<services>

Note: In an ideal world we'd add some code to our SharePoint solution that would automatically add the appropriate configuration details to the web.confg file but that's a story for another day.

We're now ready to deploy the service to SharePoint. From the Build menu select Deploy MyProject. Visual Studio will now build the solution, create a WSP package, and then deploy the package to our SharePoint server.