【问题标题】:ASMX Web Service: How to throw soap exception from asmx service which i like to capture from client sideASMX Web 服务:如何从我喜欢从客户端捕获的 asmx 服务中抛出肥皂异常
【发布时间】:2016-12-30 15:59:35
【问题描述】:

我想如果客户端发送错误的凭据然后服务抛出肥皂异常,但我尝试但仍然没有运气。

从这里查看我更新的代码https://github.com/karlosRivera/EncryptDecryptASMX

任何人都可以下载我的代码并在他们的 PC 上运行以捕获问题。

查看该区域

[AuthExtension]
[SoapHeader("CredentialsAuth", Required = true)]
[WebMethod]
public string Add(int x, int y)
{
    string strValue = "";
    if (CredentialsAuth.UserName == "Test" && CredentialsAuth.Password == "Test")
    {
        strValue = (x + y).ToString();
    }
    else
    {
        throw new SoapException("Unauthorized", SoapException.ClientFaultCode);

    }
    return strValue;
}

对于这一行throw new SoapException("Unauthorized", SoapException.ClientFaultCode);

我从我的 soapextension 进程消息函数中看到的响应 XML 正文没有发生变化。

所以我现在有两个问题

1) 我想要throw SoapException 来自需要更改soap 响应的服务。

2) 从客户端我需要赶上SoapException

请从链接中查看我的最新代码并告诉我要更改的内容。谢谢

【问题讨论】:

    标签: c# asmx soapexception soap-extension


    【解决方案1】:

    考虑迁移和使用 WCF。这是 WCF 错误。

    您可以使用SoapExtensions.AfterSerializeBeforeSerialze 方法替换错误消息或处理错误。

    Source

    【讨论】:

    • 您能否发布任何示例代码以使用soapExtensions.AfterSerialize or BeforeSerialze methods. 向客户端返回错误消息@请提出解决方案。谢谢,新年快乐:)
    • 这里:public override void ProcessMessage(SoapMessage message) { if (message.Exception != null && message.Stage == SoapMessageStage.BeforeSerialize) { if (message.Action.EndsWith("YourAction", StringComparison.InvariantCultureIgnoreCase) && message.Exception.InnerException is InvalidOperationException) { if (message.Exception.InnerException.InnerException is FormatException) { var ne = new SoapException("Custom Error Message", message.Exception.Code, message.Exception.InnerException); message.Exception = ne; } }
    【解决方案2】:

    另一种选择是避免发送SoapExceptions,但更复杂的对象嵌入了错误语义。例如

    [Serializable]
    class Result<T>
    {
        public bool IsError { get; set; }
        public string ErrorMessage { get; set; }
    
        public T Value { get; set; }
    }
    

    在这种情况下,您的方法可能如下所示:

    [AuthExtension]
    [SoapHeader("CredentialsAuth", Required = true)]
    [WebMethod]
    public Result<string> Add(int x, int y)
    {
        string strValue = "";
        if (CredentialsAuth.UserName == "Test" && CredentialsAuth.Password == "Test")
        {
            return new Result<string> { Value = (x + y).ToString() };
        }
        else
        {
            return new Result<string> { IsError = true, ErrorMessage = $"Unauthorized -  {SoapException.ClientFaultCode}" };
        }
    }
    

    Result 可以开发为包含一组字段/输入错误,以返回与错误输入参数值相关的更精确的错误消息。

    【讨论】:

    • 为什么你使用 Result 和发送 Result { Value = (x + y).ToString() };难道我们不能写像 return new Result { Value = (x + y).ToString() }; 这样的代码吗? ?
    • 新年快乐 :)
    • 不,必须在此上下文中提供类型。如果Result&lt;T&gt; 的构造函数采用 T 参数 (Result&lt;T&gt;(T initialValue)),则可以通过类型推断进行简化。
    • 抱歉,不清楚为什么 Result 类必须是泛型类型?
    • 泛型允许使用实际类型,因此在某些情况下编译时错误而不是运行时错误的优势。我刚刚测试过,构造函数不会推断类型(我的错)。 Eric Lippert here 充分解释了这一点。
    猜你喜欢
    • 2010-12-20
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    相关资源
    最近更新 更多