Write "WCF Virtual Path Provider"¶
Once the file. Svc file is created and you try to access it through your web browser, you will receive an error of type "404-Not found."
This problem is due to a malfunction in VirtualPathProvider implemented in SharePoint class SPVirtualPathProvider, and it plants when parsing URLs that begin with the "~" character.
To resolve this problem simply create your own VirtualPathProvider. It is important to note that this implementation will not repeat all the work done by SPVirtualPathProvider, but it will be added after him through the "Previous" operation of VirtualPathProvider. Ultimately our provider will point to the SPVirtualPathProvider.
But first we must create a class that inherits from "System.Web.Hosting.VirtualPathProvider.
public class WCFVirtualPathProvider : VirtualPathProvider
{
public override string CombineVirtualPaths(string basePath, string relativePath)
{
return Previous.CombineVirtualPaths(basePath, relativePath);
}
//...
// Other methods are not mentioned because they do nothing but call the Previous attribute as above
public override bool FileExists(string virtualPath)
{
string fixedVirtualPath = virtualPath;
if (
virtualPath.StartsWith("~") &&
virtualPath.EndsWith(".svc")
)
{
fixedVirtualPath = virtualPath.Remove(0, 1);
}
return Previous.FileExists(fixedVirtualPath);
}
protected override void Initialize()
{
base.Initialize();
}
}
So to go further in this class operation explanation, we will change the method "FileExists", it will undertake to remove the tilde "~" of the received path and will go for treatment in the SPVirtualPathProvider.
Now we must save our VirtualPathProvider and chain on to SPVirtualPathProvider. To do this we need to run the two lines of code below:
WCFVirtualPathProvider wcfVPP = new WCFVirtualPathProvider(); HostingEnvironment.RegisterVirtualPathProvider(wcfVPP);
Care should be taken to execute this code * once *, and this every time you run "iisreset" and / or modifie web.config. Because ASP.NET forgets the VirtualPathProvider he used after each IISRESET. The most elegant way to solve this problem is to place this code in an HttpModule, which has a static variable responsible for verifying the registration of our VirtualPathProvider. Here is the code of the HttpModule:
public class WCFVPPRegModule: IHttpModule
{
static bool wcfProviderInitialized = false;
static object locker = new object();
public void Init(HttpApplication context)
{
if (!wcfProviderInitialized)
{
lock (locker)
{
if (!wcfProviderInitialized)
{
WCFVirtualPathProvider wcfVPP = new WCFVirtualPathProvider();
HostingEnvironment.RegisterVirtualPathProvider(wcfVPP);
wcfProviderInitialized = true;
}
}
}
}
}
Once our classes are ready we put them in the GAC (in our case we'll put them in a single module called "WCFSupport"), then we modify the Web.config file to add the HttpModule which in turn will reference the VirtualPathProvider.
<add
name="WCFVPPRegModule"
type="WCFSupport.WCFVPPRegModule, WCFSupport, Version=1.0.0.0, Culture=neutral,PublicKeyToken=d4f7bd9e55b39661"
/>
*For users of Windows Sever 2008*: In IIS7 Manager click on "Authentication Settings" target SharePoint site and select "anonymous authentication". This will allow access to service without corrupt Security of your SharePoint site.
Now everything is ready for the use of WCF in SharePoint to be sure you have done it right, open your web browser, at your service URL and if you get a page similar to the image so is everything all right: