2016-11-21 88 views
1

每当我使用窗体窗体它工作正常但它总是给控制台应用程序错误。双工通信WCF

错误 - 套接字连接被中止。这可能是由于处理您的消息时出现 错误或远程主机发生接收超时 或基础网络资源问题。本地套接字 超时时间为'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); 
     } 
    } 



} 
+0

**请注意,类似事件的双工服务行为仅适用于会话。** - 发现此[在此](https://msdn.microsoft.com/en-us/library/ms731064(v = vs .110).aspx) –

+0

这几乎肯定与配置有关。确保两个服务客户端库的客户端system.serviceModel配置相同 –

+0

我对winForms使用了相同的配置并且它正在工作。但不能与控制台应用程序 – user7190448

回答

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

如果您想回答自己的问题,那很好,但请提供解决方案的充分说明。没有进一步的讨论,这个答案就毫无意义。 –

+0

我不知道它是如何工作的,但我认为我以前的代码导致了死锁,因为我只使用了一个线程 – user7190448