我的应用程序作为Windows服务运行。它使用以下帮助器方法动态创建WCF服务:
public static void StartWebService(string webServiceName, Type serviceContractType, Type serviceImplementationType)
{
if (string.IsNullOrEmpty(webServiceName)) return;
var baseAddress = GetWebServiceAddress(webServiceName);
var baseUri = new Uri(baseAddress);
lock (RunningWebServices)
{
if (RunningWebServices.ContainsKey(webServiceName))
return;
var webServiceHost = new ServiceHost(serviceImplementationType, baseUri);
var serviceBehaviour = new ServiceMetadataBehavior() { HttpGetEnabled = true };
webServiceHost.Description.Behaviors.Add(serviceBehaviour);
webServiceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
var httpBinding = new BasicHttpBinding();
webServiceHost.AddServiceEndpoint(serviceContractType, httpBinding, baseAddress);
webServiceHost.Open();
RunningWebServices.Add(webServiceName, webServiceHost);
}
}
这些服务没有.svc文件。还要清楚的是,这些是自托管服务,不是在iis下运行的,而是在was(windows激活服务)下运行的。
现在我想控制实现类是如何实例化的。听起来这是可以做到的。但是,我找到的所有关于这方面的文章都说,指定要使用的工厂的方法是将类型名放在.svc文件的@servicehost指令中。我没有这个文件!
在was下没有运行svc文件的情况下,有没有办法为wcf服务使用我自己的servicehost工厂?
更新以下答案
这是我的新助手方法,包含了Carlos的解
public static void StartWebService(string webServiceName, Type serviceContractType, Type serviceImplementationType)
{
if (string.IsNullOrEmpty(webServiceName)) return;
var baseAddress = GetWebServiceAddress(webServiceName);
var baseUri = new Uri(baseAddress);
lock (RunningWebServices)
{
if (RunningWebServices.ContainsKey(webServiceName))
return;
var webServiceHost = new ServiceHost(serviceImplementationType, baseUri);
webServiceHost.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true });
webServiceHost.Description.Behaviors.Add(new CustomWebServiceBehavior());
webServiceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
webServiceHost.AddServiceEndpoint(serviceContractType, new BasicHttpBinding(), baseAddress);
webServiceHost.Open();
RunningWebServices.Add(webServiceName, webServiceHost);
}
}
internal class CustomWebServiceBehavior : IServiceBehavior, IInstanceProvider
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHost)
{
foreach (ChannelDispatcher cd in serviceHost.ChannelDispatchers)
{
foreach (EndpointDispatcher ed in cd.Endpoints)
{
ed.DispatchRuntime.InstanceProvider = this;
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return this.GetInstance(instanceContext);
}
public object GetInstance(InstanceContext instanceContext)
{
return // this is where I use my factory;
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
}
最佳答案
您不需要服务宿主工厂来控制服务实现类的实例化方式—您需要的是通过使用自定义服务行为将IInstanceProvider
添加到调度运行时。在http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx上的博客文章有很多关于如何实现这一点的细节。例如,下面的SSCCE显示了如何使用实例提供程序的示例(对于在wcf中没有默认构造函数的服务,因此在没有实例提供程序的情况下不能直接使用它)
public class StackOverflow_29825519
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string WhoAmI();
}
public class Service : ITest
{
string name;
public Service(string name)
{
this.name = name;
}
public string WhoAmI()
{
return this.name;
}
}
class MyBehavior : IServiceBehavior, IInstanceProvider
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher ed in cd.Endpoints)
{
ed.DispatchRuntime.InstanceProvider = this;
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
public object GetInstance(InstanceContext instanceContext, Message message)
{
return this.GetInstance(instanceContext);
}
public object GetInstance(InstanceContext instanceContext)
{
return new Service("John Doe");
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Description.Behaviors.Add(new MyBehavior());
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
ITest proxy = factory.CreateChannel();
Console.WriteLine("WhoAmI: {0}", proxy.WhoAmI());
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
关于c# - 如何为没有SVC文件的自托管WCF服务指定ServiceHostFactory,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29825519/