【问题标题】:C# client of Axis2 web service complains "but expected 'text/xml'"Axis2 Web 服务的 C# 客户端抱怨“但应为 'text/xml'”
【发布时间】:2010-12-18 12:48:26
【问题描述】:

我的 C# 示例客户端 ASP.NET 程序在我的 Axis2 服务器中成功运行了一个调用,但客户端似乎不喜欢响应。

我明白了:

客户端发现响应内容类型为'multipart/related; 边界=MIMEBoundaryurn_uuid_38D413ACFC9D56F28E1258666845186; 类型=“应用程序/xop+xml”; start=""; start-info="text/xml"',但应为 'text/xml'。

根据 MSDN 论坛,我认为我必须启用 MTOM,但他们只对现已过时的 WSE 3 软件包进行了解释。

在 WCF 空间中,对于 C# 中的 ASP.NET 程序,如何启用 MTOM 或以其他方式修复此响应内容类型不匹配?实际上,接下来我需要 MTOM。

【问题讨论】:

    标签: c# wcf axis2 mtom


    【解决方案1】:

    一方面,您还必须在 Axis2 中启用 MTOM。找到您的axis2.xml配置文件(WEB-INF/conf/axis2.xml)并调整以下设置:

    <axisconfig name="AxisJava2.0">
        <!-- ================================================= -->
        <!-- Parameters -->
        <!-- ================================================= -->
        .../...
        <parameter name="enableMTOM">true</parameter>
        .../...
    </axisconfig>
    

    没有这个,Axis 根本不会处理 MTOM,客户端会很困惑。

    切换到 XOP/MTOM 也意味着切换到 multipart-mime,并且您的客户端实际上得到了 multipart-mime 的答案,所以我认为 Axis2 设置毕竟是可以的 :) 您的客户端期望纯 XML(即良好的 SOAP 响应)表明您尚未在客户端设置 MTOM。

    假设您使用的是 BasicHttpBinding,在 WCF 中启用 MTOM 可以这样完成:

    <configuration>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="MySOAP11Binding" 
                             ... 
                             messageEncoding="Mtom"
                             ...
                    >
                    .../...
                    </binding>
                </basicHttpBinding>
                .../...
    

    您肯定还必须调整绑定元素的 maxBufferSize、maxBufferPoolSize 和 maxReceivedMessageSize 属性。

    或者,您可以在代码中进行设置:

    private ServiceProxy<MyPortTypeClient, MyPortType> getClient()
    {
        EndpointAddress endpoint = new EndpointAddress("http://server/axis/services/My");
    
        // The binding
        BasicHttpBinding binding = new BasicHttpBinding();
        binding.OpenTimeout = minutes(1);
        binding.CloseTimeout = minutes(1);
        binding.SendTimeout = minutes(10);
        binding.ReceiveTimeout = minutes(10);
    
        binding.MaxBufferPoolSize = Int32.MaxValue;
        binding.MaxReceivedMessageSize = Int32.MaxValue;
        binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
    
        binding.MessageEncoding = WSMessageEncoding.Mtom;
        if (binding is BasicHttpBinding)
        {
            // Also setting to streamed mode
            ((BasicHttpBinding)(Object)binding).TransferMode = TransferMode.Streamed;
        }
    
        binding.AllowCookies = true;
    
        // MyPortType and MyPortTypeClient are implemented in Reference.cs, i.e. this
        // code is generated by svcutil or Visual Studio from your WSDL.
        MyPortTypeClient _proxy = new MyPortTypeClient(binding, endpoint);
        ServiceProxy<MyPortTypeClient, MyPortType> proxy = new ServiceProxy<MyPortTypeClient, MyPortType>(_proxy);
    
        if (!String.IsNullOrEmpty(wsUsername) && !String.IsNullOrEmpty(wsPassword))
        {
            UserNamePasswordClientCredential credentials = _proxy.ClientCredentials.UserName;
            credentials.UserName = wsUsername;
            credentials.Password = wsPassword;
        }
        return proxy;
    }
    

    在代码中执行此操作的好处是,您将从 IDE 中获得有关可以为任何特定绑定设置哪些参数的帮助。如果您从 BasicHttpBinding 切换到 WSHttpBinding,您将收到与新绑定不匹配的参数的编译错误。

    【讨论】:

      【解决方案2】:

      这通常是客户端期望一个 xml 响应,但从服务器收到一条它无法解析的错误消息。

      要么记录响应,要么使用网络嗅探器(fiddler)来检查你得到了什么。

      【讨论】:

      • 回复被客户端拒绝。从其他地方的一些帖子中,我看到我需要启用 MTOM。 “Learning WCF”的第 223 页显示了一个“”来执行此操作,但“添加服务引用”不会像有时那样创建 app.conf 文件,我不知道该去哪里。
      猜你喜欢
      • 2010-11-05
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多