public void Intercept(InterceptionContext context) { var stackItems = ServiceProvider.GetServices <ISyncProxyMiddleware>(); var stack = new SyncProxyDelegateStack(stackItems); var options = ServiceProvider.GetService <IServiceOptions <TService> >(); IServiceScope scope = null; try { var lifetime = options?.Lifetime ?? ConnectedServiceLifetime.PerCall; if (lifetime == ConnectedServiceLifetime.PerCall) { scope = ServiceProvider.CreateScope(true); } var ctx = new SyncMiddlewareContext(ServiceProvider, scope, context); stack.Invoke(ctx); } finally { scope?.Dispose(); } }
public void TestSyncProxyMiddlware() { SyncMiddlewareContext proxyContext = null; var middleware = new DelegateSyncProxyMiddlware(p => proxyContext = proxyContext ?? p); var serviceCollection = new ServiceCollection(); serviceCollection.AddConnectedService <ISyncService>(); serviceCollection.AddConnectedLocal <ISyncService, SyncService>(); serviceCollection.AddScoped <ScopedDependency>(); serviceCollection.AddScopeProxy(); serviceCollection.AddSingleton <ISyncProxyMiddleware>(middleware); var prov = serviceCollection.BuildServiceProvider(); var service = prov.GetRequiredService <ISyncService>(); var text = Guid.NewGuid().ToString(); var call = service.SyncMethodWithResult(text, 123); Assert.NotNull(proxyContext); Assert.Equal(text, proxyContext.InterceptionContext.Arguments[0]); Assert.Equal(123, proxyContext.InterceptionContext.Arguments[1]); Assert.Equal(nameof(ISyncService.SyncMethodWithResult), proxyContext.InterceptionContext.MemberName); var ex = Assert.Throws <SyncService.SyncServiceException>(() => service.SyncVoidMethod(text, 1234)); var ex2 = Assert.Throws <SyncService.SyncServiceException>(() => service.SyncVoidMethod(text, 1234)); Assert.NotEqual(ex.ScopedDependency.Id, ex2.ScopedDependency.Id); }
public void Next(SyncMiddlewareContext context, int index = 0) { if (this._steps.Count > index) { this._steps[index].Invoke(p => Next(p, index + 1), context); return; } var target = context.ServiceProvider.GetConnectedService <TService>(); var result = context.InterceptionContext.ExecuteBodyOn <TService>(target); context.InterceptionContext.SetResult(result); }
public void Invoke(SyncMiddlewareContext context) { Next(context); }
public void Invoke(SyncProxyDelegate next, SyncMiddlewareContext context) { _called(context); next(context); }