public class DefaultAsyncRunner : IAsyncRunner
{
public void Run<T>(Action<T> action)
{
Task.Factory.StartNew(delegate
{
using (var container = AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope())
{
var service = container.Resolve<T>();
action(service);
}
});
}
public class WCComunicator
{
readonly IPaisRepository paisRepository;
public WCComunicator(IPaisRepository paisRepository)
{
this.paisRepository = paisRepository;
}
public void DoSomething()
{
List<Pais> paises = this.paisRepository.GetAll().ToList();
}
}
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<DefaultCommandBus>().As<ICommandBus>().InstancePerHttpRequest();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
builder.RegisterType<DefaultAsyncRunner>().As<IAsyncRunner>();
builder.RegisterType<iSeguro.Web.Core.Common.WCComunicator>();
builder.RegisterAssemblyTypes(typeof(PaisRepository).Assembly).Where(t => t.Name.EndsWith("PaisRepository")).AsImplementedInterfaces().InstancePerHttpRequest();
var services = Assembly.Load("iSeguro.Domain");
builder.RegisterAssemblyTypes(services).AsClosedTypesOf(typeof(ICommandHandler<>)).InstancePerHttpRequest();
builder.RegisterAssemblyTypes(services).AsClosedTypesOf(typeof(IValidationHandler<>)).InstancePerHttpRequest();
builder.RegisterType<DefaultFormsAuthentication>().As<IFormsAuthentication>().InstancePerHttpRequest();
builder.RegisterFilterProvider();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
public class HomeController : Controller
{
readonly IAsyncRunner async;
public HomeController(IAsyncRunner async)
{
this.async = async;
}
public ActionResult Test()
{
async.Run<iSeguro.Web.Core.Common.WCComunicator>(c => c.DoSomething()); return RedirectToAction("Index");
}
Hi Fabricio,
I assume you found the post below on running background tasks from MVC actions.
http://aboutcode.net/2010/11/01/start-background-tasks-from-mvc-actions-using-autofac.html
The code you posted is missing the tag for the lifetime scope that is created from the root container. See the “httpRequest” string parameter passed to the BeginLifetimeScope method below. That tag is the same one that gets applied through the InstancePerHttpRequest registration methods. This is being done so that the service registrations can be reused.
public void Run<T>(Action<T> action)
{
Task.Factory.StartNew(delegate
{
using (var container = AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope("httpRequest"))
{
var service = container.Resolve<T>();
action(service);
}
});
}
Cheers,
Alex.
--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/FNDYHk03pFoJ.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.
To unsubscribe from this group, send email to autofac+unsubscribe@googlegroups.com.
Hello Alex,Seems like the API is not same in AutoFac 2.6.3.862. Can you please give the solution with latest API...Thanks,Sunny
To view this discussion on the web visit https://groups.google.com/d/msg/autofac/-/NeoW9HLwhsgJ.
I have tried many many ways, but none work, till your solution come.