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.

適用於