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

时间:2022-08-26 18:49:00

In WCF, contracts can be switched to streaming mode, to transfer large messages.

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

After reading and testing it seems to me, that streaming mode can not be used with duplex channels (channels with OneWay-calls and a callback interface).

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

Is this so? Do duplex and streaming can not be used with each other? Or is there a way?

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

(I'm trying to upload a large file to the service and use callback to report progress on this)

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

2 个解决方案

#1


to load the file from the client to the server, you can use the following code: service

要将文件从客户端加载到服务器,可以使用以下代码: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();
            }
        }}

client:

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>

is always better to pass an array of bytes. I think, not need describe the callback functions?

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

#2


Out of curiosity I was about to start some testing on your question but then Google revealed to me the two samples which might answer your question better.

出于好奇,我即将开始对你的问题进行一些测试,但谷歌向我展示了两个可能更好地回答你问题的样本。

This CodeProject example shows streaming file transfers with a progress bar without using Duplex channels.

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

This sample shows more of the same but with some different disposing of the stream.

此示例显示了更多相同但具有一些不同的流处理。

Also, a really good resource for all things WCF related is iDesgin.net. The main guy there is Juval Lowy who wrote some of the best books regarding WCF. They have dozens of excellent WCF examples you can download (although they annoyingly ask your for your email address for each one). More importantly, they also wrote a ServiceProcessEx class which greatly extends what ServiceProcess can do, especially in regards to Duplex channels. (I'm not sure if it deals much with streaming though... its not something I've done yet).

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

Hope some of this is helpful to you.

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

#1


to load the file from the client to the server, you can use the following code: service

要将文件从客户端加载到服务器,可以使用以下代码: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();
            }
        }}

client:

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>

is always better to pass an array of bytes. I think, not need describe the callback functions?

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

#2


Out of curiosity I was about to start some testing on your question but then Google revealed to me the two samples which might answer your question better.

出于好奇,我即将开始对你的问题进行一些测试,但谷歌向我展示了两个可能更好地回答你问题的样本。

This CodeProject example shows streaming file transfers with a progress bar without using Duplex channels.

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

This sample shows more of the same but with some different disposing of the stream.

此示例显示了更多相同但具有一些不同的流处理。

Also, a really good resource for all things WCF related is iDesgin.net. The main guy there is Juval Lowy who wrote some of the best books regarding WCF. They have dozens of excellent WCF examples you can download (although they annoyingly ask your for your email address for each one). More importantly, they also wrote a ServiceProcessEx class which greatly extends what ServiceProcess can do, especially in regards to Duplex channels. (I'm not sure if it deals much with streaming though... its not something I've done yet).

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

Hope some of this is helpful to you.

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