ServiceHost 构造函数

定义

初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class.

重载

ServiceHost()

初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class.

ServiceHost(Object, Uri[])

使用服务的实例及其指定的基址初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class with the instance of the service and its base addresses specified.

ServiceHost(Type, Uri[])

使用服务的类型及其指定的基址初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class with the type of service and its base addresses specified.

ServiceHost()

初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class.

protected:
 ServiceHost();
protected ServiceHost ();
Protected Sub New ()

注解

有两个构造函数可用于创建 ServiceHost 类的实例。There are two constructors used to create an instance of the ServiceHost class. 多数情况下,均使用将服务类型作为输入参数的 ServiceHost(Type, Uri[]) 构造函数。Use the ServiceHost(Type, Uri[]) constructor which takes the service type as an input parameter, most of the time. 根据需要,主机还可以使用此函数创建新服务。The host can use this to create new services as needed. 仅在您希望服务主机使用特定的服务单一实例时才使用 ServiceHost(Object, Uri[]) 构造函数。Use the ServiceHost(Object, Uri[]) constructor instead only when you want the service host to use a specific singleton instance of the service.

ServiceHost(Object, Uri[])

使用服务的实例及其指定的基址初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class with the instance of the service and its base addresses specified.

public:
 ServiceHost(System::Object ^ singletonInstance, ... cli::array <Uri ^> ^ baseAddresses);
public ServiceHost (object singletonInstance, params Uri[] baseAddresses);
new System.ServiceModel.ServiceHost : obj * Uri[] -> System.ServiceModel.ServiceHost
Public Sub New (singletonInstance As Object, ParamArray baseAddresses As Uri())

参数

singletonInstance
Object

承载的服务的实例。The instance of the hosted service.

baseAddresses
Uri[]

Array 类型的 Uri,包含承载服务的基址。An Array of type Uri that contains the base addresses for the hosted service.

异常

singletonInstancenullsingletonInstance is null.

示例

CalculatorService service = new CalculatorService();
ServiceHost serviceHost = new ServiceHost(service, baseAddress);
Dim service As CalculatorService = New CalculatorService()
Dim serviceHost As ServiceHost = New ServiceHost(service, baseAddress)

注解

如果要提供特定对象实例以供单一实例服务使用,请改用此构造函数来实现自定义的 System.ServiceModel.Dispatcher.IInstanceContextInitializerUse this constructor as an alternative to implementing a custom System.ServiceModel.Dispatcher.IInstanceContextInitializer when you want to provide a specific object instance for use by a singleton service. 当服务实现类型难以构造时(例如,如果该类型不实现无参数的默认公共构造函数),您可能希望使用此重载。You may want to use this overload when your service implementation type is difficult to construct (for example, if it does not implement a default public constructor that has no parameters).

请注意,当向此重载提供对象时,一些与 Windows Communication Foundation (WCF)实例化行为相关的功能的工作方式有所不同。Note that when an object is provided to this overload, some features related to the Windows Communication Foundation (WCF) instancing behavior work differently. 例如,当已知对象实例通过此构造函数重载提供时,调用 InstanceContext.ReleaseServiceInstance 不会产生任何影响。For example, calling InstanceContext.ReleaseServiceInstance have no effect when a well-known object instance is provided using this constructor overload. 同样,也将忽略所有其他实例释放机制。Similarly, any other instance release mechanism is ignored. ServiceHost 的行为总是像对于所有操作都将 OperationBehaviorAttribute.ReleaseInstanceMode 属性设置为 ReleaseInstanceMode.None 一样。The ServiceHost always behaves as if the OperationBehaviorAttribute.ReleaseInstanceMode property is set to ReleaseInstanceMode.None for all operations.

ServiceHost(Type, Uri[])

使用服务的类型及其指定的基址初始化 ServiceHost 类的新实例。Initializes a new instance of the ServiceHost class with the type of service and its base addresses specified.

public:
 ServiceHost(Type ^ serviceType, ... cli::array <Uri ^> ^ baseAddresses);
public ServiceHost (Type serviceType, params Uri[] baseAddresses);
new System.ServiceModel.ServiceHost : Type * Uri[] -> System.ServiceModel.ServiceHost
Public Sub New (serviceType As Type, ParamArray baseAddresses As Uri())

参数

serviceType
Type

承载服务的类型。The type of hosted service.

baseAddresses
Uri[]

Uri 类型的数组,包含承载服务的基址。An array of type Uri that contains the base addresses for the hosted service.

异常

serviceTypenullserviceType is null.

示例

此示例演示如何使用 ServiceHost 类承载 Windows Communication Foundation 服务:This sample illustrates how to use the ServiceHost class to host a Windows Communication Foundation service:

ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
Dim svcHost As ServiceHost = New ServiceHost(GetType(CalculatorService), baseAddress)

注解

当您具有服务类型并且可以根据需要(即使在需要单一实例时)创建其新实例时,请使用此构造函数。Use this constructor when you have the service type and you can create new instances of it when needed, even when you need a singleton instance. 仅在您希望服务主机使用特定的服务单一实例时才使用 ServiceHost(Object, Uri[]) 构造函数。Use the ServiceHost(Object, Uri[]) constructor instead only when you want the service host to use a specific singleton instance of the service.

适用于