ServiceHost Constructors

Definition

初始化 ServiceHost 類別的新執行個體。Initializes a new instance of the ServiceHost class.

Overloads

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 ()

Remarks

有兩個用來建立 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())

Parameters

singletonInstance
Object

裝載之服務的執行個體。The instance of the hosted service.

baseAddresses
Uri[]

型別為 ArrayUri,其中包含所裝載之服務的基底位址。An Array of type Uri that contains the base addresses for the hosted service.

Exceptions

singletonInstancenullsingletonInstance is null.

Examples

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

Remarks

當您想要提供特定物件實例供單一服務使用時,請使用此函式做為執行自訂 System.ServiceModel.Dispatcher.IInstanceContextInitializer 的替代方法。Use 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.NoneThe 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())

Parameters

serviceType
Type

裝載之服務的類型。The type of hosted service.

baseAddresses
Uri[]

型別為 Uri 的陣列,其中包含所裝載之服務的基底位址。An array of type Uri that contains the base addresses for the hosted service.

Exceptions

serviceTypenullserviceType is null.

Examples

這個範例說明如何使用 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)

Remarks

當您知道服務類型時,可以使用這個建構函式,在需要時建立新的執行個體 (即便您只是需要一個單一執行個體)。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.

Applies to