.net远程处理,如果连接丢失,如何重新连接?

时间:2013-03-19 04:49:40

标签: c# .net-remoting

我有这个工作,但我不明白如何预测连接何时断开并需要重新连接到服务器。

我想要做的是让客户端立即连接到.net远程服务器,如果它丢失了,有点像继续尝试连接,如果不需要则忽略它。

帮助表示赞赏。

这是我的代码:

namespace TaskClient
{
    using System;
    using System.Reflection;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Threading;
    using TaskShared;

    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan, false);
            Connections remObject = (Connections)Activator.GetObject(typeof(Connections), "tcp://localhost:8085/TaskServer");

            if (remObject == null)
            {
                Console.WriteLine("Could not connect to TaskServer. (tcp://localhost:8085/TaskServer)");
            }
            else
            {
                Console.WriteLine("Connected to Task Server.");

                var rt = new TestTask() 
                {
                    ApplicationName = Assembly.GetExecutingAssembly().FullName,
                    ComputerName = Environment.MachineName,
                    VersionInfo = "1.0.0",
                    JobRunning = "None"
                };

                remObject.Add(rt);
            }


            while (true)
            {
                //TODO:
                //Check if connected.. how?
                //Re-create the connection... how?

                //doing this simple won't work:
                if (remObject == null)
                    remObject = (Connections)Activator.GetObject(typeof(Connections), "tcp://localhost:8085/TaskServer");



                remObject.Invalidate(remObject[rt]);

                Thread.Sleep(1000);
            }
        }
    }
}

0 个答案:

没有答案