一、配置环境
.NET Framework 4.6.1 ASP.NET WEBAPI
<package id="Autofac" version="5.2.0" targetFramework="net461" /> <package id="Autofac.Mvc5" version="5.0.0" targetFramework="net461" /> <package id="Autofac.WebApi2" version="5.0.0" targetFramework="net461" /> <package id="Hangfire.AspNet" version="0.2.0" targetFramework="net461" /> <package id="Hangfire.Autofac" version="2.3.1" targetFramework="net461" /> <package id="Hangfire.Core" version="1.7.33" targetFramework="net461" /> <package id="Hangfire.InMemory" version="0.3.6" targetFramework="net461" />
二、Autofac配置
public class AutofacInitializer { /// <summary> /// Autofac容器对象 /// </summary> private static IContainer _container; /// <summary> /// 初始化autofac /// </summary> public static void Init() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().AsSelf(); builder.RegisterType<WechatTenpayCertificateManagerFactory>().As<IWechatTenpayCertificateManagerFactory>().SingleInstance(); builder.RegisterType<WechatTenpayClientFactory>().As<IWechatTenpayClientFactory>().SingleInstance(); builder.RegisterType<TenpayCertificateRefreshingJob>().AsSelf().InstancePerBackgroundJob(); _container = builder.Build(); //设置MVC依赖注入 DependencyResolver.SetResolver(new AutofacDependencyResolver(_container)); //设置WebApi依赖注入 System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(_container); //设置Hangfire依赖注入 Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(_container); } /// <summary> /// 从Autofac容器获取对象 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T Resolve<T>() { return _container.Resolve<T>(); } }
三、Hangfire配置
public class HangfireInitializer { public static BackgroundJobServer Init() { GlobalConfiguration.Configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UseInMemoryStorage(); var server = new BackgroundJobServer(); HangfireAspNet.Use(() => new BackgroundJobServer[] { server }); BackgroundJob.Enqueue<Services.Hangfire.TenpayCertificateRefreshingJob>(job => job.ExecuteAsync()); RecurringJob.AddOrUpdate<Services.Hangfire.TenpayCertificateRefreshingJob>(job => job.ExecuteAsync(), Cron.Daily()); return server; } }
四、注册Autofac、Hangfire
public class WebApiApplication : System.Web.HttpApplication { private Hangfire.BackgroundJobServer _backgroundJobServer; protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); // 初始化 Autofac 依赖注入容器 AutofacInitializer.Init(); //初始化 Hangfire 定时任务 _backgroundJobServer = HangfireInitializer.Init(); } protected void Application_End() { _backgroundJobServer?.Dispose(); } }
参考链接:https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat/tree/main/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47
https://www.coder.work/article/3021405
标签:WEBAPI,Autofac,ASP,container,builder,Hangfire,new,public From: https://www.cnblogs.com/htsboke/p/17225346.html