4

在 WCF 中,合约可以切换到流模式,以传输大消息。

在阅读和测试之后,在我看来,流模式不能用于双工通道(带有 OneWay 调用和回调接口的通道)。

是这样吗?双工和流媒体不能互相使用吗?或者有什么办法吗?

(我正在尝试将一个大文件上传到服务并使用回调报告进度)

4

2 回答 2

5

要将文件从客户端加载到服务器,可以使用以下代码:服务

 [ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IFreeBoxServiceCallBack))]
    public interface IFreeBoxService
    {
        [OperationContract(IsOneWay = true)]
        void sendFile(byte[] bytes, int offset, int count);

        [OperationContract(IsOneWay = true)]
        void sendFileName(string fileName);
    }

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)]
    public class FreeBoxService:IFreeBoxService
    {
        string path = "D:\\repository\\";
        string fileName = "";
        IFreeBoxServiceCallBack callBack = null;

        public FreeBoxService()
        {
            callBack = OperationContext.Current.GetCallbackChannel<IFreeBoxServiceCallBack>();
        }

        public void sendFileName(string fileName)
        {
            this.fileName = fileName;
        }

        public void sendFile(byte[] bytes, int offset, int count)
        {
            using (FileStream fileStream = new FileStream(path + fileName, FileMode.Append, FileAccess.Write))
            {
                fileStream.Write(bytes, offset, count);
                fileStream.Close();
                fileStream.Dispose();
            }
        }}

客户:

private void sendFileToServer()
        {
            FileStream fs = new FileStream(fullName,FileMode.Open,FileAccess.Read);
            int bytesRead = 0;
            bytes = new byte[15000];

            proxy.sendFileName("someFile.xml");

            while ((bytesRead = fs.Read(bytes, 0, 15000))>0)
            {
                proxy.sendFile(bytes, 0, bytesRead);
            }
            fs.Close();
            fs.Dispose();
        }

.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="FreeBoxServiceLib.FreeBoxService" behaviorConfiguration="MyServiceBehevior">
        <endpoint address="" contract="FreeBoxServiceLib.IFreeBoxService"
                  binding="netTcpBinding">
        </endpoint>
        <endpoint address="MEX" binding="mexTcpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8080/FreeBoxService/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehevior">
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

传递字节数组总是更好。我想,不需要描述回调函数吗?

于 2013-11-23T12:43:31.570 回答
2

出于好奇,我正要开始对您的问题进行一些测试,但随后 Google 向我透露了可能更好地回答您的问题的两个样本。

此 CodeProject 示例显示了带有进度条的流式文件传输,而不使用双工通道。

此示例显示了更多相同的内容,但对流的处理方式有所不同。

此外,与 WCF 相关的所有事情的一个非常好的资源是iDesgin.net。主要人物是 Juval Lowy,他写了一些关于 WCF 的最佳书籍。他们有几十个优秀的 WCF 示例可供您下载(尽管他们烦人地要求您提供每个示例的电子邮件地址)。更重要的是,他们还编写了一个 ServiceProcessEx 类,它极大地扩展了 ServiceProcess 的功能,尤其是在双工通道方面。(我不确定它是否与流媒体有很多关系……我还没有做过)。

希望其中一些对您有所帮助。

于 2009-01-17T16:44:25.370 回答