示例#1
0
        public async Task InterceptAsync(AsyncInterceptionContext context)
        {
            var stackItems = ServiceProvider.GetServices <IAsyncProxyMiddleware>();
            var options    = ServiceProvider.GetService <IServiceOptions <TService> >();

            var stack = new AsyncProxyDelegateStack(stackItems);

            IServiceScope scope = null;

            try
            {
                var lifetime = options?.Lifetime ?? ConnectedServiceLifetime.PerCall;

                if (lifetime == ConnectedServiceLifetime.PerCall)
                {
                    scope = ServiceProvider.CreateScope(true);
                }

                var ctx = new AsyncMiddlewareContext(ServiceProvider, scope, context);
                await stack.Invoke(ctx);
            }
            finally
            {
                scope?.Dispose();
            }
        }
示例#2
0
        public async Task TestAsyncProxyMiddlware()
        {
            AsyncMiddlewareContext proxyContext = null;

            var middleware = new DelegateAsyncProxyMiddlware(p => proxyContext = proxyContext ?? p);

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddConnectedService <IAsyncService>();
            serviceCollection.AddConnectedLocal <IAsyncService, AsyncService>();
            serviceCollection.AddScoped <ScopedDependency>();
            serviceCollection.AddScopeProxy();
            serviceCollection.AddSingleton <IAsyncProxyMiddleware>(middleware);

            var prov    = serviceCollection.BuildServiceProvider();
            var service = prov.GetRequiredService <IAsyncService>();

            var text = Guid.NewGuid().ToString();
            var call = await service.AsyncMethodWithResult(text, 123);

            Assert.NotNull(proxyContext);
            Assert.Equal(text, proxyContext.InterceptionContext.Arguments[0]);
            Assert.Equal(123, proxyContext.InterceptionContext.Arguments[1]);
            Assert.Equal(nameof(IAsyncService.AsyncMethodWithResult), proxyContext.InterceptionContext.MemberName);

            var ex = await Assert.ThrowsAsync <AsyncService.AsyncServiceException>(() => service.AsyncVoidMethod(text, 1234));

            var ex2 = await Assert.ThrowsAsync <AsyncService.AsyncServiceException>(() => service.AsyncVoidMethod(text, 1234));

            Assert.NotEqual(ex.ScopedDependency.Id, ex2.ScopedDependency.Id);
        }
示例#3
0
            public async Task Next(AsyncMiddlewareContext context, int index = 0)
            {
                if (this._steps.Count > index)
                {
                    await this._steps[index].Invoke(p => Next(p, index + 1), context);
                    return;
                }

                var target = context.ServiceProvider.GetConnectedService <TService>();
                var result = await context.InterceptionContext.ExecuteBodyOnAsync <TService>(target);

                context.InterceptionContext.SetResult(result);
            }
示例#4
0
 public async Task Invoke(AsyncMiddlewareContext context)
 {
     await Next(context);
 }
 public async Task Invoke(AsyncProxyDelegate next, AsyncMiddlewareContext context)
 {
     _called(context);
     await next(context);
 }