1

I am trying to use the Rebus transaction context within my message handler. I have read the documentation here and have seen the sample here, however I do not know how Windsor works.

Could somebody make an example of using ITransactionContext with EF without any IOC container just to understand the way it works?

Thanks

1 Answer 1

1

I can recommend you take a look at the Rebus.UnitOfWork package because it provides a slightly higher level API for implementing a custom unit of work – either with or without an IoC container.

With Rebus.UnitOfWork you can do this:

Configure.With(...)
    .(...)
    .Options(o => {
        o.EnableUnitOfWork(Create, Commit, RollBack, Cleanup);
    })
    .Start();

//....

static MyCustomUnitOfWork Create() => new MyCustomUnitOfWork();

static void Commit(MyCustomUnitOfWork uow) => uow.Commit();

static void RollBack(MyCustomUnitOfWork uow) => uow.RollBack();

static void Cleanup(MyCustomUnitOfWork uow) => uow.Dispose();

where MyCustomUnitOfWork can then be whatever you want, e.g. a class that creates an EF database context and calls SaveChanges and whatnot on it.

You can read more on the wiki page about Unit Of Work, or go directly to the sample that demonstrates Rebus.UnitOfWork with running code.

Sign up to request clarification or add additional context in comments.

2 Comments

Very clear thank you. The only problem I have right now is that my EF database context is already registered differently (for instance not with a factory method as the suggested sample does) and I dont know how to use a different registration for the message handler in particular. Any suggestion?
My suggestion is always to use a separate IoC container for each "logical application" you have in your process. E.g. if you are hosting a web application and a Rebus-based backend, you would have two separate "logical applications", and therefore you should have two IoC containers. This way, you can use lifestyles that target each particular usage scenario (e.g. bound to web requests in the web app, use factory method in Rebus backend) instead of bending it out of shape to try to make it work in both scenarios.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.