Hangfire 1.6.4 .NET Core - 无法解决依赖关系

时间:2016-09-06 20:39:05

标签: .net asp.net-core-mvc hangfire

我在我的.NET Core项目中使用Hangfire 1.6.4。

这是我的日程控制器:

public class ContainerJobActivator : JobActivator
{
    private readonly IServiceProvider _serviceProvider;

    public ContainerJobActivator(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public override object ActivateJob(Type type)
    {
        return _serviceProvider.GetService(type);
    }
}

但是我在执行作业时遇到错误:

GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(services.BuildServiceProvider()));

System.InvalidOperationException

类型' MyProject.Services.Email.EmailScheduler'没有服务已经注册。

我已经注册了ContainerJobActivator:

 var client = new WCClient();  
 using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
 {
     var httpRequestProperty = new HttpRequestMessageProperty();
     httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic " +
                  Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" +
                  client.ClientCredentials.UserName.Password));
     OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

     client.DoSomething();
 }

在Startup课程中:

{{1}}

怎么了?

谢谢!

1 个答案:

答案 0 :(得分:4)

这对我有帮助: 1.像这样注册Hangfire:

services.AddHangfire(configuration => configuration
                       .UseSqlServerStorage("connection string here"));

2。还有一件事(应该是第一件事):

services.AddScoped<EmailScheduler, EmailScheduler>();

我暂时没有将这个答案标记为正确的答案...... 但如果不是另一个答案,我会这样做)