Export (0) Print
Expand All

IInstanceProvider Interface

Declares methods that provide a service object or recycle a service object for a Windows Communication Foundation (WCF) service.

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

type IInstanceProvider =  interface end

The IInstanceProvider type exposes the following members.

  NameDescription
Public methodGetInstance(InstanceContext)Returns a service object given the specified InstanceContext object.
Public methodGetInstance(InstanceContext, Message)Returns a service object given the specified InstanceContext object.
Public methodReleaseInstanceCalled when an InstanceContext object recycles a service object.
Top

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, WCF creates a new instance using the GetInstance method.

NoteNote

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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft