InstanceContext.GetServiceInstance InstanceContext.GetServiceInstance InstanceContext.GetServiceInstance InstanceContext.GetServiceInstance Method

Definition

Returns the instance of the service.

Overloads

GetServiceInstance() GetServiceInstance() GetServiceInstance() GetServiceInstance()

Returns the instance of the service for the instance context.

GetServiceInstance(Message) GetServiceInstance(Message) GetServiceInstance(Message)

Returns the instance of the service for the instance context in response to an incoming message.

GetServiceInstance() GetServiceInstance() GetServiceInstance() GetServiceInstance()

Returns the instance of the service for the instance context.

public:
 System::Object ^ GetServiceInstance();
[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
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 has been closed already 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();
}

GetServiceInstance(Message) GetServiceInstance(Message) 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
Parameters
message
Message Message 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 has been closed already 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