2007年5月24日星期四

关于 使用 WSE 3.0 开启MTOM 后,产生HTTP 415 错误的解决方法

    这个文档使用 Google文件发布,如有浏览问题,请浏览http://docs.google.com/Doc?id=dggdkn6_17cthdx3

    最近,在写程序的时候为了大数据传输,而使用的WSE 但使用之后,在客户端调用出现一下问题

    请求因 HTTP 状态 415 失败: Unsupported Media Type。

    如果,你是先配置好服务器后,在VS中直接就用添加Web Services了,然后就出问题了,那么下面告诉你解决方法

    其实,我们都丢了一步, 直接使用VS生成的代理,没有启动 WSE功能,更没有 MTOM,所以报错了

    创建支持WSE代理很简单,

    打开客户端程序,使用 WSE 配置工具,打开配置文件(App.config),选上Enable this project for Web Services Enhancements

    (不知道直接在VS里右键单击工程,选择 WSE Settings 3.0 会不会自动添加 Microsoft.web.services3,我这里是装完VS SP1后才按的WSE,所以,没有这个东西)

    之后,在向项目里面添加Web服务的引用,系统会自动生成2个代理类

    Public Class Service1

    Inherits System.Web.Services.Protocols.SoapHttpClient ---->VS默认生成的

    Public Class Service1Wse

    Inherits Microsoft.Web.Services3.WebServicesClientProtocol ——>> 支持 WSE

    但是,如果要这么使用,还会有问题

    还需要使用WSE配置工具,打开客户端的配置文件,在 Messaging 选项卡下 Client Mode 设成 On

    这样就可以使用了

    如果数据很大(>4M 那么还需要 更改 配置文件,加上<maxMessageLength value="-1" />

    <microsoft.web.services3>

    <messaging>

    <maxMessageLength value="-1" />

    <mtom serverMode="optional" clientMode="On" />

    </messaging>

    </microsoft.web.services3>


    下面,说说Web 服务的配置

    VS里打开Web Services,右键单击工程,选择WSE Settings 3.0 ,启动WSE配置工具 ,(好像安装完VS SP1之后,这个东西就不会被装上),在General 选项卡里的 2 enable 选上

    之后在Messaging 选项卡,开启 Server Mode optional always

    optional 不管请求是否使用MTOM,都是用MTOM响应

    always 请求和响应 都是MTOM

    之后,OK,就可以了

    这个过程主要在 Web.config中产生

    <microsoft.web.services3>

    <messaging>

    <mtom serverMode="optional" clientMode="On" />

    </messaging>

    </microsoft.web.services3>

    如果传输数据大于4M那么就需要更改HTTP Runtime 设置 默认设置

    <system.web>

    <httpRuntime maxRequestLength="409600" executionTimeout="300" />

    </system.web>

    PS. 官方事例里配置是

    <configuration>

    <system.web>

    <httpRuntime maxMessageLength="409600"

    executionTimeoutInSeconds="300"/>

    </system.web>

    </configuration>

    HttpRuntime 里没有 MaxMessageLength 也没有 那个ExecutionTimeoutinSeconds


    之后更改消息的大小,设置 maxMessageLength value="-1"

    <configuration>

    <microsoft.web.services3>

    <messaging>

    <maxMessageLength value="-1" />

    </messaging>

    </microsoft.web.services3>

    </configuration>

    官方事例链接

    How to: Enable a Web Service to Send and Receive Large Amounts of Data

    How to: Send and Receive Large Amounts of Data to and from a Web Service

    How to: Create a Proxy Class to Communicate with a Web Service

没有评论: