2012-02-23 117 views
10

當我嘗試使用web服務時,出現以下異常。我的主要問題是什麼時候發生這種異常?在服務器或客戶端?錯誤在哪裏?服務器是否會針對各種各樣的故障而拋出這個問題?WCF無法用於通信,因爲它處於故障狀態

我做我自己的一些變化,似乎工作

它實際上現在的作品。我刪除了使用並在服務客戶端添加了som清理。

if (Service != null && Service.State != CommunicationState.Faulted) 
       { 
        success = true; 
        Service.Close(); 
       } 

      } 
      catch (Exception ex) 
      { 
       msg = "Error" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace; 
      } 
      finally{ 
       if (!success) 
       { 
        if (Service != null) Service.Abort(); 
       } 
      } 

這是個例外:

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state. 

Server stack trace: 
at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout) 

Exception rethrown at [0]: 
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 
at System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout) 
at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout) 
at System.ServiceModel.ClientBase`1.Close() 
at System.ServiceModel.ClientBase`1.System.IDisposable.Dispose() 
at bNet.Services.Customers.Cres.Helios.ServiceForm.Send(ServiceFormAction task) in C:\bNetProjects\bNet Web Tools\Solution root\bNet.Services\Customers\Cres\Helios\ServiceForm.cs:line 99 
at bNet.Web.Sites.Public.Customers.Cres.ServiceSkjema.Units.Page.ServiceFormControl.SubmitFormClick(Object sender, EventArgs e) in C:\bNetProjects\bNet Web Tools\Solution root\bNet.Web.Sites.Public\Customers\Cres\ServiceSkjema\Units\Page\ServiceFormControl.ascx.cs:line 192 
at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 
+0

異常調用堆棧實際上告訴你很多事情,你必須注意。例如,Dispose調用Close。因此,如果代理服務器已處於Faulted狀態,則只能調用Abort,而不是Close或Dispose。幸運的是你找到了必要的改變。微軟事實上有一篇很好的文章,強調推薦使用什麼樣的調用模式,http://msdn.microsoft.com/en-us/library/aa355056.aspx – 2012-02-23 08:40:34

+0

請看這裏以及http:// stackoverflow。 com/questions/2763592/the-communication-object-system-servicemodel-channels-servicechannel-can not be – 2014-02-26 15:26:08

+0

也正是這個異常不是因爲異常處理,而是爲了試圖返回沒有'TableName'的'DataTable'。請參閱http://stackoverflow.com/questions/12702/returning-datatables-in-wcf-net – 2016-01-14 09:13:55

回答

15

故障狀態意味着已經在服務器端的意外例外。在較早的通話中。

你也應該在客戶端得到一個異常,也許你的代碼忽略它?

您可以通過重新打開連接來解決它。但似乎你需要更好的錯誤處理。

+0

我做了一些改變,我自己做了這項工作。謝謝您的回答。 – espvar 2012-02-23 08:37:22

1

此錯誤也可能是由帶有OperationContract屬性標記的零方法引起的。這是我在構建新服務和長期測試時遇到的問題。

相關問題