【问题标题】:File Transferring through WCF通过 WCF 传输文件
【发布时间】:2015-02-13 22:58:14
【问题描述】:

考虑以下合同

[OperationContract]
[FaultContract(typeof(CcmFaultException))]
void UploadStream(DataFileStream input);`

[MessageContract]
public class DataFileStream
{
    [MessageHeader(MustUnderstand = true)]
    public String FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public long FileSize { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream StreamData { get; set; }
}

我确实有两个主要问题。

第一个问题是在服务端,我无法更改UploadStream 签名,当我更改接口和相关类中的方法签名时(例如添加返回类型或更多输入参数),我可以构建服务但是当我运行主机应用程序出现以下错误:

[System.InvalidOperationException]
UploadStream 操作具有参数或返回类型,该类型使用 MessageContractAttribute 进行属性化。
为了使用消息契约来表示请求消息,操作必须有一个属性为 ...

另一个问题是在客户端,即使我使用UploadStream(DataFileStream data) 合约 我可以创建DataFileStream 类的对象,但是当我想调用UploadStream 方法时,我无法传递该对象!我看到的唯一选项是客户端的以下签名:

UploadStream(string FileName, long FileSize, Stream StreamData);

请你给我一个解决这两个问题的方法。

谢谢

P.S:我正在使用以下配置:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="CCMService.Binding_ICcmWcfService" closeTimeout="00:10:00"
        openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
        maxReceivedMessageSize="2147483647">
        <reliableSession ordered ="false" inactivityTimeout ="00:05:00"
        enabled ="true" />
        <security mode="TransportWithMessageCredential">
          <transport clientCredentialType="None" proxyCredentialType="None"
           realm="" />
          <message clientCredentialType="UserName" 
                   negotiateServiceCredential="true" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>

  <client>
    <endpoint address="https://localhost:8731/CCMService" 
              binding="wsHttpBinding"
      bindingConfiguration="CCMService.Binding_ICcmWcfService" 
      contract="CCMServiceRef.ICcmWcfService"
      name="CCMService.Binding_ICcmWcfService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
  </client>
</system.serviceModel>

【问题讨论】:

  • 也许可以阅读 MessageContract 与 DataContract - MessageContract 旨在具有这些约束。
  • @steve16351 : 请阅读以下帖子

标签: c# wcf file-upload


【解决方案1】:

@史蒂夫16351

在客户端应用中考虑以下实现

    private void btnImport_Click(object sender, EventArgs e)
    {
       CCMServiceRef.DataFileStream obj = new CCMServiceRef.DataFileStream()
       {
          FileName = _FileName,
          StreamData = GetStream(_FilePath)
       };
       Program.serviceManager.Client.ImportScript(obj);
    }


    private Stream GetStream(string filePath)
    {
        System.IO.MemoryStream data = new System.IO.MemoryStream();
        System.IO.Stream str = File.OpenRead(filePath);
        str.CopyTo(data);
        data.Seek(0, SeekOrigin.Begin);
        byte[] buf = new byte[data.Length];
        data.Read(buf, 0, buf.Length);
        return data;
    }

当我切换到 DataContract 程序编译并托管应用程序运行时,但当我调用 Service 方法 ( Program.serviceManager.Client.ImportScript(obj) ) 时,程序会等待几分钟,直到不活动超时引发! 事实上,Service 方法从未执行过!

【讨论】:

    猜你喜欢
    • 2013-05-26
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2015-08-04
    • 2012-04-10
    • 2013-12-17
    相关资源
    最近更新 更多