public void ConcurrentMessageContextsResultsInDifferentServices()
        {
            ScopedService service1 = null;
            ScopedService service2 = null;

            new Thread(() =>
            {
                using (MessageContext.Establish(new Dictionary <string, object>()))
                {
                    Thread.Sleep(200);
                    service1 = container.Resolve <ScopedService>();
                    while (service2 == null)
                    {
                    }
                }
            }).Start();

            using (MessageContext.Establish(new Dictionary <string, object>()))
            {
                Thread.Sleep(200);
                service2 = container.Resolve <ScopedService>();
                while (service1 == null)
                {
                }
            }

            service1.ShouldNotBe(service2);
        }
示例#2
0
 public DependencyTestController(TransientClass transientService1, TransientClass transientService2, ScopedService scopedService1, ScopedService scopedService2)
 {
     _transientService1 = transientService1;
     _transientService2 = transientService2;
     _scopedService1    = scopedService1;
     _scopedService2    = scopedService2;
 }
示例#3
0
        public void ConcurrentMessageContextsResultsInDifferentServices()
        {
            ScopedService service1 = null;
            ScopedService service2 = null;

            new Thread(() =>
            {
                using (TransactionContext.None())
                    using (MessageContext.Establish())
                    {
                        Thread.Sleep(200);
                        service1 = kernel.Get <ScopedService>();
                        while (service2 == null)
                        {
                        }
                    }
            }).Start();

            using (TransactionContext.None())
                using (MessageContext.Establish())
                {
                    Thread.Sleep(200);
                    service2 = kernel.Get <ScopedService>();
                    while (service1 == null)
                    {
                    }
                }

            service1.ShouldNotBe(service2);
        }
示例#4
0
        protected override void OnStartup(StartupEventArgs e)
        {
#if !DEBUG
            //通过特殊手段运行应用可能导致工作目录与程序文件所在目录不一致,需要调整,否则配置文件和其他数据无法加载(仅限发布模式,调试模式修改工作目录也可能导致配置和其他数据无法加载)
            var pathToExe         = Process.GetCurrentProcess().MainModule.FileName;
            var pathToContentRoot = Path.GetDirectoryName(pathToExe);
            Directory.SetCurrentDirectory(pathToContentRoot);
#endif

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("guiAppsettings.json", optional: false, reloadOnChange: true);

            Configuration = builder.Build();

            var serviceCollection = new ServiceCollection();
            ConfigureServices(serviceCollection);

            ApplicationService = serviceCollection.BuildServiceProvider();
            ScopedService      = ApplicationService.CreateScope().ServiceProvider;

            var mainWindow = ScopedService.GetRequiredService <MainWindow>();
            this.MainWindow = mainWindow;
            mainWindow.Show();
        }
示例#5
0
 public ValuesController(ScopedService scoped, TransientService transient, SingletonService singleton, ICorrelationContextAccessor accessor)
 {
     _accessor  = accessor;
     _scoped    = scoped;
     _transient = transient;
     _singleton = singleton;
 }
示例#6
0
 public LifeTimeController(TransientService transientService,
                           ScopedService scopedService, SingletonService singletonService)
 {
     _transientService = transientService;
     _scopedService    = scopedService;
     _singletonService = singletonService;
 }
        public async Task InvokeAsync(HttpContext context, TransientService transientService,
                                      ScopedService scopedService, SingletonService singletonService)
        {
            context.Items.Add("CustomMiddlewareTransient", "Transient Middleware - " + transientService.GetGuid());
            context.Items.Add("CustomMiddlewareScoped", "Scoped Middleware - " + scopedService.GetGuid());
            context.Items.Add("CustomMiddlewareSingleton", "Singleton Middleware - " + singletonService.GetGuid());

            await _next(context);
        }
示例#8
0
 public CombeMartinContext(
     DbContextOptions <CombeMartinContext> options,
     SingletonService singletonService,
     ScopedService scopedService,
     TransientService transientService)
     : base(options)
 {
     SingletonService = singletonService;
     ScopedService    = scopedService;
     TransientService = transientService;
 }
示例#9
0
    public async Task <string> Scoped([FromServices] ScopedService service)
    {
        Console.WriteLine($"1. 注入服务Id: {service.Id}");

        var innerService1 = _serviceProvider.GetService <ScopedService>();

        Console.WriteLine($"2. 方法内获取的服务Id: {innerService1!.Id}");

        var innerService2 = _serviceProvider.GetService <ScopedService>();

        Console.WriteLine($"3. 方法内获取的服务Id: {innerService2!.Id}");

        return(service.Id);
    }
