public static async void Test(string url) { using (KernelServiceClient client = new KernelServiceClient( "NetNamedPipeBinding_KernelService", url)) { await client.GetPropertyAsync(new GetPropertyRequest()); } }
// return: // 0 主流程需返回-1 // 1 需要重做API int ConvertWebError(Exception ex0, out string strError) { // 服务器重启后 if (ex0 is System.ServiceModel.Security.MessageSecurityException) { System.ServiceModel.Security.MessageSecurityException ex = (System.ServiceModel.Security.MessageSecurityException)ex0; this.ErrorCode = ChannelErrorCode.RequestError; // 一般错误 this.ErrorInfo = GetExceptionMessage(ex); this.m_ws = null; strError = this.ErrorInfo; if (this.m_nRedoCount == 0) { this.m_nRedoCount++; return 1; // 重做 } return 0; } if (ex0 is CommunicationObjectFaultedException) { CommunicationObjectFaultedException ex = (CommunicationObjectFaultedException)ex0; this.ErrorCode = ChannelErrorCode.RequestError; // 一般错误 this.ErrorInfo = GetExceptionMessage(ex); this.m_ws = null; strError = this.ErrorInfo; // 2011/7/2 if (this.m_nRedoCount == 0) { this.m_nRedoCount++; return 1; // 重做 } return 0; } if (ex0 is EndpointNotFoundException) { EndpointNotFoundException ex = (EndpointNotFoundException)ex0; this.ErrorCode = ChannelErrorCode.RequestError; // 一般错误 this.ErrorInfo = "服务器 " + this.Url + " 没有响应"; this.m_ws = null; strError = this.ErrorInfo; return 0; } /* if (ex0 is CommunicationException) { CommunicationException ex = (CommunicationException)ex0; } * */ if (ex0 is WebException) { WebException ex = (WebException)ex0; if (ex.Status == WebExceptionStatus.Timeout) { this.ErrorCode = ChannelErrorCode.RequestTimeOut; this.ErrorInfo = "请求超时"; // (当前超时设置为" + Convert.ToString(this.Timeout) + ")"; strError = this.ErrorInfo; return 0; } if (ex.Status == WebExceptionStatus.RequestCanceled) { this.ErrorCode = ChannelErrorCode.RequestCanceled; this.ErrorInfo = "用户中断"; strError = this.ErrorInfo; return 0; } this.ErrorCode = ChannelErrorCode.RequestError; // 一般错误 this.ErrorInfo = GetExceptionMessage(ex); strError = this.ErrorInfo; return 0; } // 2013/1/11 if (ex0 is System.ServiceModel.CommunicationException && ex0.InnerException is System.ServiceModel.QuotaExceededException) { this.ErrorCode = ChannelErrorCode.QuotaExceeded; this.ErrorInfo = GetExceptionMessage(ex0); strError = this.ErrorInfo; if (this.m_nRedoCount == 0) { this.m_nRedoCount++; return 1; // 重做 } return 0; } this.ErrorCode = ChannelErrorCode.RequestError; // 一般错误 this.ErrorInfo = GetExceptionMessage(ex0); this.m_ws = null; // 不知是否正确 strError = this.ErrorInfo; return 0; }
// 2015/5/4 public void Close() { if (this.m_ws != null) { // TODO: Search()要单独处理 try { if (this.m_ws.State != CommunicationState.Faulted) this.m_ws.Close(); } catch { this.m_ws.Abort(); } this.m_ws = null; } }
public void Close() { if (this.m_ws != null) { this.m_ws.Close(); this.m_ws = null; } }
/* public int Timeout { get { return ws.Timeout; } set { ws.Timeout = value; } } * */ public void Abort() { if (m_nInSearching > 0) { if (this.m_ws != null) { if (this.m_bStoped == false) { this.DoStop(); // TODO: 如果时间太长了不返回,则调用Abort()? this.m_bStoped = true; return; } // 否则,就走到Abort()那里 } } if (this.m_ws != null) ws.Abort(); // ws.servicepoint.CloseConnectionGroup(ws.ConnectionGroupName); /* if (soapresult != null) ((WebClientAsyncResult)soapresult).Abort(); else ws.Abort(); * */ // 2011/1/7 add this.m_ws = null; }
/// <summary> /// 立即放弃通讯。而 Abort() 要文雅一些 /// </summary> public void AbortIt() { lock (syncRoot) { if (this.m_ws != null) { this.m_ws.Abort(); // TODO: 是否因为这里没有调用 .Close() 导致通道泄露? this.m_ws.Close(); // 2015/12/31 this.m_ws = null; } } }