通过WCF和Autofac使用自定义端点行为 [英] Using a Custom Endpoint Behavior with WCF and Autofac

查看:94
本文介绍了通过WCF和Autofac使用自定义端点行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现如下所示的UoW: https://blog.iannelson.uk/wcf-global-exception-handling/

I'm trying to implement a UoW like shown here: https://blog.iannelson.uk/wcf-global-exception-handling/

但是我无法终生想出如何使用Autofac进行连接.我完全不知道从哪里开始.

But I can't for the life of me figure out how to wire it up with Autofac. I have absolutely no idea where to start.

我已经通过使用 http://使WCF与Autofac正常工作autofac.readthedocs.org/en/latest/integration/wcf.html

但是要注入或添加IEndpointBehavior?不知道...

But to inject or add the IEndpointBehavior? No idea...

如果有更好的方法来实现UoW,我想听听.

If there's a better way to implement a UoW I would like to hear.

到目前为止,我已经完成:

For now I've just done:

builder.RegisterType(typeof (UnitOfWork))
    .As(typeof (IUnitOfWork))
    .InstancePerLifetimeScope()
    .OnRelease(x =>
    {
        Trace.WriteLine("Comitted of UoW");
        ((IUnitOfWork) x).Commit();
        // OnRelease inhibits the default Autofac Auto-Dispose behavior so explicitly chain to it
        x.Dispose(); 
    });

尽管我不知道这是否可以接受,但似乎很简单:(

Though I don't know if this is an acceptable way of doing it, seems like a hack :(

似乎无法在WCF中运行UoW:/

Doesn't seem like it's possible to run a UoW in WCF :/

修改3:

我在这里发布了我的解决方案:

I've posted my solution here: http://www.philliphaydon.com/2011/11/06/unit-of-work-with-wcf-and-autofac/

推荐答案

我已经找到了解决此问题的方法,在该方法中,只有在未引发任何错误的情况下,才提交工作单元.

I have found a solution to this problem, where the unit of work only will be committed if no errors is thrown.

在Autofac中将工作单元注册为InstancePerLifetimeScope

Register the unit of work as InstancePerLifetimeScope in Autofac

    builder.RegisterType(typeof (UnitOfWork))
    .As(typeof (IUnitOfWork)).InstancePerLifetimeScope();

然后我创建了一个组合的EndpointBehavior和一个ErrorHandler.

Then i have created a combined EndpointBehavior and a ErrorHandler.

public class UnitOfWorkEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior
{
    public void Validate(ServiceEndpoint endpoint)
    {
    }

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

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        var unitOfWorkInstanceHandler = new UnitOfWorkInstanceHandler();

        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(unitOfWorkInstanceHandler);
        endpointDispatcher.DispatchRuntime.InstanceContextInitializers.Add(unitOfWorkInstanceHandler);
    }

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

    protected override object CreateBehavior()
    {
        return new UnitOfWorkEndpointBehavior();
    }

    public override Type BehaviorType
    {
        get { return typeof (UnitOfWorkEndpointBehavior); }
    }
}



public class UnitOfWorkInstanceHandler : IInstanceContextInitializer, IErrorHandler
{
    private bool _doCommit = true;

    public void Initialize(InstanceContext instanceContext, Message message)
    {
        instanceContext.Closing += CommitUnitOfWork;
    }

    void CommitUnitOfWork(object sender, EventArgs e)
    {
        //Only commit if no error has occured
        if (_doCommit)
        {
            //Resolve the UnitOfWork form scope in Autofac
            OperationContext.Current.InstanceContext.Extensions.Find<AutofacInstanceContext>().Resolve<IUnitOfWork>().Commit();
        }
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        _doCommit = false;
    }

    public bool HandleError(Exception error)
    {
        _doCommit = false;
        return false;
    }
}

在web.config中注册端点行为

The registration of the Endpoint Behavior in web.config

<system.serviceModel>
    ...
    <extensions>
      <behaviorExtensions>
        <add name="UnitOfWork" type="Namespace.UnitOfWorkBehavior, Namespace"/>
      </behaviorExtensions>
    </extensions>
      <behaviors>
        <endpointBehaviors>
          <behavior name="">
            <UnitOfWork/>
          </behavior>
        </endpointBehaviors>
    ...
    </behaviors>
    ...
</system.serviceModel>

这篇关于通过WCF和Autofac使用自定义端点行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