Created
February 17, 2010 13:37
-
-
Save Buthrakaur/306608 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VirtualWcfPathProvider : VirtualPathProvider | |
{ | |
public static readonly string VirtualWcfDirectoryName = "~/Services"; // Must start with "~/", must not end with "/". | |
private readonly IDictionary<string, Type> serviceMap; | |
public VirtualWcfPathProvider(IDictionary<string, Type> serviceMap) | |
{ | |
this.serviceMap = serviceMap; | |
} | |
public override bool FileExists(string virtualPath) | |
{ | |
var appRelativeVirtualPath = ToAppRelativeVirtualPath(virtualPath); | |
return IsVirtualFile(appRelativeVirtualPath) || Previous.FileExists(virtualPath); | |
} | |
public override VirtualFile GetFile(string virtualPath) | |
{ | |
var appRelativeVirtualPath = ToAppRelativeVirtualPath(virtualPath); | |
if (IsVirtualFile(appRelativeVirtualPath)) | |
{ | |
var srp = VirtualPathUtility.MakeRelative(VirtualWcfDirectoryName + "/", virtualPath); | |
if (srp.EndsWith(".svc", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
srp = srp.Substring(0, srp.Length - 4); | |
} | |
if (serviceMap.ContainsKey(srp)) | |
{ | |
return new WcfVirtualFile(virtualPath, serviceMap[srp].FullName, typeof(Castle.Facilities.WcfIntegration.DefaultServiceHostFactory).FullName); | |
} | |
} | |
return Previous.GetFile(virtualPath); | |
} | |
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) | |
{ | |
string appRelativeVirtualPath = ToAppRelativeVirtualPath(virtualPath); | |
if (IsVirtualFile(appRelativeVirtualPath) || IsVirtualDirectory(appRelativeVirtualPath)) | |
{ | |
return null; | |
} | |
return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); | |
} | |
private bool IsVirtualFile(string appRelativeVirtualPath) | |
{ | |
if (appRelativeVirtualPath.StartsWith(VirtualWcfDirectoryName + "/", StringComparison.OrdinalIgnoreCase)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
private bool IsVirtualDirectory(string appRelativeVirtualPath) | |
{ | |
return appRelativeVirtualPath.Equals(VirtualWcfDirectoryName, StringComparison.OrdinalIgnoreCase); | |
} | |
private string ToAppRelativeVirtualPath(string virtualPath) | |
{ | |
string appRelativeVirtualPath = VirtualPathUtility.ToAppRelative(virtualPath); | |
if (!appRelativeVirtualPath.StartsWith("~/")) | |
{ | |
throw new HttpException("Unexpectedly does not start with ~."); | |
} | |
return appRelativeVirtualPath; | |
} | |
} | |
public class WcfVirtualFile : VirtualFile | |
{ | |
private readonly string service; | |
private readonly string factory; | |
public WcfVirtualFile(string vp, string service, string factory) | |
: base(vp) | |
{ | |
this.service = service; | |
this.factory = factory; | |
} | |
public override Stream Open() | |
{ | |
var ms = new MemoryStream(); | |
var tw = new StreamWriter(ms); | |
tw.Write(string.Format(CultureInfo.InvariantCulture, | |
"<%@ServiceHost Service=\"{0}\" Factory=\"{1}\"%>", | |
HttpUtility.HtmlEncode(service), | |
HttpUtility.HtmlEncode(factory))); | |
tw.Flush(); | |
ms.Position = 0; | |
return ms; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment