ASP.NET 单声道 : How to send SOAP instead of HTML?

标签 asp.net html soap mono webmethod

我有一个基于在 Mono 之上构建的 SOAP 的 ASP.NET Web 服务。 如果我在服务内部抛出异常,异常仍然在 HTTP+HTML 级别。我想做的是始终将所有异常作为 SOAP 响应发送,即我没有任何正常的 aspx 页面(一切都应该使用 SOAP)。

我已经尝试在 Application_Error() 方法中处理 Global.asax.cs 文件中的所有异常,但它似乎总是以 HTML 格式发送异常。 我看到的是一般的 ASP.NET 错误页面。

当指向 HTML 时,我的 SOAP 客户端通知我它无法解析 HTML。

在没有抛出异常的情况下,从服务器发送 SOAP 效果很好。

我研究了各种网络资源,了解到 Application_Error 不应该用于来自该资源的 SOAP 异常处理: Handling and Throwing Exceptions in XML Web Services

我必须实现自己的 HTTP 模块还是 ExceptionUtility Class还是 HTTP 处理程序?

我在我的开发机器上运行这个: 版本信息:Mono 运行时版本:2.10.5 (Debian 2.10.5-1); ASP.NET 版本:4.0.30319.1

我正在 Ubuntu 11.10 中使用 MonoDevelop 的内置 xsp HTTP 服务器对此进行测试。

这是我的测试代码:

Global.asax.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Web.Services.Protocols;

namespace SoapTaikina
{
    public class Global : System.Web.HttpApplication
    {
        protected virtual void Application_Start (Object sender, EventArgs e)
        {
            Backend.Initialize();
        }

        protected virtual void Application_Error (Object sender, EventArgs e)
        {
            // This doesn't appear to be executed when Foo() throws an exception.
            // I cannot catch the Foo() constructor exception here.
            throw new SoapException("This is never reached.", null);
        }

        // These are not used in this example.
        protected virtual void Session_Start (Object sender, EventArgs e) { }
        protected virtual void Application_BeginRequest (Object sender, EventArgs e) { }
        protected virtual void Application_EndRequest (Object sender, EventArgs e) { }
        protected virtual void Application_AuthenticateRequest (Object sender, EventArgs e) { }
        protected virtual void Session_End (Object sender, EventArgs e) { }
        protected virtual void Application_End (Object sender, EventArgs e) { }
    }
}

Foo.asmx.cs:

using System;
using System.Web;
using System.Web.Services;

namespace SoapTaikina
{
    public class Foo : System.Web.Services.WebService
    {
        public Foo()
        {
            // This may throw an Exception which will not be caught in
            // Application_Error().
            //
            // This is the problem spot.
            Backend2.InitializeMoreStuff();
        }

        [WebMethod]
        public bool DoStuff() {
            // This works fine and returns a SOAP response.
            return true;
        }
    }
}

最佳答案

一、理论

根据 .NET Framework 的要求,Application_Error 永远不会被触发。这是因为运行页面的管道与运行 Web 服务的管道不同。另外,请注意在 Application_Error 中抛出异常是一个非常糟糕的主意。

我发现从浏览器测试 Web 服务(其中,可能因为,接受 header 未设置为 application/soap+xml 而是设置为 text/html) 显示纯文本消息。如果客户端是 SOAP 代理(即您在 Visual Studio/MonoDevelop 中从 Web 服务的 WSDL 生成)并继承自 SoapHttpClientProtocol,则预期异常总是作为SOAP fault<抛出.

第二,练习。

您从 Application_Start 执行 var f = new Foo()。这是错误的,因为 Web 服务骨架类是在每个 HTTP/SOAP 请求上全新实例化的,并且永远不应在特殊的 Application_Start 方法中初始化,该方法在第一个请求上运行并且没有请求本身正在处理中。此外,您应该避免在网络服务的构造函数中做复杂的事情(可能会抛出异常)。这只是一个糟糕的设计,而不是一个无法编译或无法工作的解决方案。

您出现问题的原因可能是 ASP.NET 管道从未达到将请求映射到 Web 服务处理程序而不是页面处理程序的程度,从而触发默认行为。试图在单声道存储库中找到代码,没有运气:)

我建议你先删除Application_Start中的任何WS初始化,如果你说你真的不需要它,扔掉Application_Error

关于ASP.NET 单声道 : How to send SOAP instead of HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9378451/

相关文章:

java - 是否可以同时提交两个表格?

html - 一个 anchor 中的多种下划线颜色

web-services - 使用 Coldfusion 与 Bullhorn SOAP web 服务 API 集成

c# - WCF 中的 SOAP 消息反序列化问题 - 字段具有空值

java - 如何从 WSDL 创建包含多个服务的 WebService?

c# - 从数据库中检索数据的高级方法

c# - 将子类别的 Microsoft Chart Control X 轴标签格式化为类似于 Excel 中生成的图表

facebook - 如何以编程方式将图片上传到 Facebook?

HTML CSS - Angular 对象具有不锐利的边缘

javascript - 将文本框中输入的文本附加到 url 并打开 url 链接