【问题标题】:WCF: is it possible to use streaming mode in a duplex channel?WCF:是否可以在双工通道中使用流模式?
【发布时间】:2010-09-30 01:15:10
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: wcf streaming duplex


    【解决方案1】:

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

     [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>
    

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      希望本文对您有所帮助。

      【讨论】:

      • 第一个(CodeProject)没有引用双工绑定第二个两个链接断开
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 2014-08-14
      • 2010-10-11
      相关资源
      最近更新 更多