IInstanceProvider.GetInstance Method (InstanceContext)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns a service object given the specified InstanceContext object.

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

Function GetInstance (
	instanceContext As InstanceContext
) As Object

Parameters

instanceContext
Type: System.ServiceModel.InstanceContext

The current InstanceContext object.

Return Value

Type: System.Object

A user-defined service object.

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

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.

 Public Class ObjectProviderBehavior
  Implements IInstanceProvider

Private message As String
Private service As SampleService = Nothing

Public Sub New(ByVal msg As String)
  Console.WriteLine("The non-default constructor has been called.")
  Me.message = msg
  Me.service = New SampleService("Singleton sample service.")
End Sub

#Region "IInstanceProvider Members"

Public Function GetInstance(ByVal instanceContext As InstanceContext, ByVal message As System.ServiceModel.Channels.Message) As Object Implements IInstanceProvider.GetInstance
  Console.WriteLine("GetInstance is called:")
  Return Me.service
End Function

Public Function GetInstance(ByVal instanceContext As InstanceContext) As Object Implements IInstanceProvider.GetInstance
  Console.WriteLine("GetInstance is called:")
  Return Me.service
End Function

Public Sub ReleaseInstance(ByVal instanceContext As InstanceContext, ByVal instance As Object) Implements IInstanceProvider.ReleaseInstance
  Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.")
End Sub

#End Region
 End Class

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.

 Public Class SingletonBehaviorAttribute
  Inherits Attribute
  Implements IContractBehaviorAttribute, IContractBehavior

#Region "IContractBehaviorAttribute Members"

Public ReadOnly Property TargetContract() As Type Implements IContractBehaviorAttribute.TargetContract
  Get
	  Return GetType(ISampleService)
  End Get
End Property

#End Region

#Region "IContractBehavior Members"

Public Sub AddBindingParameters(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IContractBehavior.AddBindingParameters
  Return
End Sub

Public Sub ApplyClientBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) Implements IContractBehavior.ApplyClientBehavior
  Return
End Sub

Public Sub ApplyDispatchBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal dispatch As DispatchRuntime) Implements IContractBehavior.ApplyDispatchBehavior
  dispatch.InstanceProvider = New ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.")
End Sub

Public Sub Validate(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint) Implements IContractBehavior.Validate
  Return
End Sub

#End Region
 End Class

.NET Framework
Available since 3.0
Return to top
Show: