Returns a service object given the specified InstanceContext object.
Function GetInstance ( _ instanceContext As InstanceContext, _ message As Message _ ) As Object
Dim instance As IInstanceProvider Dim instanceContext As InstanceContext Dim message As Message Dim returnValue As Object returnValue = instance.GetInstance(instanceContext, _ message)
Object GetInstance( InstanceContext instanceContext, Message message )
Object^ GetInstance( InstanceContext^ instanceContext, Message^ message )
function GetInstance( instanceContext : InstanceContext, message : Message ) : Object
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.
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 : 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.
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 }
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003