解决方案:
一些跟踪显示 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.
原帖
我今天遇到了一个看似奇怪的问题,我就是找不到答案!
问题:当我抛出一个 FaultException 时,我的服务正在抛出一个 CommunicationException!如果我不抛出异常,它就不会这样做。
在我的服务中,我正确定义了故障合同:
[OperationContract]
[FaultContract(typeof(Faults.HelloWorldFault))]
Responses.HelloWorldResponse HelloWorld(Requests.HelloWorldRequest parameter);
然后在错误条件下我抛出正确类型的异常:
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"));
}
然后我在客户端捕捉到它:
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)
{
...
}
这对我来说似乎一切正常,并且应该可以工作。但是,一旦我去测试我的错误子句(通过提供错误的数据,例如缺少的字段),整个事情就死在我身上了。当我抛出我的 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.
有人可以提供一些关于这个的见解吗?我正在使用 basicHttp 绑定,我也尝试过使用 wsHttp。我会根据要求发布我的配置文件。