C# 具有双工通信的WCF

C# 具有双工通信的WCF,c#,wcf,C#,Wcf,每当我使用窗口窗体时,它都可以正常工作,但控制台应用程序总是出错 错误-套接字连接已中止。这可能是由于 处理邮件时出错,或超过接收超时时间 远程主机或底层网络资源问题。本地插座 超时时间为“00:01:00” 这是我的密码 合同 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using

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

错误-套接字连接已中止。这可能是由于 处理邮件时出错,或超过接收超时时间 远程主机或底层网络资源问题。本地插座 超时时间为“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);
            }
        }
    }
}

请注意,双工服务的类似事件的行为仅在会话中有效。-发现这几乎肯定与配置有关。确保两个服务客户端库之间的client system.serviceModel配置相同。我对winForms使用了相同的配置,并且它正在工作。但不适用于控制台应用程序请注意,双工服务的类似事件的行为仅在会话中有效。-发现这几乎肯定与配置有关。确保两个服务客户端库之间的client system.serviceModel配置相同。我对winForms使用了相同的配置,并且它正在工作。但不适用于控制台应用程序如果您想回答自己的问题,这很好,但请提供解决方案的充分解释。如果没有进一步的讨论,这个答案绝对没有意义。我不知道它到底是如何工作的,但我假设我以前的代码导致了死锁,因为我只使用了一条线程。如果您想回答自己的问题,这很好,但请对您的解决方案提供充分的解释。如果没有进一步的讨论,这个答案绝对没有意义。我不知道它到底是如何工作的,但我假设我以前的代码导致了死锁,因为我只使用了一个线程
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);
        }
    }



}
new Task
(
()=>
{
        obj.ProcessReport();
}
).Start();