gpt4 book ai didi

wcf - 如何将参数传递给 ServiceHost

转载 作者:行者123 更新时间:2023-12-03 17:44:13 30 4
gpt4 key购买 nike

我正在做以下事情

 //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?但无法理解它。谢谢

最佳答案

如果我理解正确,您希望将参数传递给服务实现类的构造函数。您可以通过 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);

注意 - 使用这种方法意味着您正在使用服务类的单例实例。如果每个 session 或每个请求需要一个新实例,请考虑使用 ServiceHostFactorythis answer 中所述.

关于wcf - 如何将参数传递给 ServiceHost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14206267/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com