The new packaging and deployment stuff for SharePoint 2010 is certainly a lot better than VSeWSS but there are a few things missing, such as the ability to add a WCF service as a project item. One of these days I’ll build a template to do it but for now here’s a quick step by step guide.
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. You’ll notice that a new folder is added to the solution
2. We can now go ahead and add our 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
1 <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%> |
2 |
<% @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
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%>
<% @ServiceHost Service="MyProject.MyService" %>
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 dayWe’re now ready to deploy the service to SharePoint.
ac87cf3a-6945-4bd2-a4a2-08e89e8e41d7|0|.0