WCF:FaultContract(typeof(ExceptionDetail))问题

发布于 2024-08-25 19:32:03 字数 252 浏览 5 评论 0原文

我已经为我的操作合约添加了属性 [FaultContract(typeof(ExceptionDetail))] 。当我尝试将服务添加到客户端应用程序时,出现此错误 - “自定义工具错误:无法为服务引用“ServiceReference1”生成代码。请检查其他错误和警告消息以了解详细信息。”< /code>

但是当我注释掉FaultContract 属性时,我可以在我的客户端应用程序中添加wcf 服务引用。

I have put the attribute [FaultContract(typeof(ExceptionDetail))] for my operation contract. When I am trying to add the service to a client application, I get this error - "Custom tool error: Failed to generate code for the service reference 'ServiceReference1'. Please check other error and warning messages for details."

But when I comment out the FaultContract Attribute, I am able to add the wcf service reference th' my client app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

辞别 2024-09-01 19:32:03

拥有FaultContracts的目的是首先能够从服务传回SOAP错误,这不会破坏服务器和客户端之间的通信通道(优雅且可互操作地处理.NET异常等错误条件),其次,使用FaultContracts,您的服务器将抛出类型错误(FaultException),并且您的客户端可以捕获这些错误。

如果您希望或需要真正具有互操作性,则需要:

  • 将所有的FaultContract 类型定义为用[DataContract] 属性修饰的类
  • 捕获服务器上的所有.NET 异常(使用例如IErrorHandler 接口)并将它们转换为可互操作的 SOAP 错误

如果你控制了电线的两端,并且两端都是.NET,那么你可以简化这一点:在服务器上,处理所有.NET异常并将它们变成例如FaultException,即就是,创建一个“错误(无论什么 .NET 异常)”,然后在客户端捕获这些类型的FaultException 并处理它们:

[FaultContract(typeof(ArgumentOutOfRangeException)]
[OperationContract]
public void CallService(.......)

然后在您的实现中使用:

try
{
    clientProxy.CallService();
}
catch(FaultException<ArgumentOutOfRangeException> ex)
{
   // handle the most specific exception first
}
catch(FaultException ex)
{
   // handle all other, unspecific server faults
}
catch(CommunicationException ex)
{
   // handle all other, client-proxy related WCF errors
}
catch(Exception ex)
{
   // handle anything else....
}

The point of having FaultContracts is to make it possible to first of all pass back SOAP faults from the service which will not break the communication channel between the server and the client (handling error conditions like .NET exceptions gracefully and interoperably), and secondly, using FaultContracts, your server than throw typed faults (FaultException<T>) and your client can catch those.

If you want or need to be really interoperable, you need to:

  • define all your FaultContract types as classes decorated with the [DataContract] attribute
  • catch all .NET exceptions on the server (using e.g. IErrorHandler interface) and turn them into interoperable SOAP faults

If you control both ends of the wire and both ends are .NET, then you can simplify this by one step: on the server, handle all .NET exceptions and turn them into e.g. FaultException<ArgumentOutOfRangeException>, that is, create a "fault of (whatever .NET exception)" and then on the client, catch those typed FaultException and handle them:

[FaultContract(typeof(ArgumentOutOfRangeException)]
[OperationContract]
public void CallService(.......)

and then in your implementation, use this:

try
{
    clientProxy.CallService();
}
catch(FaultException<ArgumentOutOfRangeException> ex)
{
   // handle the most specific exception first
}
catch(FaultException ex)
{
   // handle all other, unspecific server faults
}
catch(CommunicationException ex)
{
   // handle all other, client-proxy related WCF errors
}
catch(Exception ex)
{
   // handle anything else....
}
凉薄对峙 2024-09-01 19:32:03

删除该 FaultContract,并配置 includeExceptionDetailInFaults

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Behavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Remove that FaultContract, and instead configure includeExceptionDetailInFaults:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Behavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
放肆 2024-09-01 19:32:03

Use Service Trace Viewer tool from http://msdn.microsoft.com/en-us/library/ms732023.aspx, to view the activity trace .

永不分离 2024-09-01 19:32:03

几分钟前我也遇到了同样的问题。
这是由于缺少默认构造函数造成的。另请记住,所有属性都必须具有公共 get/set 访问器。

I had the same problem few minutes ago.
It was due to the absence of a default constructor. Also remember that all properties must have public get/set accessors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文