【问题标题】:Reconnect vpn. Windows service重新连接VPN。视窗服务
【发布时间】:2014-05-30 08:58:44
【问题描述】:

我一直在尝试实现一个可以保持 vpn 连接有效的 Windows 服务。我发现可以通过订阅RasConnectionWatcher.Disconnected 事件来使用DotRas 库:

public class SampleService {
    public SampleService() {
        this.shutdownEvent = new ManualResetEvent(false);
        this.connectionWatcher = new RasConnectionWatcher();
        this.connectionWatcher.Disconnected += onVpnDisconnected;
    }

    // redial
    void onVpnDisconnected(Object sender, RasConnectionEventArgs e) {
        this.DialUp();
    }

    void DialUp() {
        // connection setup is omitted
        // keep the handle of the connection
        this.connectionWatcher.Handle = dialer.Dial();
    }

    public void Start() {
        this.thread = new Thread(WorkerThreadFunc);
        this.thread.IsBackground = true;
        this.thread.Start();
    }

    public void Stop() {
        this.shutdownEvent.Set();
        if(!this.thread.Join(3000)) this.thread.Abort();
    }

    private void WorkerThreadFunc() {
        this.DialUp();
        while(!this.shutdownEvent.WaitOne(0)) Thread.Sleep(1000);
    }
}

当我启动服务时,vpn 连接打开没有任何问题,但是当我手动中断连接时,Disconnected 事件似乎没有启动。

【问题讨论】:

    标签: c# .net multithreading windows-services


    【解决方案1】:

    解决方案 1

    在这里找到类似的问题/答案:

    http://social.msdn.microsoft.com/Forums/en-US/56ab2d0d-2425-4d76-81fc-04a1e1136141/ras-connection-application-and-service?forum=netfxnetcom.

    解决方案 2

    昨天得到了Jeff Winn的答复:

    https://dotras.codeplex.com/discussions/547038

    public class VpnKeeperService : IService {
        private ManualResetEvent shutdownEvent;
        private RasConnectionWatcher connWatcher;
        private Thread thread;
    
        public VpnKeeperService() {
            this.shutdownEvent = new ManualResetEvent(false);
            this.connWatcher = new RasConnectionWatcher();
    
            this.connWatcher.EnableRaisingEvents = true;
            this.connWatcher.Disconnected += (s, args) => { this.DialUp(); };
        }
    
        Boolean DialUp() {
            try {
                using(var phoneBook = new RasPhoneBook()) {
                    var name = VpnConfig.GetConfig().ConnectionName;
                    var user = VpnConfig.GetConfig().Username;
                    var pass = VpnConfig.GetConfig().Password;
                    var pbPath = VpnConfig.GetConfig().PhoneBookPath;
    
                    phoneBook.Open(pbPath);
    
                    var entry = phoneBook.Entries.FirstOrDefault(e => e.Name.Equals(name));
                    if(entry != null) {
                        using(var dialer = new RasDialer()) {
                            dialer.EntryName = name;
                            dialer.Credentials = new NetworkCredential(user, pass);
                            dialer.PhoneBookPath = pbPath;
    
                            dialer.Dial();
                        }
                    }
                    else throw new ArgumentException(
                        message: "entry wasn't found: " + name,
                        paramName: "entry"
                    );
                }
                return true;
            }
            catch {
                // log the exception
                return false;
            }
        }
    
        public void Start() {
            this.thread = new Thread(WorkerThreadFunc);
            this.thread.Name = "vpn keeper";
            this.thread.IsBackground = true;
            this.thread.Start();
        }
    
        public void Stop() {
            this.shutdownEvent.Set();
            if(!this.thread.Join(3000)) {
                this.thread.Abort();
            }
        }
    
        private void WorkerThreadFunc() {
            if(this.DialUp()) {
                while(!this.shutdownEvent.WaitOne(0)) {
                    Thread.Sleep(1000);
                }
            }
        }
    }
    

    希望对某人有所帮助。

    【讨论】:

    • 很好的资源。谢谢老兄。
    猜你喜欢
    • 2010-11-23
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2015-09-18
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多