簡體   English   中英

WCF具有雙工通信功能

[英]WCF with Duplex Communication

每當我使用Window Forms它工作正常但它總是給控制台應用程序錯誤。

錯誤 - 套接字連接已中止。 這可能是由於處理消息的錯誤或遠程主機超出接收超時或基礎網絡資源問題引起的。 本地套接字超時為'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);
        }
    }



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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM