Using a custom endpoint with WCF and Autofac

I am trying to implement UoW as shown here: http://blog.iannelson.systems/wcf-global-exception-handling/

But I can’t understand for life how to connect it to Autofac. I don’t know where to start.

My WCF works fine with Autofac using http://autofac.readthedocs.org/en/latest/integration/wcf.html

But add or add IEndpointBehavior? I do not know...

If there is a better way to implement UoW, I'd love to hear.

Edit:

Now I just did:

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

Although I do not know if this is suitable for this, it seems to be a hack :(

Edit2:

It doesn't seem like you can run UoW in WCF: /

Edit 3:

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

+5
source share
1 answer

I found a solution to this problem, where a unit of work will only be executed if no errors are thrown.

Register unit of work as InstancePerLifetimeScope in Autofac

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

Then I created a combined EndpointBehavior and 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; } } 

Register 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> 
+2
source

Source: https://habr.com/ru/post/912294/


All Articles