首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重新连接vpn。Windows服务

重新连接vpn。Windows服务
EN

Stack Overflow用户
提问于 2014-05-30 08:58:44
回答 1查看 762关注 0票数 0

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

代码语言:javascript
复制
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事件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-02 12:11:31

解决方案1

在此发现类似的问题/答案:

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

解决方案2

昨天杰夫·温给出了一个答复:

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

代码语言:javascript
复制
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);
            }
        }
    }
}

希望它能帮到别人。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23950702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档