/// <summary> /// Gets the client /// If the broker client is null, a broker client is created by the client id and returned /// If the broker client is not null, the client id must match the broker client's id if this is not a singleton frontend, otherwise an exception would be throwed /// </summary> /// <param name="clientId">indicating the client id</param> /// <returns>the broker client</returns> private BrokerClient GetClient(string clientId) { // Bug 3062: For http client, the connection is always the same thus we should allow different client id in the same channel if (this.isSingleton) { BrokerClient client = this.clientManager.GetClient(clientId, GetUserName()); client.SingletonInstanceConnected(); return(client); } else { if (this.client == null) { lock (this.lockToCreateClient) { if (this.client == null) { //get domainUsername. string domainUsername; if (SoaCommonConfig.WithoutSessionLayer) { string domain = Environment.UserDomainName; string username = Environment.UserName; if (domain != System.Net.Dns.GetHostName()) { domainUsername = Path.Combine(domain, username); } else { domainUsername = username; } } else { domainUsername = GetUserName(); } this.client = this.clientManager.GetClient(clientId, domainUsername); this.client.FrontendConnected(this, this); return(this.client); } } } if (String.Compare(this.client.ClientId, clientId, StringComparison.OrdinalIgnoreCase) == 0) { return(this.client); } else { ThrowHelper.ThrowSessionFault(SOAFaultCode.Broker_ClientIdNotMatch, SR.ClientIdNotMatch); Debug.Fail("[ServiceJobMonitor] This line could not be reached."); return(null); } } }
/// <summary> /// AsyncCallback for ReceiveRequest /// </summary> /// <param name="ar">async result</param> private void ReceiveRequest(IAsyncResult ar) { ChannelClientState state = (ChannelClientState)ar.AsyncState; T channel = (T)state.Channel; BrokerClient client = state.Client; RequestContext request; try { request = channel.EndReceiveRequest(ar); } catch (TimeoutException) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[RequestReplyFrontEnd] Receive Request timed out"); if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } catch (CommunicationException ce) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[RequestReplyFrontEnd] Exception while receiving requests: {0}", ce.Message); // Retry receiving requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } #region Debug Failure Test SimulateFailure.FailOperation(1); #endregion // After channel timed out, the request will be null if you call channel.ReceiveRequest() // Need to end the channel at this time if (request == null) { // Indicate that the channel should be closed // Close the channel lock (channel) { if (channel.State == CommunicationState.Opened) { try { channel.Close(); } catch (CommunicationException ce) { BrokerTracing.TraceEvent(TraceEventType.Warning, 0, "[RequestReplyFrontEnd] Exception throwed while close the channel: {0}", ce); channel.Abort(); } } } return; } // Try to get the client id and the user name string callerSID; string userName = this.GetUserName(request.RequestMessage, out callerSID); string clientId = GetClientId(request.RequestMessage, callerSID); try { ParamCheckUtility.ThrowIfTooLong(clientId.Length, "clientId", Constant.MaxClientIdLength, SR.ClientIdTooLong); ParamCheckUtility.ThrowIfNotMatchRegex(ParamCheckUtility.ClientIdValid, clientId, "clientId", SR.InvalidClientId); } catch (ArgumentException) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedClientIdInvalid(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage)); RequestContextBase requestContextToReject = new RequestReplyRequestContext(request, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.GenerateFaultMessage(request.RequestMessage, request.RequestMessage.Headers.MessageVersion, SOAFaultCode.Broker_InvalidClientIdOrTooLong, SR.InvalidClientIdOrTooLong), this.ReplySentCallback, requestContextToReject); return; } if (client == null || client.State == BrokerClientState.Disconnected || String.Compare(clientId, client.ClientId, StringComparison.OrdinalIgnoreCase) != 0) { try { client = this.ClientManager.GetClient(clientId, userName); client.SingletonInstanceConnected(); } catch (FaultException <SessionFault> e) { if (e.Detail.Code == (int)SOAFaultCode.AccessDenied_BrokerQueue) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedAuthenticationError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), userName); RequestContextBase requestContextToReject = new RequestReplyRequestContext(request, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.GenerateFaultMessage(request.RequestMessage, request.RequestMessage.Headers.MessageVersion, SOAFaultCode.Broker_UserNameNotMatch, SR.UserNameNotMatch, userName, clientId), this.ReplySentCallback, requestContextToReject); return; } else { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), e.ToString()); throw; } } catch (BrokerQueueException e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), e.ToString()); RequestContextBase requestContextToReject = new RequestReplyRequestContext(request, this.Observer); requestContextToReject.BeginReply(FrontEndFaultMessage.TranslateBrokerQueueExceptionToFaultMessage(e, request.RequestMessage), this.ReplySentCallback, requestContextToReject); return; } catch (Exception e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request.RequestMessage), e.ToString()); throw; } state.Client = client; } // Receive new requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } // Try to get the user info header bool userDataFlag = GetUserInfoHeader(request.RequestMessage); // Create request context RequestContextBase requestContext; if (userDataFlag) { // Message that needs not to reply requestContext = DummyRequestContext.GetInstance(request.RequestMessage.Version); // Reply the message to the client immediately if user data is found for request/reply MEP (basic http binding) try { request.BeginReply(Message.CreateMessage(request.RequestMessage.Headers.MessageVersion, request.RequestMessage.Headers.Action + "Response"), this.ReplySent, request); } catch (Exception e) { BrokerTracing.TraceEvent(TraceEventType.Error, 0, "[RequestReplyFrontEnd] Exception throwed while trying to reply dummy message to client: {0}", e); } } else { requestContext = new RequestReplyRequestContext(request, this.Observer, client); } // Check auth if (!this.CheckAuth(requestContext, request.RequestMessage)) { return; } // Bug 15195: Remove security header for https TryRemoveSecurityHeaderForHttps(request.RequestMessage); // Send request to the broker client client.RequestReceived(requestContext, request.RequestMessage, null); }
/// <summary> /// AsyncCallback for ReceiveRequest /// </summary> /// <param name="ar">async result</param> private void ReceiveRequest(IAsyncResult ar) { ChannelClientState state = (ChannelClientState)ar.AsyncState; BrokerClient client = state.Client; Message request; try { request = this.azureQueueProxy.EndReceiveRequest(ar); } catch (TimeoutException) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[AzureQueueFrontEnd] Receive Request timed out"); if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } catch (CommunicationException ce) { BrokerTracing.TraceEvent(TraceEventType.Information, 0, "[AzureQueueFrontEnd] Exception while receiving requests: {0}", ce.Message); // Retry receiving requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } return; } #region Debug Failure Test SimulateFailure.FailOperation(1); #endregion // Try to get the client id and the user name string userName = GetUserName(request); string clientId = GetClientId(request, string.Empty); try { ParamCheckUtility.ThrowIfTooLong(clientId.Length, "clientId", Constant.MaxClientIdLength, SR.ClientIdTooLong); ParamCheckUtility.ThrowIfNotMatchRegex(ParamCheckUtility.ClientIdValid, clientId, "clientId", SR.InvalidClientId); } catch (ArgumentException) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedClientIdInvalid(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request)); return; } if (client == null || client.State == BrokerClientState.Disconnected || !client.ClientId.Equals(clientId, StringComparison.InvariantCultureIgnoreCase)) { try { client = this.ClientManager.GetClient(clientId, userName); client.SingletonInstanceConnected(); } catch (FaultException <SessionFault> e) { if (e.Detail.Code == (int)SOAFaultCode.AccessDenied_BrokerQueue) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedAuthenticationError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), userName); return; } else { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), e.ToString()); throw; } } catch (BrokerQueueException e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), e.ToString()); return; } catch (Exception e) { BrokerTracing.EtwTrace.LogFrontEndRequestRejectedGeneralError(this.SessionId, clientId, Utility.GetMessageIdFromMessage(request), e.ToString()); throw; } state.Client = client; } // Receive new requests if (!ar.CompletedSynchronously) { this.TryToBeginReceiveMessagesWithThrottling(state); } // Try to get the user info header bool userDataFlag = GetUserInfoHeader(request); // Create request context RequestContextBase requestContext; //Message that needs not to reply requestContext = DummyRequestContext.GetInstance(request.Version); // Bug 15195: Remove security header for https TryRemoveSecurityHeaderForHttps(request); // Send request to the broker client client.RequestReceived(requestContext, request, null); }