【发布时间】:2012-03-13 15:19:46
【问题描述】:
当我尝试使用网络服务时,我得到以下异常。我的主要问题是这个异常什么时候发生?在服务器还是客户端?错误在哪里?服务器是否会因各种故障而抛出此错误?
我自己做了一些似乎有效的更改
它现在确实有效。我在服务客户端上删除了 using 并添加了 som cleanup。
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)
【问题讨论】:
-
异常调用栈其实告诉你很多必须注意的事情。例如,Dispose 调用 Close。因此,如果代理已经处于故障状态,则只能调用 Abort,不能调用 Close 或 Dispose。幸运的是,您发现了必要的更改。微软其实有一篇很好的文章强调推荐什么样的调用模式,msdn.microsoft.com/en-us/library/aa355056.aspx
-
也得到了这个异常,不是因为异常处理,而是因为试图在没有设置
TableName的情况下返回DataTable。见stackoverflow.com/questions/12702/…