Mat*_*ott 11
使用Darko动态IIS托管的WCF服务工作的衍生产品,您可以实现您想要的功能.让我们从我们可能想要托管的示例服务开始,我们称之为IMessageBroker
,它的合同很简单:
[ServiceContract]
public interface IMessageBroker
{
[OperationContract]
string Send(string message);
}
Run Code Online (Sandbox Code Playgroud)
我们将此合同用于服务和MEF出口/进口.我们还将定义一些额外的元数据:
public interface IMessageBrokerMetadata
{
public string Name { get; }
public string Channel { get; }
}
Run Code Online (Sandbox Code Playgroud)
由于这是一个简单的项目,我会欺骗并使用一个简单的静态类来管理CompositionContainer
用于组成部件的MEF :
public static class MEF
{
private static CompositionContainer container;
private static bool initialised;
public static void Initialise()
{
var catalog = new DirectoryCatalog("bin");
container = new CompositionContainer(catalog);
initialised = true;
}
public static CompositionContainer Container
{
get
{
if (!initialised) Initialise();
return container;
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了能够动态生成WCF服务,我们需要创建一个ServiceHostFactory,它可以访问我们的组合容器来访问我们的类型,所以你可以这样做:
public class MEFServiceHostFactory : ServiceHostFactory
{
public override ServiceHostBase CreateServiceHost(string constructorString, System.Uri[] baseAddresses)
{
var serviceType = MEF.Container
.GetExports<IMessageBroker, IMessageBrokerMetadata>()
.Where(l => l.Metadata.Name == constructorString)
.Select(l => l.Value.GetType())
.Single();
var host = new ServiceHost(serviceType, baseAddresses);
foreach (var contract in serviceType.GetInterfaces())
{
var attr = contract.GetCustomAttributes(typeof(ServiceContractAttribute), true).FirstOrDefault();
if (attr != null)
host.AddServiceEndpoint(contract, new BasicHttpBinding(), "");
}
var metadata = host.Description.Behaviors
.OfType<ServiceMetadataBehavior>()
.FirstOrDefault();
if (metadata == null)
{
metadata = new ServiceMetadataBehavior();
metadata.HttpGetEnabled = true;
host.Description.Behaviors.Add(metadata);
}
else
{
metadata.HttpGetEnabled = true;
}
return host;
}
}
Run Code Online (Sandbox Code Playgroud)
本质上,该constructorString
参数用于传递我们想要的特定服务的元数据名称.接下来,我们需要处理这些服务的定位.我们现在需要的是一个VirtualPathProvider
我们可以通过一个动态创建实例的方法VirtualFile
.提供者看起来像:
public class ServiceVirtualPathProvider : VirtualPathProvider
{
private bool IsServiceCall(string virtualPath)
{
virtualPath = VirtualPathUtility.ToAppRelative(virtualPath);
return (virtualPath.ToLower().StartsWith("~/services/"));
}
public override VirtualFile GetFile(string virtualPath)
{
return IsServiceCall(virtualPath)
? new ServiceFile(virtualPath)
: Previous.GetFile(virtualPath);
}
public override bool FileExists(string virtualPath)
{
if (IsServiceCall(virtualPath))
return true;
return Previous.FileExists(virtualPath);
}
public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
return IsServiceCall(virtualPath)
? null
: Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
Run Code Online (Sandbox Code Playgroud)
我们正在做的是将任何调用映射/Services/
到我们的MEF派生端点.该服务需要一个虚拟文件,这就是我们将它们联系在一起的地方:
public class ServiceFile : VirtualFile
{
public ServiceFile(string virtualPath) : base(virtualPath)
{
}
public string GetName(string virtualPath)
{
string filename = virtualPath.Substring(virtualPath.LastIndexOf("/") + 1);
filename = filename.Substring(0, filename.LastIndexOf("."));
return filename;
}
public override Stream Open()
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write("<%@ ServiceHost Language=\"C#\" Debug=\"true\" Service=\"" + GetName(VirtualPath) +
"\" Factory=\"Core.MEFServiceHostFactory, Core\" %>");
writer.Flush();
stream.Position = 0;
return stream;
}
}
Run Code Online (Sandbox Code Playgroud)
虚拟文件将从虚拟路径中分解元数据名称,其中/Services/SampleMessageBroker.svc
- > SampleMessageBroker
.然后我们生成一些代表.svc
文件标记的标记Service="SampleMessageBroker"
.此参数将传递到MEFServiceHostFactory
我们可以选择端点的位置.因此,给出一个样本端点:
[Export(typeof(IMessageBroker)),
ExportMetadata("Name", "SampleMessageBroker"),
ExportMetadata("Channel", "Greetings")]
public class SampleMessageBroker : IMessagerBroker
{
public string Send(string message)
{
return "Hello! " + message;
}
}
Run Code Online (Sandbox Code Playgroud)
我们现在可以动态访问它/Services/SampleMessageBroker.svc
.您可能想要做的是提供静态服务,该服务允许您集成哪些端点可用,并将其反馈给您的消费客户端.
哦,不要忘记连接虚拟路径提供程序:
HostingEnvironment.RegisterVirtualPathProvider(new ServiceVirtualPathProvider());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3007 次 |
最近记录: |