Edit

Share via


InstanceContext.GetServiceInstance Method

Definition

Returns the instance of the service.

Overloads

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

Returns

The object that represents the service instance.

Exceptions

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.

Examples

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

Applies to

GetServiceInstance(Message)

Source:
InstanceContext.cs
Source:
InstanceContext.cs

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

Parameters

message
Message

The incoming message that triggered the creation of a service object.

Returns

The object that represents the service instance.

Exceptions

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.

Examples

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);
}

Remarks

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.

Applies to