And*_*unt 3 wcf faultexception
一些跟踪显示正在抛出CommunicationException,因为我的异常T没有正确序列化的问题; 因为,两层深,我有一个匿名类型的对象,这是不可序列化的.删除它并冒泡更改似乎解决了它.在此之前还有其他小事我做过,但是我不记得我的生活是什么,只是在配置中没有完成.
我从我的踪迹中收到消息,例如:
Type 'RebuiltWCFService.Requests.HelloWorldRequest' with data contract name 'HelloWorldRequest:http://schemas.datacontract.org/2004/07/RebuiltWCFService.Requests' is not expected.
Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
Run Code Online (Sandbox Code Playgroud)
我今天遇到了一个看似奇怪的问题,我无法找到答案!
问题:当我抛出FaultException时,我的服务抛出了一个CommunicationException!如果我不抛出异常,它不会这样做.
在我的服务中,我正确地定义了错误合同:
[OperationContract]
[FaultContract(typeof(Faults.HelloWorldFault))]
Responses.HelloWorldResponse HelloWorld(Requests.HelloWorldRequest parameter);
Run Code Online (Sandbox Code Playgroud)
然后在错误条件下我抛出了正确类型的异常:
if (_errors.Count() > 0)
{
Faults.HelloWorldFault fault = new Faults.HelloWorldFault(_errors);
throw new FaultException<Faults.HelloWorldFault>(fault, new FaultReason("There are one or more errors in the request. Check the 'Errors' property for more detaisl"));
}
Run Code Online (Sandbox Code Playgroud)
然后我在客户端捕获它:
try
{
response = client.HelloWorld(new BasicService.HelloWorldRequest() { Number = 49 });
client.Close();
Console.WriteLine(String.Format("Response message: {0}, Response number: {1}", response.Message, response.Number));
}
catch (FaultException<BasicService.HelloWorldFault> ex)
{
...
}
Run Code Online (Sandbox Code Playgroud)
对我来说这一切似乎都没问题,并且它应该可行.但是,一旦我去测试我的错误子句(通过提供错误的数据,例如缺少字段),整个事情就会消失在我身上.当我抛出FaultException时,服务会抛出带有消息的CommunicationException
An error occurred while receiving the HTTP response to http://localhost:8732/Design_Time_Addresses/RebuiltWCFService/Service1/.
This could be due to the service endpoint binding not using the HTTP protocol.
This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down).
See server logs for more details.
Run Code Online (Sandbox Code Playgroud)
任何人都能提供一些关于这一点的见解吗?我正在使用basicHttp绑定,我也尝试使用wsHttp.我会根据要求发布我的配置文件.
FaultException是CommunicationException的子级.因此,代码中发生的事情没有任何问题.
如果您不确定处理时异常是什么,通常会将其报告为CommunicationException.如果要以自己的方式处理特定异常,请使用以下结构.
try
{ ... }
catch (FaultException<MyDemoException> me)
{ ... }
catch (FaultException fe)
{ ... }
catch (CommunicationException ce)
{ ... }
catch (Exception ex)
{ ... }
Run Code Online (Sandbox Code Playgroud)
在上面的结构中,Exception是CommunicationException的父级.CommunicationException是FaultException的父级,依此类推.
System.Object
System.Exception
System.SystemException
System.ServiceModel.CommunicationException
System.ServiceModel.FaultException
System.ServiceModel.FaultException<TDetail>
System.ServiceModel.Web.WebFaultException<T>
Run Code Online (Sandbox Code Playgroud)