WCF:在双工通道中是否可以使用流模式?

4
在WCF中,合同可以切换到流模式,以传输大型消息。
经过阅读和测试,似乎流模式不能与双工通道(具有单向调用和回调接口的通道)一起使用。
这是真的吗?双工和流不能相互使用吗?还是有办法?
(我正在尝试上传一个大文件到服务,并使用回调报告进度)
2个回答

5

要将文件从客户端加载到服务器,您可以使用以下代码: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
出于好奇,我本来想在你的问题上进行一些测试,但是谷歌向我展示了两个可能更好地回答你的问题的示例。 这个CodeProject示例展示了不使用Duplex通道的流文件传输和进度条。 这个示例也展示了类似的内容,但是对流的处理有所不同。
此外,关于WCF相关的所有事情,一个真正好的资源是iDesgin.net。那里的主要人物是Juval Lowy,他写了一些最好的关于WCF的书籍。他们有数十个可以下载的优秀WCF示例(尽管他们会烦人地要求你为每个示例提供电子邮件地址)。更重要的是,他们还编写了一个ServiceProcessEx类,大大扩展了ServiceProcess的功能,特别是关于Duplex通道。(不过我不确定它是否涉及到流...这不是我做过的事情)。
希望这些对你有所帮助。

4
第一个链接(CodeProject)没有提及双向绑定。后面两个链接已经失效。 - Robert MacLean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接