How to stop a connection?

Jun 13, 2013 at 8:25 AM
Hi,

I have some code to create a new L2TP-VPN Connection:
            _dialer = new RasDialer();
            _phonebook = new RasPhoneBook();
            _phonebook.Open(true);

            _dialer.DialCompleted += _dialer_DialCompleted;
            _dialer.Error += _dialer_Error;
            _dialer.StateChanged += _dialer_StateChanged;

            this.Hostname = Hostname;
            this.Passphrase = Passphrase;
            this.Username = Username;
            this.UserPassphrase = UserPassphrase;
            this.ConnectionName = ConnectionName;

            if (!this._phonebook.Entries.Contains(ConnectionName))
            {
                _entry = RasEntry.CreateVpnEntry(ConnectionName, Hostname, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));
                _entry.Options.UsePreSharedKey = true;
                this._phonebook.Entries.Add(_entry);
                _entry.UpdateCredentials(RasPreSharedKey.Client, Passphrase);
            }
            else
            {
                _entry = this._phonebook.Entries[ConnectionName];
            }
Works great! ok. Now I want to start the connection.
            this._dialer.EntryName = ConnectionName;
            this._dialer.PhoneBookPath = _entry.Owner.Path;
            this._dialer.Credentials = new System.Net.NetworkCredential(Username, UserPassphrase);
            this._handle = this._dialer.DialAsync();
Ok. Connection is started and I get the events of states and success.

Now I want to stop the connection.
But there is no this._dialer.Stop() or s.th. hm?

So I checked the Examples
        private void DisconnectButton_Click(object sender, EventArgs e)
        {
            if (this.Dialer.IsBusy)
            {
                // The connection attempt has not been completed, cancel the attempt.
                this.Dialer.DialAsyncCancel();
            }
            else
            {
                // The connection attempt has completed, attempt to find the connection in the active connections.
                RasConnection connection = RasConnection.GetActiveConnectionByHandle(this.handle);
                if (connection != null)
                {
                    // The connection has been found, disconnect it.
                    connection.HangUp();
                }
            }
        }
When I do this, the connection gets stopped, ok.
But there are no events! No success/error messages.

Is there a way, to stop a connection and have events?
Whats the way to get the informations about the current status of the connection?

Btw. The examples seem to use a lot of methods which are marked as obsolete :(

Greetz
Coordinator
Jun 13, 2013 at 12:54 PM
"Is there a way, to stop a connection and have events?"
Unfortunately the underlying Win32 RAS API doesn't have any events exposed during the disconnection process.

"Whats the way to get the informations about the current status of the connection?"
Once connected, RasConnection has methods all over it for getting information about an active connection.

"Btw. The examples seem to use a lot of methods which are marked as obsolete :("
As indicated on the home page, there are a lot of methods marked as obsolete in this release because they'll be getting removed in the next release. They still work just fine, just be aware that they will be removed in the 2.0 version. If you don't wish to use the obsolete methods, use the methods that aren't marked as obsolete as indicated by the documentation. The ones that are marked as obsolete are just calling the other exposed method internally. Those were there due to LINQ not being available until .NET 3.0, and this was originally a .NET 2 API.