You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何通过SOAP请求使用DIME实现WCF文件附件上传?

使用WCF-WSDL结合DIME协议实现文件附件上传

问题描述

我对该主题较为陌生,表述可能不够清晰。当前我使用WCF-WSDL向端点上传文件,希望采用DIME协议实现文件附件功能。使用DIME需获取请求SOAP上下文,我已通过soapUI查看到所需的SOAP请求结构,现寻求如何利用该SOAP请求实现DIME附件上传的技术帮助。

现有示例代码

SoapContext reqContext = objCA SDM_WS.RequestSoapContext
DimeAttachment dimeAttach = New DimeAttachment("image/gif",
TypeFormat.MediaType, "c:\test.txt")
reqContext.Attachments.Add(dimeAttach)

strResult = SDM_WS.createAttachment(sid, "doc_rep:1002",
"cr:400001", "my desc", "c:\test.txt")

示例SOAP请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://www.ca.com/UnicenterServicePlus/ServiceDesk">
                       <soapenv:Header/>
                        <soapenv:Body>
                            <ser:createAttachment>
                                <sid>654741903</sid>
                                <repositoryHandle>doc_rep:1002</repositoryHandle>
                                <objectHandle>cr:635354</objectHandle>
                                <description>TEST</description>
                                <fileName>TEST</fileName>
                             </ser:createAttachment>
                          </soapenv:Body>
                       </soapenv:Envelope>

实现方案

1. 配置支持DIME的WCF绑定

DIME是早期SOAP附件协议,需确保WCF客户端绑定启用流式传输并兼容SOAP 1.1,可通过自定义绑定实现:

<!-- 配置文件中添加绑定 -->
<bindings>
  <customBinding>
    <binding name="DimeBinding">
      <textMessageEncoding messageVersion="Soap11"/>
      <httpTransport transferMode="Streamed" />
    </binding>
  </customBinding>
</bindings>

将客户端端点绑定指定为该自定义绑定。

2. 修正SOAP上下文与附件添加逻辑

原示例代码存在语法错误,WCF中需通过OperationContextScope管理请求上下文,正确添加DIME附件的代码如下(C#):

using (var operationScope = new OperationContextScope(SDM_WS.InnerChannel))
{
    // 创建DIME附件,媒体类型需匹配文件实际类型(如text/plain对应txt文件)
    var dimeAttachment = new DimeAttachment("text/plain", TypeFormat.MediaType, @"c:\test.txt");
    
    // 将附件添加到请求消息属性
    var attachmentProperty = new AttachmentMessageProperty();
    attachmentProperty.Attachments.Add(dimeAttachment);
    OperationContext.Current.OutgoingMessageProperties.Add(AttachmentMessageProperty.Name, attachmentProperty);

    // 调用服务方法,fileName参数只需传入文件名(无需路径)
    string result = SDM_WS.createAttachment(sid, "doc_rep:1002", "cr:400001", "my desc", "test.txt");
}

3. 匹配SOAP请求参数注意事项

  • fileName字段仅需传入文件名(如示例中的"TEST"),文件内容通过DIME附件传输,服务端会自动关联文件名与附件内容。
  • sid、repositoryHandle、objectHandle需与服务端会话及资源定位信息完全一致,否则会导致上传失败。

4. 调试验证

  • 使用WCF跟踪工具捕获客户端发送的请求,检查DIME附件是否正确嵌入SOAP消息中。
  • 对比soapUI中的请求结构,确认参数与附件格式匹配,若服务端返回错误,优先排查服务端是否支持DIME协议及绑定配置。

内容的提问来源于stack exchange,提问作者elhanan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 17:40:32