void SetClaimsPrincipalToOperationContext(MessageRpc rpc) { // TODO: Reenable this code //ServiceSecurityContext securityContext = rpc.SecurityContext; //if (!rpc.HasSecurityContext) //{ // SecurityMessageProperty securityContextProperty = rpc.Request.Properties.Security; // if (securityContextProperty != null) // { // securityContext = securityContextProperty.ServiceSecurityContext; // } //} //if (securityContext != null) //{ // object principal; // if (securityContext.AuthorizationContext.Properties.TryGetValue(AuthorizationPolicy.ClaimsPrincipalKey, out principal)) // { // ClaimsPrincipal claimsPrincipal = principal as ClaimsPrincipal; // if (claimsPrincipal != null) // { // // // // Always set ClaimsPrincipal to OperationContext.Current if identityModel pipeline is used. // // // OperationContext.Current.ClaimsPrincipal = claimsPrincipal; // } // else // { // throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.NoPrincipalSpecifiedInAuthorizationContext)); // } // } //} }
void InspectInputs(ref MessageRpc rpc) { if (ParameterInspectors.Length > 0) { InspectInputsCore(ref rpc); } }
private async Task ProcessError(MessageRpc rpc) { try { _error.ProvideMessageFault(rpc); } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } _error.HandleError(e); } PrepareReply(rpc); if (rpc.CanSendReply) { rpc.ReplyTimeoutHelper = new TimeoutHelper(rpc.Channel.OperationTimeout); //if (rpc.Reply != null) //{ // TraceUtility.MessageFlowAtMessageSent(rpc.Reply, rpc.EventTraceActivity); //} await ReplyAsync(rpc); } ProcessMessageCleanup(rpc); }
// void EndFinalizeCorrelation(ref MessageRpc rpc) // { // try // { // rpc.Reply = rpc.CorrelationCallback.EndFinalizeCorrelation(rpc.AsyncResult); // } // catch (Exception e) // { // if (Fx.IsFatal(e)) // { // throw; // } // if (!this.error.HandleError(e)) // { // rpc.CanSendReply = false; // } // } // } bool EndReply(ref MessageRpc rpc) { bool success = false; try { rpc.TaskResult.GetAwaiter().GetResult(); rpc.RequestContextThrewOnReply = false; success = true; //if (TD.DispatchMessageStopIsEnabled()) //{ // TD.DispatchMessageStop(rpc.EventTraceActivity); //} } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } error.HandleError(e); } return(success); }
private ServiceSecurityContext GetAndCacheSecurityContext(MessageRpc rpc) { ServiceSecurityContext securityContext = rpc.SecurityContext; if (!rpc.HasSecurityContext) { SecurityMessageProperty securityContextProperty = rpc.Request.Properties.Security; if (securityContextProperty == null) { securityContext = null; // SecurityContext.Anonymous } else { securityContext = securityContextProperty.ServiceSecurityContext; if (securityContext == null) { throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SecurityContextMissing, rpc.Operation.Name)), rpc.Request); } } rpc.SecurityContext = securityContext; rpc.HasSecurityContext = true; } return(securityContext); }
internal void AfterReply(ref MessageRpc rpc) { if (rpc.Operation.IsTerminating && rpc.Channel.HasSession) { Timer timer = new Timer(new TimerCallback(AbortChannel), rpc.Channel.Binder.Channel, rpc.Channel.CloseTimeout, TimeSpan.FromMilliseconds(-1)); } }
internal async Task LockInstanceAsync(MessageRpc rpc) { if (concurrencyMode != ConcurrencyMode.Multiple) { ConcurrencyInstanceContextFacet resource = rpc.InstanceContext.Concurrency; bool needToWait = false; lock (rpc.InstanceContext.ThisLock) { if (!resource.Locked) { resource.Locked = true; } else { needToWait = true; } } if (needToWait) { await resource.EnqueueNewMessage(); } // TODO: Throw this on setup if (concurrencyMode == ConcurrencyMode.Reentrant) { throw new NotSupportedException(nameof(ConcurrencyMode.Reentrant)); } } }
public void Authenticate(ref MessageRpc rpc) { SecurityMessageProperty security = SecurityMessageProperty.GetOrCreate(rpc.Request); ReadOnlyCollection <IAuthorizationPolicy> authPolicy = security.ServiceSecurityContext.AuthorizationPolicies; try { authPolicy = _serviceAuthenticationManager.Authenticate(security.ServiceSecurityContext.AuthorizationPolicies, rpc.Channel.ListenUri, ref rpc.Request); if (authPolicy == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.AuthenticationManagerShouldNotReturnNull)); } } catch (Exception ex) { if (Fx.IsFatal(ex)) { throw; } // TODO: PerformanceCounters // TODO: Decide if we want Auditing and add back throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateFailedAuthenticationFaultException()); } rpc.Request.Properties.Security.ServiceSecurityContext.AuthorizationPolicies = authPolicy; }
void InitializeCallContextCore(MessageRpc rpc) { IClientChannel channel = rpc.Channel.Proxy as IClientChannel; int offset = Parent.CallContextCorrelationOffset; try { for (int i = 0; i < rpc.Operation.CallContextInitializers.Length; i++) { ICallContextInitializer initializer = CallContextInitializers[i]; rpc.Correlation[offset + i] = initializer.BeforeInvoke(rpc.InstanceContext, channel, rpc.Request); } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } if (ErrorBehavior.ShouldRethrowExceptionAsIs(e)) { throw; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); } }
void BeforeSendReply(MessageRpc rpc, ref Exception exception, ref bool thereIsAnUnhandledException) { if (_messageInspectors.Length > 0) { BeforeSendReplyCore(rpc, ref exception, ref thereIsAnUnhandledException); } }
internal void AfterReceiveRequestCore(ref MessageRpc rpc) { try { for (int i = 0; i < _messageInspectors.Length; i++) { rpc.Correlation[i] = _messageInspectors[i].AfterReceiveRequest(ref rpc.Request, (IClientChannel)rpc.Channel.Proxy, rpc.InstanceContext); //if (TD.MessageInspectorAfterReceiveInvokedIsEnabled()) //{ // TD.MessageInspectorAfterReceiveInvoked(rpc.EventTraceActivity, this.messageInspectors[i].GetType().FullName); //} } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } if (ErrorBehavior.ShouldRethrowExceptionAsIs(e)) { throw; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e); } }
private void InitializeCallContext(MessageRpc rpc) { if (CallContextInitializers.Length > 0) { InitializeCallContextCore(rpc); } }
internal async Task EnsureInstanceContextAsync(MessageRpc rpc) { if (rpc.InstanceContext == null) { rpc.InstanceContext = new InstanceContext(rpc.Host, false); rpc.InstanceContext.ServiceThrottle = rpc.channelHandler.InstanceContextServiceThrottle; rpc.MessageRpcOwnsInstanceContextThrottle = false; } rpc.OperationContext.SetInstanceContext(rpc.InstanceContext); rpc.InstanceContext.Behavior = this; if (rpc.InstanceContext.State == CommunicationState.Created) { Task openTask = null; lock (rpc.InstanceContext.ThisLock) { if (rpc.InstanceContext.State == CommunicationState.Created) { var helper = new TimeoutHelper(rpc.Channel.CloseTimeout); // awaiting the task outside the lock is safe as OpenAsync will transition the state away from Created before // it returns an uncompleted Task. openTask = rpc.InstanceContext.OpenAsync(helper.GetCancellationToken()); Fx.Assert(rpc.InstanceContext.State != CommunicationState.Created, "InstanceContext.OpenAsync should transition away from Created before returning a Task"); } } await openTask; } rpc.InstanceContext.BindRpc(rpc); }
void InspectOutputs(MessageRpc rpc) { if (ParameterInspectors.Length > 0) { InspectOutputsCore(rpc); } }
internal SynchronizationContext GetSyncContext(MessageRpc rpc) { Fx.Assert(rpc.InstanceContext != null, "instanceContext is null !"); SynchronizationContext syncContext = rpc.InstanceContext.SynchronizationContext ?? context; return(syncContext); }
internal void AfterReceiveRequest(ref MessageRpc rpc) { if (_messageInspectors.Length > 0) { AfterReceiveRequestCore(ref rpc); } }
internal void ProvideMessageFault(MessageRpc rpc) { if (rpc.Error != null) { ProvideMessageFaultCore(rpc); } }
internal void HandleError(MessageRpc rpc) { if (rpc.Error != null) { HandleErrorCore(rpc); } }
internal async Task <MessageRpc> InvokeAsync(MessageRpc rpc) { if (rpc.Error == null) { try { InitializeCallContext(rpc); object target = rpc.Instance; DeserializeInputs(rpc); InspectInputs(rpc); ValidateMustUnderstand(rpc); ValidateAuthorizedClaims(rpc); if (Parent.RequireClaimsPrincipalOnOperationContext) { SetClaimsPrincipalToOperationContext(rpc); } if (Parent.SecurityImpersonation != null) { await Parent.SecurityImpersonation.RunImpersonated(rpc, async() => { (rpc.ReturnParameter, rpc.OutputParameters) = await Invoker.InvokeAsync(target, rpc.InputParameters); }); } else { (rpc.ReturnParameter, rpc.OutputParameters) = await Invoker.InvokeAsync(target, rpc.InputParameters); } InspectOutputs(rpc); SerializeOutputs(rpc); }
internal void UnlockInstance(ref MessageRpc rpc) { if (concurrencyMode != ConcurrencyMode.Multiple) { ConcurrencyBehavior.UnlockInstance(rpc.InstanceContext); } }
void UninitializeCallContext(MessageRpc rpc) { if (CallContextInitializers.Length > 0) { UninitializeCallContextCore(rpc); } }
bool PrepareAndAddressReply(ref MessageRpc rpc) { bool canSendReply = true; if (!_manualAddressing) { if (!object.ReferenceEquals(rpc.RequestID, null)) { RequestReplyCorrelator.PrepareReply(rpc.Reply, rpc.RequestID); } if (!rpc.Channel.HasSession) { canSendReply = RequestReplyCorrelator.AddressReply(rpc.Reply, rpc.ReplyToInfo); } } AddMessageProperties(rpc.Reply, rpc.OperationContext, rpc.Channel); //if (FxTrace.Trace.IsEnd2EndActivityTracingEnabled && rpc.EventTraceActivity != null) //{ // rpc.Reply.Properties[EventTraceActivity.Name] = rpc.EventTraceActivity; //} return(canSendReply); }
void SetActivityIdOnThread(MessageRpc rpc) { //if (FxTrace.Trace.IsEnd2EndActivityTracingEnabled && rpc.EventTraceActivity != null) //{ // // Propogate the ActivityId to the service operation // EventTraceActivityHelper.SetOnThread(rpc.EventTraceActivity); //} }
void HandleErrorCore(MessageRpc rpc) { bool handled = HandleErrorCommon(rpc.Error, ref rpc.FaultInfo); if (handled) { rpc.Error = null; } }
void ProvideMessageFaultCoreCoda(MessageRpc rpc) { if (rpc.FaultInfo.Fault.Headers.Action == null) { rpc.FaultInfo.Fault.Headers.Action = rpc.RequestVersion.Addressing.DefaultFaultAction; } rpc.Reply = rpc.FaultInfo.Fault; }
internal Task <MessageRpc> DispatchAsync(MessageRpc rpc, bool isOperationContextSet) { rpc.ErrorProcessor = ProcessError; rpc.AsyncProcessor = ProcessMessageAsync; var task = rpc.ProcessAsync(isOperationContextSet); rpc._processCallReturned = true; return(task); }
Task AcquireDynamicInstanceContextAsync(MessageRpc rpc) { if (rpc.InstanceContext.QuotaThrottle != null) { return(AcquireDynamicInstanceContextCoreAsync(rpc)); } else { return(Task.CompletedTask); } }
bool AcquireDynamicInstanceContext(ref MessageRpc rpc) { //if (rpc.InstanceContext.QuotaThrottle != null) //{ // return AcquireDynamicInstanceContextCore(ref rpc); //} //else //{ return(true); //} }
void ValidateMustUnderstand(ref MessageRpc rpc) { if (parent.ValidateMustUnderstand) { rpc.NotUnderstoodHeaders = rpc.Request.Headers.GetHeadersNotUnderstood(); if (rpc.NotUnderstoodHeaders != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new MustUnderstandSoapException(rpc.NotUnderstoodHeaders, rpc.Request.Version.Envelope)); } } }
void ProvideMessageFaultCore(MessageRpc rpc) { if (messageVersion != rpc.RequestVersion) { Fx.Assert("CoreWCF.Dispatcher.ErrorBehavior.ProvideMessageFaultCore(): (this.messageVersion != rpc.RequestVersion)"); } InitializeFault(rpc); ProvideFault(rpc.Error, rpc.Channel.GetProperty <FaultConverter>(), ref rpc.FaultInfo); ProvideMessageFaultCoreCoda(rpc); }