SharePoint 2007 as a WCF host - Step #1, make a WCF Home

Posted on 9/8/2008 @ 9:14 PM in #SharePoint by | Feedback | 14892 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.


The first thing you need to do is find a home for your WCF services. You could put them inside _layouts or _vti_bin, but I like to keep things clean and create a new home for them for the following reasons:

a) You will be adding significant information in the web.config

b) Isolation

c) Won't interfere with Microsoft's future updates.

Okay thats great, so lets get started. You can create such a WCF home using the following steps -

  1. First, create a physical directory. I like to create it inside the same virtual directory that is hosting my SharePoint site. This way, all relevant security permissions are simply inherited. I created a dir at C:\Inetpub\wwwroot\wss\VirtualDirectories\80\_wcf.
  2. Next, on your port 80 (or whatever port you are WCF enabling), through IISMGR, create a new directory .. call it _wcf. Do not turn it into a virtual directory - if you do, you will have to make modifications to the VirtualPathProvider I intend to show in step #4.
  3. Run the following - "%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r -y

Okay good, your home is all setup.

Next, create a WCF Service in a WCF Service library, and throw it in the GAC.

After which, you can create a .svc file.

Sound off but keep it civil:

Older comments..


On 5/11/2008 4:26:56 PM Peter said ..
I think you need a "Step 0: Why are we using WCF again?" post, because...well, I'm curious.


On 5/11/2008 4:28:31 PM Peter said ..
Oops, scratch that, apparently the "intro" post dropped off your front page and I missed it. Ok, disregard my previous comment.


On 5/11/2008 6:41:46 PM Sahil Malik said ..
LOL :).

Okay, is there a better way I can surface such content on my front page? The thing is, I don't think a pager would be very helpful in searching through a gazillion blogposts.

What do you say?

SM


On 5/14/2008 1:13:56 PM Peter said ..
Let's just forget this ever happened :)


On 5/14/2008 1:36:49 PM Sahil Malik said ..
OMG WTF! It's not like I caught you changing clothes or sump'n :)

Okay - Men in Black - memory erased :)

SM


On 7/15/2008 4:24:27 AM arun said ..
through IISMGR, create a new virtual directory .. call it _wcf. Do not turn it into a virtual directory -

What does this mean? you create a new virtual directory, but do not turn it inot one??


On 7/15/2008 10:08:45 AM Sahil Malik said ..
Arun - all that means is, when I wrote the above, I was high!

I've fixed the error.

BTW - ideally, the directory should be under _vti_bin, so you can deploy stuff using solutions, but this post explains everything in a very hands-on manner.

Sahil


On 8/20/2008 6:19:29 AM arun mathew said ..
thanks for your reply:). This post was really helpful.

Can the code be used for work with virtual directories now?

My services work when i add a directory, as given in this post. But if i try to deploy them on another machine using a web setup project, the setup creates a virtual directory by default. And then I am unable to even browse the wcf services.


It gives an error saying "resource not found"

Is there any change required to the wcfvirtualpath provider, as you have said above, for working with virtual directories?


Please help.


On 7/20/2010 2:57:08 PM Dan said ..
I realize that this post is a couple years old, but I have found it very helpful and I have one question. Above, you say :

"Next, on your port 80 (or whatever port you are WCF enabling), through IISMGR, create a new directory .. call it _wcf. Do not turn it into a virtual directory - if you do, you will have to make modifications to the VirtualPathProvider I intend to show in step #4."

This confuses me. If I am in IIS Manager and I right-click on _vti_bin, I only have an option to create a Virtual Directory. How would I create a new directory but not make it a virtual directory?


On 7/28/2010 11:01:11 AM Jag said ..
Hi Sahil,

You are such a star :-) Thanks a lot for this great post. I successfully managed to host a WCF service inside sharepoint.

Thanks a lot.

Cheers---Jag/UK


On 8/1/2011 9:36:01 AM Rizwan Ansari said ..
Hi ,

I have hosted the WCF service in SharePoint as per the above article. However, I am facing strange issue, where the WCF service start failing and cannot be consumed after IISRESET. Even if I open the site it does not trigger the service. I have to explicitly hit the URL http://sitename/_vti_bin/WCFServices/HelloWorld.svc using the browser to trigger off the service.

Following are the error details :

------------------- Error Start ------------------------


{System.ServiceModel.ProtocolException: The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: '<html>


<head>


<title>Runtime Error</title>


<style>


body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}


p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}


b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}


H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }


H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }


pre {font-family:"Lucida Console";font-size: .9em}


.marker {font-weight: bold; color: black;text-decoration: none;}


.version {color: gray;}


.error {margin-bottom: 10px;}


.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }


</style>


</head>

<body bgcolor="white">

<span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

<h2> <i>Runtime Error</i> </h2></span>

'. ---> System.Net.WebException: The remote server returned an error: (500) Internal Server Error.


at System.Net.HttpWebRequest.GetResponse()


at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)


--- End of inner exception stack trace ---

Server stack trace:


at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException)


at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)


at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)


at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)


at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)


at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)


at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)


at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:


at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)


at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)


at TestClient.Service.IHelloWorld.SayHello(String inputName)


at TestClient.Service.HelloWorldClient.SayHello(String inputName) in D:\PERSONAL\RnD\Sharepoint\MyServices\TestClient\Service References\Service\Reference.cs:line 50


at TestClient.Program.Main(String[] args) in D:\PERSONAL\RnD\Sharepoint\MyServices\TestClient\Program.cs:line 23}

------------------- Error End ------------------------

Please advice.

Regards