【问题标题】:WCF with Duplex Communication具有双工通信的 WCF
【发布时间】:2016-11-21 17:04:41
【问题描述】:

每当我使用窗口窗体时,它都可以正常工作,但控制台应用程序总是出错。

错误- 套接字连接被中止。这可能是由 处理您的消息时出错或超过接收超时 远程主机或底层网络资源问题。本地套接字 超时为“00:01:00”。

这是我的代码

合同

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace ClassLibrary1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IReportService" in both code and config file together.
    [ServiceContract(CallbackContract=typeof(IReportServiceCallbak))]
    public interface IReportService
    {
        [OperationContract(IsOneWay=true)]
        void ProcessReport();
    }

    public interface IReportServiceCallbak
    {
        [OperationContract(IsOneWay=true)]
        void Progress(int percentage);
    }
}

服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

namespace ClassLibrary1
{
    public class ReportService : IReportService
    {

        public void ProcessReport()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(50);
                OperationContext.Current.GetCallbackChannel<IReportServiceCallbak>().Progress(i);
            }
        }
    }
}

客户

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Threading;

namespace DuplexClientsss
{
    class Program
    {
        static void Main(string[] args)
        {
            new Tests();

        }    }

    class Tests : ReportService.IReportServiceCallback
    {
        ReportService.ReportServiceClient obj;
        public Tests()
        {
             obj = new ReportService.ReportServiceClient(new InstanceContext(this));
             obj.ProcessReport();
        }
                public void Progress(int percentage)
        {
            Console.WriteLine(percentage);
        }
    }



}

【问题讨论】:

  • 请注意,双工服务的类似事件的行为只在会话中起作用。 - 发现这个here
  • 这几乎肯定与配置有关。确保两个服务客户端库之间的客户端 system.serviceModel 配置相同
  • 我为 winForms 使用了相同的配置,它正在工作。但不适用于控制台应用程序

标签: c# wcf


【解决方案1】:
new Task
(
()=>
{
        obj.ProcessReport();
}
).Start();

【讨论】:

  • 如果您想回答自己的问题,那很好,但请对您的解决方案提供充分的解释。如果没有进一步讨论,这个答案绝对没有意义。
  • 我不知道它是如何工作的,但我认为我之前的代码导致了死锁,因为我只使用了一个线程
猜你喜欢
  • 2011-03-23
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多