// Asking for PeerTrust alone should throw SecurityNegotiationException // if the certificate is not in the TrustedPeople store. For this test // we use a valid chain-trusted certificate that we know is not in the // TrustedPeople store. public static void Https_SecModeTrans_CertValMode_PeerTrust_Fails_Not_In_TrustedPeople() { EndpointAddress endpointAddress = null; string testString = "Hello"; ChannelFactory <IWcfService> factory = null; IWcfService serviceProxy = null; CommunicationException communicationException = null; try { // *** SETUP *** \\ BasicHttpsBinding binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; endpointAddress = new EndpointAddress(new Uri( Endpoints.Https_SecModeTrans_ClientCredTypeNone_ServerCertValModeChainTrust_Address)); factory = new ChannelFactory <IWcfService>(binding, endpointAddress); factory.Credentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication(); factory.Credentials.ServiceCertificate.SslCertificateAuthentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust; serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ try { serviceProxy.Echo(testString); } catch (CommunicationException ce) { communicationException = ce; } // *** VALIDATE *** \\ Assert.True(communicationException != null, "Expected CommunicationException but no exception was thrown."); Assert.True(communicationException.GetType().Name == "SecurityNegotiationException", String.Format("Expected SecurityNegotiationException but received {0}", communicationException.ToString())); // *** CLEANUP *** \\ ((ICommunicationObject)serviceProxy).Close(); factory.Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }
// Asking for PeerTrust alone should throw SecurityNegotiationException // if the certificate is not in the TrustedPeople store. For this test // we use a valid chain-trusted certificate that we know is not in the // TrustedPeople store. public static void NetTcp_SecModeTrans_CertValMode_PeerTrust_Fails_Not_In_TrustedPeople() { EndpointAddress endpointAddress = null; string testString = "Hello"; ChannelFactory <IWcfService> factory = null; IWcfService serviceProxy = null; CommunicationException communicationException = null; try { // *** SETUP *** \\ NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport); binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None; endpointAddress = new EndpointAddress(new Uri( Endpoints.Tcp_CustomBinding_SslStreamSecurity_Address)); factory = new ChannelFactory <IWcfService>(binding, endpointAddress); factory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust; serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ try { serviceProxy.Echo(testString); } catch (CommunicationException ce) { communicationException = ce; } // *** VALIDATE *** \\ Assert.True(communicationException != null, "Expected CommunicationException but no exception was thrown."); Assert.True(communicationException.GetType().Name == "SecurityNegotiationException", String.Format("Expected SecurityNegotiationException but received {0}", communicationException.ToString())); // *** CLEANUP *** \\ // objects are in faulted state and will throw, so only use finally style cleanup } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }
public WebStream Open() { int num; Exception communicationException = null; int num1 = 5; do { int num2 = num1 - 1; try { MessagingClientEtwProvider.Provider.WebStreamConnecting(base.Activity, this.factoryEndpointUri.AbsoluteUri, this.sbUri.AbsoluteUri, num2); if (this.Connect()) { MessagingClientEtwProvider.Provider.WebStreamConnectCompleted(base.Activity, this.factoryEndpointUri.AbsoluteUri, this.sbUri.AbsoluteUri, num2); return(this); } } catch (WebException webException1) { WebException webException = webException1; if (Fx.IsFatal(webException)) { throw; } string str = WebStream.CloseResponseInWebException(webException); communicationException = new CommunicationException(string.Concat(webException.Message, str), webException); } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } communicationException = exception; } string str1 = (communicationException != null ? communicationException.ToString() : "Failed to connect. No exception thrown."); MessagingClientEtwProvider.Provider.WebStreamConnectFailed(base.Activity, this.factoryEndpointUri.AbsoluteUri, this.sbUri.AbsoluteUri, num2, str1); this.Reset(); this.disposed = false; num = num1 - 1; num1 = num; }while (num > 0); this.disposed = true; throw Fx.Exception.AsError(new CommunicationException(SRClient.HTTPConnectivityMode, communicationException), null); }
/// <summary> /// 解析异常类型 /// </summary> /// <param name="innerExp">Exception对象</param> /// <returns>返回详细信息</returns> private static string HandleException(Exception innerExp) { StringBuilder builder = new StringBuilder(); while (innerExp != null) { //服务端异常 FaultException <string> faultEx = innerExp as FaultException <string>; if (faultEx != null) { builder.AppendFormat("-------------------{0}:{1}-----------------\r\n", faultEx.GetType().Name, faultEx.Message); builder.AppendLine(faultEx.Detail); break; } //SOAP 错误 FaultException faultEx1 = innerExp as FaultException; if (faultEx1 != null) { builder.AppendLine(faultEx1.ToString()); break; } //给进程或操作分配的时间过期时引发的异常 TimeoutException timeoutEx = innerExp as TimeoutException; if (timeoutEx != null) { builder.AppendLine("连接服务器发生异常,超时!"); builder.AppendLine(timeoutEx.ToString()); break; } //服务或客户端应用程序中的通信错误。 CommunicationException commuEx = innerExp as CommunicationException; if (commuEx != null) { builder.AppendLine("连接服务器发生异常,通信错误!"); builder.AppendLine(commuEx.ToString()); break; } builder.AppendFormat("-----------------{0} : {1}--------------------\r\n{2}", innerExp.GetType().Name, innerExp.Message, innerExp.StackTrace); innerExp = innerExp.InnerException; } return(builder.ToString()); }
// Asking for PeerTrust alone should throw SecurityNegotiationException // if the certificate is not in the TrustedPeople store. For this test // we use a valid chain-trusted certificate that we know is not in the // TrustedPeople store. public static void NetTcp_SecModeTrans_CertValMode_PeerTrust_Fails_Not_In_TrustedPeople() { #if FULLXUNIT_NOTSUPPORTED bool root_Certificate_Installed = Root_Certificate_Installed(); bool client_Certificate_Installed = Client_Certificate_Installed(); bool peer_Certificate_Installed = Peer_Certificate_Installed(); bool ssl_Available = SSL_Available(); if (!root_Certificate_Installed || !client_Certificate_Installed || !peer_Certificate_Installed || !ssl_Available) { Console.WriteLine("---- Test SKIPPED --------------"); Console.WriteLine("Attempting to run the test in ToF, a ConditionalFact evaluated as FALSE."); Console.WriteLine("Root_Certificate_Installed evaluated as {0}", root_Certificate_Installed); Console.WriteLine("Client_Certificate_Installed evaluated as {0}", client_Certificate_Installed); Console.WriteLine("Peer_Certificate_Installed evaluated as {0}", peer_Certificate_Installed); Console.WriteLine("SSL_Available evaluated as {0}", ssl_Available); return; } #endif EndpointAddress endpointAddress = null; string testString = "Hello"; ChannelFactory <IWcfService> factory = null; IWcfService serviceProxy = null; CommunicationException communicationException = null; try { // *** SETUP *** \\ NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport); binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None; endpointAddress = new EndpointAddress(new Uri( Endpoints.Tcp_CustomBinding_SslStreamSecurity_Address)); factory = new ChannelFactory <IWcfService>(binding, endpointAddress); factory.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.PeerTrust; serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ try { serviceProxy.Echo(testString); } catch (CommunicationException ce) { communicationException = ce; } // *** VALIDATE *** \\ Assert.True(communicationException != null, "Expected CommunicationException but no exception was thrown."); Assert.True(communicationException.GetType().Name == "SecurityNegotiationException", String.Format("Expected SecurityNegotiationException but received {0}", communicationException.ToString())); // *** CLEANUP *** \\ // objects are in faulted state and will throw, so only use finally style cleanup } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }
/// <summary> /// The behaviour to perform when a communication exception occurs while the service operation is called. /// </summary> /// <param name="exception">The exception.</param> public void PerformCommunicationExceptionBehaviour(CommunicationException exception) { Console.Error.WriteLine(exception == null ? NullPhrase : exception.ToString()); }
private static void MessageReceivePump(object state) { bool flag; DuplexRequestBindingElement.DuplexRequestSessionChannel duplexRequestSessionChannel = (DuplexRequestBindingElement.DuplexRequestSessionChannel)state; try { do { flag = false; if (duplexRequestSessionChannel.State != CommunicationState.Opened && duplexRequestSessionChannel.State != CommunicationState.Closing) { continue; } IAsyncResult asyncResult = duplexRequestSessionChannel.innerChannel.BeginTryReceive(TimeSpan.MaxValue, DuplexRequestBindingElement.DuplexRequestSessionChannel.onMessageReceived, duplexRequestSessionChannel); if (!asyncResult.CompletedSynchronously) { continue; } flag = duplexRequestSessionChannel.HandleMessageReceived(asyncResult); }while (flag); } catch (CommunicationException communicationException1) { CommunicationException communicationException = communicationException1; MessagingClientEtwProvider.TraceClient(() => MessagingClientEtwProvider.Provider.EventWriteRuntimeChannelFaulting(duplexRequestSessionChannel.innerChannel.GetType().Name, duplexRequestSessionChannel.innerChannel.LocalAddress.Uri.AbsoluteUri, duplexRequestSessionChannel.innerChannel.RemoteAddress.Uri.AbsoluteUri, duplexRequestSessionChannel.innerChannel.Via.AbsoluteUri, duplexRequestSessionChannel.innerChannel.Session.Id, string.Concat("MessageReceivePump: ", communicationException.ToString()))); duplexRequestSessionChannel.Fault(); } }
/// <summary> /// The behaviour to perform when a communication exception occurs while the service operation is called. /// </summary> /// <param name="exception">The exception.</param> public void PerformCommunicationExceptionBehaviour(CommunicationException exception) { Console.Out.WriteLine(exception == null ? NullPhrase : Prefix + "Communication: " + exception.ToString()); }