Sharepoint 2010 : Creating a Pluggable Workflow Service (part 3) - Hosting an .svc File in SharePoint, Token Replacement in Visual Studio , Adding WCF Service Configuration to SharePoint
Hosting an .svc File in SharePoint
To make our service available for our client application, we need to host it somewhere. Since SharePoint runs on Internet Information Server (IIS), we need to create a .svc file with details of our service implementation. Of course, before we create the file, we need somewhere to put it; 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.
Choose Project | Add SharePoint “Layouts” Mapped Folder. You’ll notice that a new Layouts folder is added to the solution:
We can now go ahead and add our CalculationResultService.svc file. In the Layouts\WorkflowDemonstration folder, add a new XML file named CalculationResultService.svc. Replace the contents of the file with the following:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%>
<% @ServiceHost Service="WorkflowDemonstration.CalculationResultService" %>
Token Replacement in Visual Studio
Visual Studio 2010 allows the use of replaceable tokens when creating SharePoint solution packages. Our code sample makes use of the token $SharePoint.Project.AssemblyFullName$ that will be replaced when the package is built, by the four-part assembly name for the associated assembly. However, at the time of writing, no WCF template is available for SharePoint. Therefore, tokens are not automatically replaced in files with an .svc extension.
Thankfully, this is a simple problem to resolve. Navigate to C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools and then open the Microsoft.VisualStudio.SharePoint.targets file. This is an XML format file that defines various configuration settings for building SharePoint projects. 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 read the configuration of the WCF service from the web.config file. For the purposes of our demonstration, we’ll make the necessary changes manually.
Open the web.config file for our application (at C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config if the application is the first application running on port 80). In the system.serviceModel element, add the following configuration details:
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="WfDemoBinding">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="WfDemoBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WfDemoBehavior"
name="WorkflowDemonstration.CalculationResultService">
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="WfDemoBinding"
contract="WorkflowDemonstration.ICalculationResultService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost/_layouts/WorkflowDemonstration" />
</baseAddresses>
</host>
</service>
</services>
We’re now ready to deploy the service to SharePoint. From the Build menu select Deploy WorkflowDemonstration. Visual Studio will now build the solution, create a WSP package, and then deploy the package to our SharePoint server.
As we did for our client application, we can now make use of WCFTestClient to send a test call to our WCF service. This time the endpoint address will be http://localhost/_layouts/WorkflowDemonstration/CalculationResultService.svc. If all is well, our service will return True when invoked as per our stub implementation.