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

发布于 2024-07-11 03:37:58 字数 168 浏览 7 评论 0原文

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

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

是这样吗? 双工和流式不能同时使用吗? 或者有什么办法吗?

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

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

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).

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-07-18 03:37:58

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

始终最好传递字节数组。 我想,回调函数不需要描述吧?

to load the file from the client to the server, you can use the following code: 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?

说不完的你爱 2024-07-18 03:37:58

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

此 CodeProject 示例显示了在不使用双工通道的情况下使用进度条进行流式文件传输。

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

另外,关于 WCF 相关所有内容的一个非常好的资源是 iDesgin.net< /a>. 其中的主要人物是 Juval Lowy,他撰写了一些有关 WCF 的最佳书籍。 他们有数十个优秀的 WCF 示例可供您下载(尽管他们会烦人地要求您提供每个示例的电子邮件地址)。 更重要的是,他们还编写了一个 ServiceProcessEx 类,该类极大地扩展了 ServiceProcess 的功能,特别是在双工通道方面。 (我不确定它是否涉及流媒体,但我还没有做过)。

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

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.

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).

Hope some of this is helpful to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文