示例#1
0
        private void ConfigureHangfire(IAppBuilder app)
        {
            Container c = new Container();

            c.Options.DefaultScopedLifestyle       = new ExecutionContextScopeLifestyle();
            c.Options.AllowOverridingRegistrations = true;

            JsonConvert.DefaultSettings = () => Constants.Misc_DefaultJsonSerializerSettings
                                          .CopyTo(new JsonSerializerSettings())
                                          .UsingValue(_s =>
            {
                _s.TypeNameHandling = TypeNameHandling.Objects;
            });

            global::Hangfire.GlobalConfiguration.Configuration
            .UseFilter(new Hangfire.Interceptor(c.BeginExecutionContextScope))
            .UseSqlServerStorage(Hangfire.DBInitializer.InitDb("HangfireDb"))
            .UsingValue(_storage => _storage
                        .UseActivator(new SimpleInjectorJobActivator(DIRegistrations.RegisterHangfireTypes(c, _storage.Entry.GetConnection()))));

            app.UseHangfireDashboard();
            app.UseHangfireServer();
        }
        public void TestMethod1()
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();

            DIRegistrations.RegisterTypes(container);
            container.BeginExecutionContextScope().Using(cxt =>
            {
                var x = cxt.GetInstance <ICredentialHasher>();

                var start = DateTime.Now;
                x.CalculateHash("abcdefghijkl");
                Console.WriteLine($"First call: {DateTime.Now - start}");

                start = DateTime.Now;
                x.CalculateHash("abcdefghijkl");
                Console.WriteLine($"Second call: {DateTime.Now - start}");

                start = DateTime.Now;
                x.CalculateHash("abcdefghijkl");
                Console.WriteLine($"Third call: {DateTime.Now - start}");
            });
        }