WCF无法用于通信,因为它处于故障状态



当我尝试使用Web服务时,会出现以下异常。我的主要问题是,这种异常是什么时候发生的?在服务器或客户端上?错误在哪里?服务器是否会因各种故障而抛出此项?

我自己做了一些更改,这些更改似乎有效

它现在确实有效。我在服务客户端上删除了使用并添加了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:bNetProjectsbNet Web ToolsSolution rootbNet.ServicesCustomersCresHeliosServiceForm.cs:line 99
at bNet.Web.Sites.Public.Customers.Cres.ServiceSkjema.Units.Page.ServiceFormControl.SubmitFormClick(Object sender, EventArgs e) in C:bNetProjectsbNet Web ToolsSolution rootbNet.Web.Sites.PublicCustomersCresServiceSkjemaUnitsPageServiceFormControl.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)

故障状态意味着服务器端出现了意外的异常。在之前的通话中。

你应该在客户端也得到一个异常,也许你的代码忽略了它?

您可以通过重新打开连接来解决此问题。但您似乎需要更好的错误处理。

与其使用using语句,不如尝试在不使用它的情况下运行代码。

来自

using(var client = new WCFClient())
{
    // ... code
}

var client = new WCFClient()
// ... code

执行此操作后,我们可以看到原始的WCF无法用于通信,因为它处于Faulted状态。消息是由using()调用本身引起的为什么我们使用WCF客户端的代码传递了无效凭据,服务器以错误响应,并将代理的状态更改为故障。正如我们所知,using()块在对象上调用Dispose()——在本例中是我们的WCF客户端。

由于WCF客户端失败,并且WCF客户端处于故障状态,调用Dispose()导致错误WCF无法用于通信,因为它处于要抛出的故障状态

我们可以通过将使用WCF客户端的代码封装在try...catch块中来看到这一点。

此错误也可能是由于使用OperationContract属性标记了零个方法造成的。这是我在构建新服务并进行长期测试时遇到的问题。

最新更新