ערוך

שתף באמצעות


ServiceHost Constructors

Definition

Initializes a new instance of the ServiceHost class.

Overloads

ServiceHost()

Initializes a new instance of the ServiceHost class.

ServiceHost(Object, Uri[])

Initializes a new instance of the ServiceHost class with the instance of the service and its base addresses specified.

ServiceHost(Type, Uri[])

Initializes a new instance of the ServiceHost class with the type of service and its base addresses specified.

ServiceHost()

Initializes a new instance of the ServiceHost class.

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

Remarks

There are two constructors used to create an instance of the ServiceHost class. 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. 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

ServiceHost(Object, Uri[])

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[]

An Array of type Uri that contains the base addresses for the hosted service.

Exceptions

singletonInstance 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

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

Note that when an object is provided to this overload, some features related to the Windows Communication Foundation (WCF) instancing behavior work differently. 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. The ServiceHost always behaves as if the OperationBehaviorAttribute.ReleaseInstanceMode property is set to ReleaseInstanceMode.None for all operations.

Applies to

ServiceHost(Type, Uri[])

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[]

An array of type Uri that contains the base addresses for the hosted service.

Exceptions

serviceType is null.

Examples

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. 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