示例#10
0
        public void Scoped_GetService_SameInOneScope()
        {
            IServiceProvider scopedProvider1 = _provider.CreateScope().ServiceProvider;
            IServiceProvider scopedProvider2 = _provider.CreateScope().ServiceProvider;

            ScopedService scoped11 = scopedProvider1.GetService <ScopedService>();
            ScopedService scoped12 = scopedProvider1.GetService <ScopedService>();
            ScopedService scoped21 = scopedProvider2.GetService <ScopedService>();
            ScopedService scoped22 = scopedProvider2.GetService <ScopedService>();

            Assert.AreEqual(scoped11, scoped12);
            Assert.AreEqual(scoped21, scoped22);
            Assert.AreNotEqual(scoped11, scoped22);
        }
        public InjectionController(ScopedService scopedCounter1, ScopedService scopedCounter2,
                                   SingletonService singletonCounter1,
                                   SingletonService singletonCounter2,
                                   TransientService transientCounter1,
                                   TransientService transientCounter2)
        {
            _scopedCounter1 = scopedCounter1;
            _scopedCounter2 = scopedCounter2;

            _singletonCounter1 = singletonCounter1;
            _singletonCounter2 = singletonCounter2;

            _transientCounter1 = transientCounter1;
            _transientCounter2 = transientCounter2;
        }
示例#12
0
        public async Task HttpPost_Ensure_Scoped_Services_Work()
        {
            // arrange
            TestServer server = ServerFactory.Create(
                services =>
            {
                services.AddScoped <ScopedService>();
                services.AddGraphQL(SchemaBuilder.New()
                                    .AddQueryType(c => c
                                                  .Name("Query")
                                                  .Field("foo")
                                                  .Resolver(ctx =>
                {
                    ScopedService service = ctx.Service <ScopedService>();
                    service.Increase();
                    return(service.Count);
                })));
            },
                app => app
                .Use(next => ctx =>
            {
                ScopedService service = ctx.RequestServices.GetService <ScopedService>();
                service.Increase();
                return(next(ctx));
            })
                .UseGraphQL());

            var request =
                @"
                    {
                        foo
                    }
                ";
            var contentType = "application/graphql";

            // act
            HttpResponseMessage message =
                await server.SendPostRequestAsync(request, contentType, null);

            // assert
            ClientQueryResult result = await DeserializeAsync(message);

            result.MatchSnapshot();
        }
示例#13
0
        private void Application_SessionEnding(object sender, SessionEndingCancelEventArgs e)
        {
            if (!ScopedService.GetRequiredService <IOptions <AppSettings> >().Value.TriggerSessionEnding)
            {
                return;
            }
            if (!(MainWindow as MainWindow).IsHostRunning)
            {
                return;
            }

            MessageBoxResult result = MessageBox.Show($"{e.ReasonSessionEnding}. Host is Running. End session?", "Session Ending", MessageBoxButton.YesNo);

            // End session, if specified
            if (result == MessageBoxResult.Yes)
            {
                (MainWindow as MainWindow).StopHostAndClose();
            }
            if (result == MessageBoxResult.No)
            {
                e.Cancel = true;
            }
        }
示例#14
0
 public ValueTask InvokeAsync(InvocationContext invocationContext, SingleService singleService, ScopedService scopedService, TransientService transientService)
 => invocationContext.ProceedAsync();
示例#15
0
 public Service(TransientService transientService, ScopedService scopedService, SingletonService singletonService)
 {
     this.transientService = transientService;
     this.scopedService    = scopedService;
     this.singletonService = singletonService;
 }
示例#16
0
 public TestService(ScopedService scopedService)
 {
     this.scopedService = scopedService;
 }
示例#17
0
 public void Add(ScopedService service)
 {
     Services.Add(service);
 }
示例#18
0
 public ShowScope(ScopedService scoped1, ScopedService scoped2)
 {
     _Scoped1 = scoped1;
     _Scoped2 = scoped2;
 }
示例#19
0
 public UsedScopedService(ScopedService s)
 {
     _s = s;
 }
示例#20
0
 public ValuesController(SingletonService singletonService, ScopedService scopedService, TransientService transientService)
 {
     _singletonService = singletonService;
     _scopedService    = scopedService;
     _transientService = transientService;
 }
 public TypedClientWithScopedService(HttpClient httpClient, ScopedService service)
 {
     HttpClient = httpClient;
     Service    = service;
 }
 public HandlerWithScopedService(ScopedService service)
 {
     Service = service;
 }
 public ValuesController(IHttpClientFactory factory, ScopedService service, ILogger <ValuesController> logger)
 {
     _factory = factory;
     _service = service;
     _logger  = logger;
 }