SharePoint 2007 as a WCF host - Step #3, create a .svc file

Posted on 9/8/2008 @ 9:13 PM in #SharePoint by | Feedback | 9901 views

Table of Contents for SharePoint as a WCF host

- Basics -

1. Create a WCF Home. This is the virtual directory that will host all your WCF endpoints.

2. Create a WCF Service Library, and throw it in the GAC.

3. Create a relevant .svc file in the WCF home you created in step #1.

4. Write a WCF Virtual Path Provider, and register it in the SharePoint site.


- Real world -

1. Adding WCF Support to a website.

2. Deploying WCF EndPoints as solutions.

Okay good, so you created a _wcf home, and you created a service in a service library. The service library is in the GAC.

The next step is to create a .svc file.

Well, here is how my HelloWorld.svc looks like -

<%@ Assembly Name="HelloWorld, Version=, Culture=neutral, PublicKeyToken=0512278251be14ea"%> 
<% @ServiceHost Service="HelloWorld.service1" %>

Well, is that enough? Not nearly. You haven't specified the endpoint details yet.

In the same _wcf home, create a web.config that looks like this -

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <configuration>
   3:    <system.serviceModel>
   4:      <services>
   5:        <service behaviorConfiguration="HelloWorld.service1Behavior" name="HelloWorld.service1">
   6:          <endpoint address="" binding="wsHttpBinding" contract="HelloWorld.IService1" />
   7:          <host>
   8:            <baseAddresses>
   9:              <add baseAddress="http://moss2007/_wcf/" />
  10:            </baseAddresses>
  11:          </host>
  12:        </service>
  13:      </services>
  14:      <behaviors>
  15:        <serviceBehaviors>
  16:          <behavior name="HelloWorld.service1Behavior">
  17:            <serviceMetadata httpGetEnabled="true" />
  18:            <serviceDebug includeExceptionDetailInFaults="false" />
  19:          </behavior>
  20:        </serviceBehaviors>
  21:      </behaviors>
  22:    </system.serviceModel>
  23:  </configuration>

As you can see above, very very similar to hosting it in a plain vanilla IIS website, I have specified endpoint details. The important/interesting thing to note here is that the base address - is your WCF home.

Great you should be all set huh? Not nearly.

If this was plain vanilla IIS/ASP.NET, then you would have been all set. But go ahead and try accessing your .svc file at http://moss2007/_wcf/HelloWorld.svc and you will be greeted by the below:

What is going on??

Well, after having banged my head with reflector for a few hours, I figured out that the SPVirtualPathProvider isn't coded to handle URLs that start with '~'. (Seriously!).

Anyway, luckily ASP.NET is extensible, so I'm gonna write my own Virtual Path Provider to fix this issue.

Sound off but keep it civil:

Older comments..

On 9/26/2009 4:42:16 PM Ahmer said ..
i am getting error The resource cannot be found. i traced it in eventlog it says endpoint not found exception however when i deploy it in seperate virtual directory it works plz help.

i am giving the correct base address

On 11/12/2009 1:45:50 PM liliana said ..
hey, do you have any ide why am i having this error:

i try to put the assembly like you wrote it but i get

"Could not load file or assembly ''"WcfServicioUsuarios"\,'' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) "