简体   繁体   English

WCF具有双工通信功能

[英]WCF with Duplex Communication

Whenever I am using Window Forms It works Fine But it always give error with console applicion. 每当我使用Window Forms它工作正常但它总是给控制台应用程序错误。

Error- The socket connection was aborted. 错误 - 套接字连接已中止。 This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. 这可能是由于处理消息的错误或远程主机超出接收超时或基础网络资源问题引起的。 Local socket timeout was '00:01:00'. 本地套接字超时为'00:01:00'。

Here is My Code 这是我的代码

Contract 合同

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);
    }
}

Service 服务

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 客户

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM