Wcf 和 ExceptionDetail 问题
我创建了自己的类,如下所示:
[DataContract]
public class MyOperationFault : ExceptionDetail
{
/// <summary>
/// Contructor
/// </summary>
/// <param name="ex"></param>
public MyOperationFault(Exception ex) : base(ex)
{
}
}
然后我的 wcf 服务接口如下所示:
[OpearationContract()]
[FaultContract(typeof(MyOperationFault))]
void DoWork();
现在,在开发环境中一切都按预期工作 - 当我引发 FaultException
时:
throw new FaultException<MyOperationFault>(new MyOperationFault(new Exception("Failed")));
它在客户端被捕获,没问题。
当我使用 wcftestclient.exe
工具测试我的服务时,出现了问题。我收到此错误:
添加服务失败。服务元数据可能无法访问。 确保您的服务正在运行并公开元数据。 错误:无法从中获取元数据 http://localhost:33620/MyService.svc 如果这是 Windows (R) 请访问您可以访问的通信基础服务 检查您是否已在指定的位置启用元数据发布 地址。如需启用元数据发布的帮助,请参阅 MSDN 文档位于 http://go.microsoft.com/fwlink/?LinkId=65455。 WS-元数据交换 错误 URI:http://localhost:33620/MyService.svc 元数据 包含无法解析的引用: 'http://localhost:33620/MyService.svc'。无法连接到 http://localhost:33620/MyService.svc。 TCP 错误代码 10061:否 由于目标机器主动拒绝而无法建立连接 它127.0.0.1:33620。无法连接到远程服务器 否 由于目标机器主动拒绝而无法建立连接 它127.0.0.1:33620
一旦我注释掉 [FaultContract(typeof(MyOperationFault))]
从服务的方法 - wcftestclient
开始正常工作。如何解决这个问题?
I have created my own class like this:
[DataContract]
public class MyOperationFault : ExceptionDetail
{
/// <summary>
/// Contructor
/// </summary>
/// <param name="ex"></param>
public MyOperationFault(Exception ex) : base(ex)
{
}
}
then my wcf service interface looks like this:
[OpearationContract()]
[FaultContract(typeof(MyOperationFault))]
void DoWork();
Now everything works as expected in the dev environment - when I raise the FaultException
:
throw new FaultException<MyOperationFault>(new MyOperationFault(new Exception("Failed")));
It gets caught on the client's side no problem.
The problem appears when I move to test my service with wcftestclient.exe
tool. I am getting this error:
Failed to add a service. Service metadata may not be accessible.
Make sure your service is running and exposing metadata.
Error: Cannot obtain Metadata from
http://localhost:33620/MyService.svc If this is a Windows (R)
Communication Foundation service to which you have access, please
check that you have enabled metadata publishing at the specified
address. For help enabling metadata publishing, please refer to the
MSDN documentation at
http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange
Error URI: http://localhost:33620/MyService.svc Metadata
contains a reference that cannot be resolved:
'http://localhost:33620/MyService.svc'. Could not connect to
http://localhost:33620/MyService.svc. TCP error code 10061: No
connection could be made because the target machine actively refused
it 127.0.0.1:33620. Unable to connect to the remote server No
connection could be made because the target machine actively refused
it 127.0.0.1:33620
As soon as I comment out the [FaultContract(typeof(MyOperationFault))]
from the service's methods - the wcftestclient
starts working without a glitch. How to tackle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我面临着同样的问题。事实证明,用作详细参数的类需要无参数构造函数才能工作...
TestClient 可能失败,因为 MyOperationFault 缺少无参数构造函数。
我想出的解决方案是添加一个私有无参数构造函数,如下所示
I was facing the same issue. Turns out the classes used as detail parameter need a parameterless constructor in order to work...
The TestClient probably failed because MyOperationFault was lacking a parameterless constructor.
The solution i came up with was adding a private parameterless constructor as in