簡體   English   中英

在 WCF 和 Autofac 中使用自定義端點行為

[英]Using a Custom Endpoint Behavior with WCF and Autofac

我正在嘗試實現如下所示的 UoW: https ://blog.iannelson.uk/wcf-global-exception-handling/

但是我一輩子都無法弄清楚如何將它與 Autofac 連接起來。 我完全不知道從哪里開始。

通過使用http://autofac.readthedocs.org/en/latest/integration/wcf.html,我已經讓 WCF 與 Autofac 一起正常工作

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

如果有更好的方法來實施 UoW,我想聽聽。

編輯:

現在我剛剛完成:

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(); 
    });

雖然我不知道這是否是一種可以接受的方式,但似乎是一種黑客:(

編輯2:

似乎不可能在 WCF 中運行 UoW:/

編輯3:

我在這里發布了我的解決方案: http : //www.philliphaydon.com/2011/11/06/unit-of-work-with-wcf-and-autofac/

我找到了這個問題的解決方案,只有在沒有拋出錯誤的情況下才會提交工作單元。

在 Autofac 中將工作單元注冊為 InstancePerLifetimeScope

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

然后我創建了一個組合的 EndpointBehavior 和一個 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 中注冊 Endpoint Behavior

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM