Skip to content

Instantly share code, notes, and snippets.

@CraigStuntz
Created November 8, 2011 13:53
Show Gist options
  • Save CraigStuntz/1347782 to your computer and use it in GitHub Desktop.
Save CraigStuntz/1347782 to your computer and use it in GitHub Desktop.
Possible means of using Autofac with MassTransit
public static void main(string[] args)
{
var builder = new ContainerBuilder();
// register each consumer manually
builder.RegisterType<YourConsumer>().As<IConsumer>();
//or use Autofac's scanning capabilities -- SomeClass is any class in the correct assembly
builder.RegisterAssemblyTypes(typeof(SomeClass).Assembly).As<IConsumer>();
//now we add the bus
builder.Register(c => ServiceBusFactory.New(sbc =>
{
//other configuration options
//this will find all of the consumers in the container and
//register them with the bus.
sbc.LoadFrom(c);
})).As<IServiceBus>()
.SingleInstance();
var container = builder.Build();
}
// Note: We recommend that most of this code be placed in an Autofac Module
// ( http://code.google.com/p/autofac/wiki/StructuringWithModules )
@drusellers
Copy link

what all is line 9 doing?

@CraigStuntz
Copy link
Author

"Find every type in the assembly which contains SomeClass that happens to support the IConsumer interface and register it with the container such that when we are later ask for IConsumer implementations, we'll get those types."

The thing which is confusing, at first glance, is typeof(SomeClass).Assembly. Autofac doesn't have .FromThisAssembly() like Windsor (as far as I know), so you just need an assembly reference for the relevant assembly. Pick any class in that assembly, and use the Type.Assembly property to get that reference.

@CraigStuntz
Copy link
Author

One could also use Assembly.GetExecutingAssembly.

@drusellers
Copy link

Ok, that's what I want. Thanks. Its now in the docs. :)

@drusellers
Copy link

Yeah, but that's less explicit for most users. Keep it concrete, keep it clear. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment