IInstanceProvider Interface
Declares methods that provide a service object or recycle a service object for a service.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
The IInstanceProvider type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | GetInstance(InstanceContext) | Returns a service object given the specified InstanceContext object. |
![]() | GetInstance(InstanceContext, Message) | Returns a service object given the specified InstanceContext object. |
![]() | ReleaseInstance | Called when an InstanceContext object recycles a service object. |
Implement the IInstanceProvider interface to control the creation and recycling of service objects when one is requested or disposed by an InstanceContext object.
Once the IInstanceProvider interface is implemented, you must assign your custom instance provider object to the InstanceProvider property using either an endpoint behavior (a IEndpointBehavior object) or a contract behavior (a IContractBehavior object).
If the insertion mechanism is an endpoint behavior you can also implement a BehaviorExtensionElement object that can insert your custom behavior using a configuration file. If the insertion mechanism is a contract behavior, you can insert the behavior programmatically prior to the opening of the service host or you can implement a custom attribute. (For an example of the contract behavior approach, see the Example section.)
IInstanceProvider has two methods, GetInstance and ReleaseInstance. These methods are typically implemented to create service objects using a non-default constructor or to initialize or dispose of some state related to the lifetime of the object. Service object pooling is one example of custom IInstanceProvider functionality.
Typically, the InstanceContext invokes the GetInstance when the InstanceContext is first created and invokes the ReleaseInstance method when the InstanceContext is closed.
There are two ways to cause an InstanceContext object to release a service object before the InstanceContext is closed. The first method is to set the ReleaseInstanceMode to AfterCall or BeforeAndAfterCall. The second method is to call the ReleaseServiceInstance method. If this is done, the InstanceContext calls the ReleaseInstance method on the dispatcher's instance provider. If a new message arrives after the instance has been released, creates a new instance using the GetInstance method.
Note |
|---|
If the InstanceContextMode of the service is Single, the system does not call the GetInstance or ReleaseInstance methods even when the user did not provide a well-known service object unless the user directly calls ReleaseServiceInstance and then calls GetServiceInstance. |
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
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Note