public async Task InterceptionContextAsyncRegisterInsteadOfInterceptorsTest() { bool before1 = false, exception = false, after1 = false; var context = new AsyncInterceptionContext(this, "TestMethod", (Func <string, Task <string> >)TestMethodAsync, "TestValue"); context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => before1 = true, InterceptionMode.BeforeBody)); context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => c.SetResult("Result"), InterceptionMode.InsteadOfBody)); try { context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => c.SetResult("whatever"), InterceptionMode.InsteadOfBody)); } catch (ArgumentException ex) { exception = ex.ParamName == "interceptor"; } context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => after1 = true, InterceptionMode.AfterBody)); var result = await context.ExecuteAsync <string>(); Assert.True(before1); Assert.True(exception); Assert.Equal("Result", result); Assert.True(after1); }
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(); } }
protected async Task InsteadInterceptorAsync(AsyncInterceptionContext context) { if (context.HasResult) { context.SetResult(await context.ExecuteBodyAsync()); await Task.Run(() => OnInterceptorCalled(InterceptionMode.InsteadOfBody, context)); } }
public async Task InterCeptionContextInsteadOfWithValueTypes() { var context = new AsyncInterceptionContext(null, "test", new Func <Task <bool> >(async() => false)); context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => c.SetResult(Activator.CreateInstance(c.ReturnType.GetGenericArguments()[0])), InterceptionMode.InsteadOfBody)); var result = await context.ExecuteAsync <bool>(); Assert.False(result); }
private async Task <bool> EnsureService(AsyncInterceptionContext context) { if (this.State == ConnectionState.Connected || this.State == ConnectionState.Checking) { return(true); } Task <bool> task = null; lock (isConnectingLocker) { if (ensureServiceTaskCompletion != null) { task = ensureServiceTaskCompletion.Task; } else { ensureServiceTaskCompletion = new TaskCompletionSource <bool>(); } } if (task != null) { return(await task); } else { this.State = ConnectionState.Pending; #if SILVERLIGHT || NET4 await TaskEx.Delay(GetRetryOffset()); #else await Task.Delay(GetRetryOffset()); #endif var tokens = context.Arguments.OfType <CancellationToken>(); CancellationToken token = default(CancellationToken); if (tokens.Any()) { token = tokens.First(); if (token.IsCancellationRequested) { return(false); } } var reConnected = this.reconnectTaskCompletion.Task; try { await ServiceContext.Current.GetServiceAsync <TChannel>(this, token); } catch { this.State = ConnectionState.Disconnected; } var result = await reConnected; lock (isConnectingLocker) { var completion = this.ensureServiceTaskCompletion; ensureServiceTaskCompletion = null; completion.SetResult(result); } return(result); } }
protected async virtual Task ProxyMethodInterceptor(AsyncInterceptionContext context) { #if !SILVERLIGHT var oldCallback = CallContext.LogicalGetData(CallbackEnvironment.CallContextKey); var oldSession = CallContext.LogicalGetData(CallbackEnvironment.CallContextSessionIdKey); CallContext.LogicalSetData(CallbackEnvironment.CallContextKey, this); CallContext.LogicalSetData(CallbackEnvironment.CallContextSessionIdKey, this.SessionId); #endif int retrys = 0; try { while (GetRetryCount() == -1 || retrys <= GetRetryCount()) { try { if (await EnsureService(context)) { var result = await context.ExecuteBodyAsync(); context.SetResult(result); return; } else { retrys++; } } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } if (RetryCondition != null && RetryCondition(ex)) { retrys++; this.State = ConnectionState.Disconnected; } else { #if (SILVERLIGHT || NET4) throw ex; #else ExceptionDispatchInfo.Capture(ex).Throw(); #endif } } } if (context.HasResult && context.ReturnType.GetGenericArguments().First().IsValueType) { context.SetResult(Activator.CreateInstance(context.ReturnType.GetGenericArguments().First())); } } finally { #if !SILVERLIGHT CallContext.LogicalSetData(CallbackEnvironment.CallContextSessionIdKey, oldSession); CallContext.LogicalSetData(CallbackEnvironment.CallContextKey, oldCallback); #endif } }
public AsyncMiddlewareContext(IServiceProvider rootServiceProvider, IServiceScope scope, AsyncInterceptionContext interceptionContext) { RootServiceProvider = rootServiceProvider; Scope = scope; InterceptionContext = interceptionContext; }
protected Task BeforeInterceptorAsync(AsyncInterceptionContext context) { return(Task.Run(() => OnInterceptorCalled(InterceptionMode.BeforeBody, context))); }