|
namespace Samples |
|
{ |
|
using System; |
|
using System.ServiceModel; |
|
using System.ServiceModel.Channels; |
|
using System.ServiceModel.Description; |
|
using System.ServiceModel.Dispatcher; |
|
|
|
delegate T InstanceFactoryCallback<out T>(); |
|
|
|
class InstanceFactory<T> : IContractBehavior, IInstanceProvider, IEndpointBehavior |
|
{ |
|
readonly InstanceFactoryCallback<T> callback; |
|
|
|
public InstanceFactory(InstanceFactoryCallback<T> callback) |
|
{ |
|
this.callback = callback; |
|
} |
|
|
|
void IContractBehavior.Validate(ContractDescription contractDescription, ServiceEndpoint endpoint) |
|
{ |
|
} |
|
|
|
void IContractBehavior.ApplyDispatchBehavior( |
|
ContractDescription contractDescription, |
|
ServiceEndpoint endpoint, |
|
DispatchRuntime dispatchRuntime) |
|
{ |
|
dispatchRuntime.InstanceProvider = this; |
|
} |
|
|
|
void IContractBehavior.ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime) |
|
{ |
|
} |
|
|
|
void IContractBehavior.AddBindingParameters( |
|
ContractDescription contractDescription, |
|
ServiceEndpoint endpoint, |
|
BindingParameterCollection bindingParameters) |
|
{ |
|
} |
|
|
|
void IEndpointBehavior.Validate(ServiceEndpoint endpoint) |
|
{ |
|
} |
|
|
|
void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) |
|
{ |
|
} |
|
|
|
void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) |
|
{ |
|
endpointDispatcher.DispatchRuntime.InstanceProvider = this; |
|
} |
|
|
|
void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) |
|
{ |
|
} |
|
|
|
public object GetInstance(InstanceContext instanceContext) |
|
{ |
|
return this.callback(); |
|
} |
|
|
|
public object GetInstance(InstanceContext instanceContext, Message message) |
|
{ |
|
return this.callback(); |
|
} |
|
|
|
public void ReleaseInstance(InstanceContext instanceContext, object instance) |
|
{ |
|
var disposable = instance as IDisposable; |
|
if (disposable != null) |
|
{ |
|
disposable.Dispose(); |
|
} |
|
} |
|
} |
|
} |