从 ASMX Web 服务捕获自定义异常

发布于 2024-08-02 04:30:52 字数 184 浏览 2 评论 0原文

我有一个 Web 服务,在其中创建了自定义异常。假设此异常的名称是 InvalidContractException。

我想做的是,如果发生特定步骤,我想抛出此异常。但是,我无法弄清楚客户端如何捕获 InvalidContractException 以便正确处理它。

这是一个用 C# 编写的 ASP.Net Web 服务

I have a web service in which I have created a custom exception. Let's say the name of this exception is InvalidContractException.

What I would like to do is if a specific step occurs, I want to throw this exception. However, I can't figure out how the client would catch the InvalidContractException in order to handle it properly.

This is an ASP.Net Web Service written in C#

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

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

发布评论

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

评论(3

动听の歌 2024-08-09 04:30:52

您不能这样做:

  1. Web 服务会出现 SOAP 错误。异常是特定于平台的。
  2. 当 ASMX Web 服务中未处理异常时,.NET 会将其转换为 SOAP 错误。异常的详细信息未序列化。
  3. 在 ASMX 客户端中,SOAP 错误将被转换为 SoapException。

ASMX Web 服务没有对​​ SOAP 错误提供适当的支持。除了客户端的 SoapException 之外,没有其他方法可以获取任何异常。

升级到 WCF 的另一个原因。


作为使用 ASMX 不能执行的操作的示例,以下是 WCF 的工作原理。 WCF 允许您为每个 Web 服务操作指定它可以返回哪些错误:

[ServiceContract]
public interface IMyServiceContract
{
    [FaultContract(typeof(IntegerZeroFault))]
    [FaultContract(typeof(SomeOtherFault))]
    [OperationContract]
    public string GetSomeString(int someInteger);
}

[DataContract]
public class IntegerZeroFault
{
    [DataMember]
    public string WhichInteger {get;set;}
}

[DataContract]
public class SomeOtherFault
{
    [DataMember]
    public string ErrorMessage {get;set;}
}

public class MyService : IMyServiceContract
{
    public string GetSomeString(int someInteger)
    {
        if (someInteger == 0) 
            throw new FaultException<IntegerZeroFault>(
                new IntegerZeroFault{WhichInteger="someInteger"});
        if (someInteger != 42)
            throw new FaultException<SomeOtherFault>(
                new SomeOtherFault{ErrorMessage ="That's not the anaswer"});
        return "Don't panic";
    }
}

例如,WCF 客户端可以捕获 FaultException。当我使用 Java 客户端尝试此操作时,它能够捕获 SomeOtherFault,它是 IBM Rational Web Developer 创建的从 Java Exception 类派生的。

You can't do this:

  1. Web services do SOAP Faults. Exceptions are platform-specific.
  2. When an exception is unhandled in an ASMX web service, .NET will translate it into a SOAP Fault. The details of the exception are not serialized.
  3. In an ASMX client, a SOAP fault will be translated into a SoapException.

ASMX web services do not have proper support for SOAP Faults. There is no way to get any exception other than a SoapException on the client side.

Yet another reason to upgrade to WCF.


As an example of what you can't do with ASMX, here's how WCF works. WCF allows you to specify, for each web service operation, which faults it can return:

[ServiceContract]
public interface IMyServiceContract
{
    [FaultContract(typeof(IntegerZeroFault))]
    [FaultContract(typeof(SomeOtherFault))]
    [OperationContract]
    public string GetSomeString(int someInteger);
}

[DataContract]
public class IntegerZeroFault
{
    [DataMember]
    public string WhichInteger {get;set;}
}

[DataContract]
public class SomeOtherFault
{
    [DataMember]
    public string ErrorMessage {get;set;}
}

public class MyService : IMyServiceContract
{
    public string GetSomeString(int someInteger)
    {
        if (someInteger == 0) 
            throw new FaultException<IntegerZeroFault>(
                new IntegerZeroFault{WhichInteger="someInteger"});
        if (someInteger != 42)
            throw new FaultException<SomeOtherFault>(
                new SomeOtherFault{ErrorMessage ="That's not the anaswer"});
        return "Don't panic";
    }
}

A WCF client can then catch FaultException<SomeOtherFault>, for instance. When I've tried this with a Java client, it was able to catch SomeOtherFault, which IBM Rational Web Developer created to derive from the Java Exception class.

掩于岁月 2024-08-09 04:30:52

如果您的目标是抛出异常,以便用户知道出了问题、发生了某些特定异常并对其进行特殊处理,那么您确实有一个选择。您只需依赖客户端代理的正确实现(实际上,一种选择是您自己提供客户端代理)。

此处有一篇有用的文章,介绍如何< code>SoapException 的工作。

本质上,它归结为在 SoapException 的详细信息节点中对异常详细信息(例如错误代码)进行编码,然后在重新抛出异常之前在客户端对其进行解析。

没有办法重新创建异常,就好像它是跨服务边界抛出的一样,并且没有自动的方法来获取除了客户端的 SoapException 之外的任何内容。

If your goal is to throw an exception so that a user can know that something went wrong, some specific exception occurred and have special handling for it, you do have an option. You just have to rely on a correct implementation of the client proxy (Indeed, one option is to provide the client proxies yourself).

There's a useful article availabe here about how SoapExceptions work.

Essentially, it comes down to encoding your exception details (eg an error code) in the detail node of the SoapException, and then parsing it on the client side before rethrowing the exception.

There's no way to recreate the exception as if it were thrown across the service boundary, and there's no automagical way to get anything except for a SoapException on the client side.

纵性 2024-08-09 04:30:52

在对抛出错误的 WCF 服务的客户端调用中,您确实

尝试 ..

catch failure as FaultException(Of YourService.FooBarFault)

end try

您必须为可以生成错误的服务方法声明一个FaultContract,例如

_

 <FaultContract(GetType(FooBarFault))> _

 Function yourCall

In your client call to the WCF service that throws the Fault, you do

try ..

catch fault as FaultException(Of YourService.FooBarFault)

end try

You have to declare a FaultContract for the service method that can generate the fault, e.g.

_

 <FaultContract(GetType(FooBarFault))> _

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