.NET Framework Class Library
IInstanceProvider..::.GetInstance Method (InstanceContext, Message)

Returns a service object given the specified InstanceContext object.

Namespace:  System.ServiceModel.Dispatcher
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
Syntax

Visual Basic (Declaration)
Function GetInstance ( _
    instanceContext As InstanceContext, _
    message As Message _
) As Object
Visual Basic (Usage)
Dim instance As IInstanceProvider
Dim instanceContext As InstanceContext
Dim message As Message
Dim returnValue As Object

returnValue = instance.GetInstance(instanceContext, _
    message)
C#
Object GetInstance(
    InstanceContext instanceContext,
    Message message
)
Visual C++
Object^ GetInstance(
    InstanceContext^ instanceContext, 
    Message^ message
)
JScript
function GetInstance(
    instanceContext : InstanceContext, 
    message : Message
) : Object

Parameters

instanceContext
Type: System.ServiceModel..::.InstanceContext
The current InstanceContext object.
message
Type: System.ServiceModel.Channels..::.Message
The message that triggered the creation of a service object.

Return Value

Type: System..::.Object
The service object.
Remarks

Use the GetInstance(InstanceContext, Message) method to control the exact service object that a WCF service receives when it attempts to create a new one.

Examples

The following code example shows how to implement IInstanceProvider that provides "singleton" behavior; it always returns the same service instance and does not recycle it.

C#
public class ObjectProviderBehavior : IInstanceProvider
{

  string message;
  SampleService service = null;

  public ObjectProviderBehavior(string msg)
  {
    Console.WriteLine("The non-default constructor has been called.");
    this.message = msg;
    this.service = new SampleService("Singleton sample service.");
  }

  #region IInstanceProvider Members

  public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
  {
    Console.WriteLine("GetInstance is called:");
    return this.service;
  }

  public object GetInstance(InstanceContext instanceContext)
  {
    Console.WriteLine("GetInstance is called:");
    return this.service;
  }

  public void ReleaseInstance(InstanceContext instanceContext, object instance)
  {
    Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.");
  }

  #endregion
}

The following code example shows how to implement a custom attribute that implements IContractBehavior to insert the custom service instance provider. It also implements IContractBehaviorAttribute, which binds its use to a specific contract.

C#
public class SingletonBehaviorAttribute : Attribute, IContractBehaviorAttribute, IContractBehavior
{

  #region IContractBehaviorAttribute Members

  public Type TargetContract
  {
    get { return typeof(ISampleService); }
  }

  #endregion

  #region IContractBehavior Members

  public void AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection parameters)
  {
    return;
  }

  public void ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  {
    return;
  }

  public void ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch)
  {
    dispatch.InstanceProvider = new ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.");
  }

  public void Validate(ContractDescription description, ServiceEndpoint endpoint)
  {
    return;
  }

  #endregion
}
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Tags :


Community Content

j_saremi
What is the difference between the 2 GetInstance() methods?
When would the one without Message gets called? If one throws NotImplementedException would the other get called?
Tags :

Page view tracker