我正在做以下
//Define the service host
this._smeediPluginServiceHost = new ServiceHost(typeof(SmeediServiceHost), smeediServiceUri);
this._smeediPluginServiceHost.AddServiceEndpoint(typeof(ISmeediServiceHost), GetBinding(), smeediServiceUri);
SetupAndStartWebService(_smeediPluginServiceHost);
private void SetupAndStartWebService(ServiceHost serviceHost, ServiceDiscoveryBehavior serviceDiscoveryBehavior = null)
{
//Define service behaviours
ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
serviceMetadataBehavior.HttpGetEnabled = true;
//Add the behaviours to the service
serviceHost.Description.Behaviors.Add(serviceMetadataBehavior);
if (serviceDiscoveryBehavior != null)
serviceHost.Description.Behaviors.Add(serviceDiscoveryBehavior);
serviceHost.Open();
}
我需要将一个参数传递给服务,我无法弄清楚如何。我看了How do I pass values to the constructor on my wcf service?,但无法理解它。感谢
答案 0 :(得分:20)
如果我理解正确,您希望将参数传递给服务实现类的构造函数。你可以通过passing an instance of the service class to the ServiceHost
constructor来代替它的类型。那就是:
// Create the service instance
var instance = new SmeediServiceHost("some parameters");
// Define the service host using the above instance
this._smeediPluginServiceHost = new ServiceHost(instance, smeediServiceUri);
警告 - 使用此方法意味着您正在使用服务类的单例实例。如果您需要为每个会话或每个请求添加一个新实例,请考虑使用this answer中所述的ServiceHostFactory
。