当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Providing a factory instead of a Type in WCF ServiceHost

问题描述:

Is there any way to extend ServiceHost so that it accepts a factory?

I would like to use dynamic service proxies for some preprocessing of WCF calls without touching the service implementation.

EDIT

Now the ServiceHost creation looks like

var host = new ServiceHost(typeof(MyService));

and i would like to use something like

var factory = new ServiceProxyFactory<MyService>();

var host = new MyServiceHost(typeof(MyService), factory);

网友答案:

What you describe is not really possible with .NET...

You can implement a custom ServiceHost which allows you to customize the behaviour of the ServiceHost and/or the IDispatchMessageInspector which allows you inspect/modify any message inbound and outbound... IF you really wnat to implement some sort of "dynamic routing" then there is always the Routing capability of WCF...

Other options (though no 100% solution):

  • WCF service returning another service (service factory?)
  • WCF and factory design pattern
  • http://msdn.microsoft.com/en-us/library/aa702697.aspx

EDIT - I stand corrected:

It is not really possible to use a usual Factory BUT WCF provides the possibility to plug in an IInstanceProvider either at the Service or the Endpoint level...

For further reference see the following links

  • http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx
  • http://msdn.microsoft.com/library/system.servicemodel.dispatcher.iinstanceprovider.aspx
  • How do I pass values to the constructor on my wcf service?
  • http://geekswithblogs.net/13DaysaWeek/archive/2010/12/01/dependency-injection-and-wcf-services.aspx
  • http://www.eggheadcafe.com/tutorials/aspnet/b428fb65-08b4-45c8-97cd-47ee1a1eaf41/composing-wcf-applications.aspx
网友答案:

The interface i was looking for is IInstanceProvider.

public class MyServiceInstanceProvider<TService> : IInstanceProvider where TService : new()
{

    public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
    {
        return ServiceFactory.Create<TService>();
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return ServiceFactory.Create<TService>();
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        return;
    }
}

public class MyEndpointBehavior<TService> : IEndpointBehavior where TService : new()
{

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
        return;
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        return;
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.InstanceProvider = new MyServiceInstanceProvider<TService>();
    }

    public void Validate(ServiceEndpoint endpoint)
    {
        return;
    }
}

public class MyServiceHost<TService> : ServiceHost where TService : new()
{
    public MyServiceHost(params Uri[] baseAddresses)
        :base(typeof(TService), baseAddresses)
    {            
    }

    public override System.Collections.ObjectModel.ReadOnlyCollection<ServiceEndpoint> AddDefaultEndpoints()
    {
        var endpoints = base.AddDefaultEndpoints();

        foreach (var endpoint in endpoints)
        {
            endpoint.Behaviors.Add(new MyEndpointBehavior<TService>());
        }

        return endpoints;
    }

    public override void AddServiceEndpoint(ServiceEndpoint endpoint)
    {
        base.AddServiceEndpoint(endpoint);
        endpoint.Behaviors.Add(new MyEndpointBehavior<TService>());
    }

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: