Asp.net 网络 api 使用 autofac 和 Hangfire
Asp.net web api with autofac and Hangfire
我最近升级到新版本的 Hangfire,我正在努力尝试使用 autofac 和 Hangfire 设置我的网络api。我正在使用 Autofac Hangfire 集成版本 1.1 和 Hangfire 1.4.2。我正在使用 Owin 进行托管。我不断收到以下错误:
The requested service 'IFoo' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
这是我的owin启动配置。我所有的注册都是在 AutofacStandardModule class
public class Startup
{
public void Configuration(IAppBuilder app)
{
//we will have the firewall block all CE endpoints from the outside instead
//ConfigureOAuthTokenConsumption(app);
var storage = new SqlServerStorage("connection string");
JobStorage.Current = storage;
app.UseHangfireServer(new BackgroundJobServerOptions(),storage);
app.UseHangfireDashboard("/Hangfire",new DashboardOptions(),storage);
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacStandardModule());
var container = builder.Build();
GlobalConfiguration.Configuration.UseAutofacActivator(container);
}
}
此外,这是我的网站 api 配置 class。我也不知道我应该如何在这里配置 Hangfire..
public static class WebApiConfig
{
public static void Register(HttpConfiguration config, Autofac.Module moduleToAppend)
{
config.MapHttpAttributeRoutes();
config.EnableCors();
config.EnableSystemDiagnosticsTracing();
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(
Assembly.GetExecutingAssembly())
.Where(t =>
!t.IsAbstract && typeof(ApiController).IsAssignableFrom(t))
.InstancePerLifetimeScope();
builder.RegisterModule(
new AutofacStandardModule());
if (moduleToAppend != null)
{
builder.RegisterModule(moduleToAppend);
}
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(
container);
//Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
//JobActivator.Current = new AutofacJobActivator(container);
}
}
我解决了,看来我入队的时候没说清楚我的工作是什么类型
所做的就是改变
_jobClient.Enqueue(
() => _foo.Bar(fooId, fooId2));
..进入..
_jobClient.Enqueue<IFoo>(x => x.Bar(fooId, fooId2));
我最近升级到新版本的 Hangfire,我正在努力尝试使用 autofac 和 Hangfire 设置我的网络api。我正在使用 Autofac Hangfire 集成版本 1.1 和 Hangfire 1.4.2。我正在使用 Owin 进行托管。我不断收到以下错误:
The requested service 'IFoo' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
这是我的owin启动配置。我所有的注册都是在 AutofacStandardModule class
public class Startup
{
public void Configuration(IAppBuilder app)
{
//we will have the firewall block all CE endpoints from the outside instead
//ConfigureOAuthTokenConsumption(app);
var storage = new SqlServerStorage("connection string");
JobStorage.Current = storage;
app.UseHangfireServer(new BackgroundJobServerOptions(),storage);
app.UseHangfireDashboard("/Hangfire",new DashboardOptions(),storage);
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacStandardModule());
var container = builder.Build();
GlobalConfiguration.Configuration.UseAutofacActivator(container);
}
}
此外,这是我的网站 api 配置 class。我也不知道我应该如何在这里配置 Hangfire..
public static class WebApiConfig
{
public static void Register(HttpConfiguration config, Autofac.Module moduleToAppend)
{
config.MapHttpAttributeRoutes();
config.EnableCors();
config.EnableSystemDiagnosticsTracing();
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(
Assembly.GetExecutingAssembly())
.Where(t =>
!t.IsAbstract && typeof(ApiController).IsAssignableFrom(t))
.InstancePerLifetimeScope();
builder.RegisterModule(
new AutofacStandardModule());
if (moduleToAppend != null)
{
builder.RegisterModule(moduleToAppend);
}
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(
container);
//Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
//JobActivator.Current = new AutofacJobActivator(container);
}
}
我解决了,看来我入队的时候没说清楚我的工作是什么类型
所做的就是改变
_jobClient.Enqueue(
() => _foo.Bar(fooId, fooId2));
..进入..
_jobClient.Enqueue<IFoo>(x => x.Bar(fooId, fooId2));