InstanceContext.GetServiceInstance 方法

定义

返回服务实例。Returns the instance of the service.

重载

GetServiceInstance()

为实例上下文返回服务的实例。Returns the instance of the service for the instance context.

GetServiceInstance(Message)

为实例上下文返回服务的实例,以响应传入消息。Returns the instance of the service for the instance context in response to an incoming message.

GetServiceInstance()

为实例上下文返回服务的实例。Returns the instance of the service for the instance context.

public:
 System::Object ^ GetServiceInstance();
public object GetServiceInstance ();
member this.GetServiceInstance : unit -> obj
Public Function GetServiceInstance () As Object

返回

Object

表示服务实例的对象。The object that represents the service instance.

例外

该服务实例处于已创建或正在打开的状态,或没有初始化。The service instance is in a created or opening state or is not initialized.

该服务实例已中止。The service instance is aborted.

该服务实例已关闭,并且在这些状态下无法修改。The service instance has been closed already and cannot be modified in these states.

该服务实例处于错误状态,并且在这些状态下无法修改。The service instance is faulted and cannot be modified in these states.

示例

Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/service");

// Create a ServiceHost for the CalculatorService type and provide the base address.
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
{
    serviceHost.Open();
    OperationContext operationContext = OperationContext.Current;
    InstanceContext instanceContext = operationContext.InstanceContext;
    CalculatorService service = (CalculatorService) instanceContext.GetServiceInstance();
}

适用于

GetServiceInstance(Message)

为实例上下文返回服务的实例,以响应传入消息。Returns the instance of the service for the instance context in response to an incoming message.

public:
 System::Object ^ GetServiceInstance(System::ServiceModel::Channels::Message ^ message);
public object GetServiceInstance (System.ServiceModel.Channels.Message message);
member this.GetServiceInstance : System.ServiceModel.Channels.Message -> obj
Public Function GetServiceInstance (message As Message) As Object

参数

message
Message

触发服务对象的创建的传入消息。The incoming message that triggered the creation of a service object.

返回

Object

表示服务实例的对象。The object that represents the service instance.

例外

该服务实例处于已创建或正在打开的状态,或没有初始化。The service instance is in a created or opening state or is not initialized.

该服务实例已中止。The service instance is aborted.

该服务实例已关闭,并且在这些状态下无法修改。The service instance has been closed already and cannot be modified in these states.

该服务实例处于错误状态,并且在这些状态下无法修改。The service instance is faulted and cannot be modified in these states.

示例

Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/service");

// Create a ServiceHost for the CalculatorService type and provide the base address.
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress))
{
    serviceHost.Open();
    OperationContext operationContext = OperationContext.Current;
    InstanceContext instanceContext = operationContext.InstanceContext;
    CalculatorService service = (CalculatorService)instanceContext.GetServiceInstance(msg);
}

注解

一般情况下,您只能在消息到达后被定向时,通过调用 Open 创建服务实例;如果没有消息到达,则不创建 InstanceContext 和服务对象,因此直到实际需要时才会部署资源。The general idea is that you only create an instance of the service when a message directed at it arrives, by calling Open; then if no message arrives, no InstanceContext is created and no service object is created, and so resources are not deployed until actually required.

适用于