0

我们有这样的 JAX-WS Web 服务:

public class NamedDataHandlerContainer {
    public String options; // format is option1_name=option1_value;option2_name=option2_value
    @XmlMimeType("application/octet-stream") public DataHandler dataHandler;
}

@WebService
public interface mtomserver {
    @WebMethod public int saveFile(String name,
    @XmlMimeType("application/octet-stream") List<NamedDataHandlerContainer> contents,
    @XmlMimeType("application/octet-stream") @WebParam(mode = WebParam.Mode.OUT) Holder<List<NamedDataHandlerContainer>> results);
}

当使用 .NET 4.0 的 SvcUtil 处理该 Web 服务的 WSDL 时,它会为 NamedDataHandlerContainer.dataHandler 生成 byte[] 类型:

public partial class namedDataHandlerContainer;
{
    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    public string options;

    [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "base64Binary", Order = 1)]
    public byte[] dataHandler;
}

但是,在 App.config 中,它会生成 Mtom 工件:

<basicHttpBinding>
    <binding name="mtomserverImplPortBinding" messageEncoding="Mtom" maxReceivedMessageSize="1000000000" />
</basicHttpBinding>

(我们添加了 maxReceivedMessageSize 以允许大型附件)。事实上,WCF 客户端向服务发送 MTOM 附件 - 我们正在转储 HTTP 有效负载并确认:

--uuid:394d798b-e43e-47cc-82dd-64e32ef51edd+id=1
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><saveFile xmlns="http://wsserver.mtomtest/"><arg0 xmlns="">myfile.bin</arg0><arg1 xmlns=""><options>my options from .NET</options><dataHandler><xop:Include href="cid:http://tempuri.org/1/634993057692269386" xmlns:xop="http://www.w3.org/2004/08/xop/include"/></dataHandler></arg1></saveFile></s:Body></s:Envelope>
--uuid:394d798b-e43e-47cc-82dd-64e32ef51edd+id=1
Content-ID: <http://tempuri.org/1/634993057692269386>
Content-Transfer-Encoding: binary
Content-Type: application/octet-stream

<binary content goes here>

JAX-WS 可以成功地将流应用到此类有效负载。但是,有没有办法在 .NET 端实现流式传输?我已阅读 MSDN,其中明确表示可能只存在一个启用流式传输的参数。但是,有没有办法让自定义消息序列化程序(或自定义的东西,我不是 WCF 专家)并且仍然避免将整个有效负载加载到内存中。

4

1 回答 1

0

WCF 具有启用流式传输的配置。您不需要编写任何额外的代码来实现这一点。

<basicHttpBinding>
    <binding name="mtomserverImplPortBinding" messageEncoding="Mtom" maxReceivedMessageSize="1000000000" transferMode="Streamed"/>
</basicHttpBinding>

来源:http: //msdn.microsoft.com/en-us/library/ms789010.aspx

于 2013-06-12T18:01:15.377 回答