当我在DI中注册Func时,HangFire不喜欢它

时间:2018-01-13 23:54:27

标签: c# asp.net asp.net-core hangfire

我有一个类需要将Func注入其构造函数中。该类称为SomeClassThatNeedsFunc:

// register a factory function to create a db context to be used in singleton classes
        services.AddTransient<Func<MyCustomContext>>(provider => (() =>
        {
            var dbContextOptionsBuilder = new DbContextOptionsBuilder<MyCustomContext>();
            dbContextOptionsBuilder.UseSqlServer(sqlConnectionString);
            return new MyCustomContext(dbContextOptionsBuilder.Options);
        }));
// register class that needs the factory:
        services.AddSingleton<SomeClassThatNeedsFunc>();

我得到例外:

warn: Hangfire.AutomaticRetryAttribute[0]
      Failed to process the job '4': an exception occurred. Retry attempt 3 of 10 will be performed in 00:01:55.
System.InvalidOperationException: Unable to resolve service for type 'System.Func`1[System.Threading.Tasks.VoidTaskResult]' while attempting to activate 'System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]'.
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
   at Hangfire.AspNetCore.AspNetCoreJobActivatorScope.Resolve(Type type)
   at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
   at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.<PerformJobWithFilters>b__0()
   at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
   at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_1.<PerformJobWithFilters>b__2()
   at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable`1 filters)
   at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
   at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId)

0 个答案:

没有答案