Wcf 和 ExceptionDetail 问题

发布于 2024-10-19 13:30:30 字数 1810 浏览 6 评论 0原文

我创建了自己的类,如下所示:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2024-10-26 13:30:30

我面临着同样的问题。事实证明,用作详细参数的类需要无参数构造函数才能工作...

TestClient 可能失败,因为 MyOperationFault 缺少无参数构造函数。

我想出的解决方案是添加一个私有无参数构造函数,如下所示

[DataContract]
public class MyOperationFault : ExceptionDetail
{
    /// i dont know why but this fixed the issue
    private MyOperationFault()
    {
    }

    /// <summary>
    /// Contructor
    /// </summary>
    /// <param name="ex"></param>
    public MyOperationFault(Exception ex) : base(ex)
    {
    }
}

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

[DataContract]
public class MyOperationFault : ExceptionDetail
{
    /// i dont know why but this fixed the issue
    private MyOperationFault()
    {
    }

    /// <summary>
    /// Contructor
    /// </summary>
    /// <param name="ex"></param>
    public MyOperationFault(Exception ex) : base(ex)
    {
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文