我正在开发一个应用程序,该应用程序将使用Windows VPN API为用户自动创建和连接VPN配置文件,但在尝试检查其连接状态时从Windows获得无意义的错误。我确定在此之前正确创建了配置文件(我可以很好地连接该配置文件),并且配置文件var非空并且是我创建的配置文件。
我的连接状态代码如下:
var vpnManagementAgent = new VpnManagementAgent();
var profiles = await vpnManagementAgent.GetProfilesAsync();
var profile = profiles.SingleOrDefault(p => p.ProfileName == "test");
logger.Trace($"got profile: {profile.PrettyPrint()}");
var result = profile.ConnectionStatus;
logger.Debug($"successfully got profile connection status: {result}");
尝试读取ConnectionStatus会导致此错误:
Error 1/17/2018 12:25:38 PM: IPC: [System.Exception] The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))
at Windows.Networking.Vpn.VpnNativeProfile.get_ConnectionStatus()
at WindowsFullTrustProcessShared.ProfileService<GetVPNConnectionStatus>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WindowsFullTrustProcess.MessageListener.<HandleRequest>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WindowsAppServiceIPC.TypedMessageListenerThread`2.<HandleRequest>d__1.MoveNext()
有问题的功能签名:
//
// Summary:
// Gets the current connection status.
//
// Returns:
// An enumeration value giving current connection status.
public VpnManagementConnectionStatus ConnectionStatus { get; }
VpnManagementConnectionStatus枚举的定义:
//
// Summary:
// Values used to report the current status of a VPN profile.
[ContractVersion(typeof(UniversalApiContract), 65536)]
public enum VpnManagementConnectionStatus
{
//
// Summary:
// The profile is disconnected.
Disconnected = 0,
//
// Summary:
// The profile is in the process of disconnecting.
Disconnecting = 1,
//
// Summary:
// The profile is connected.
Connected = 2,
//
// Summary:
// The profile is in the process of connecting.
Connecting = 3
}
据我所知,假设我有一个有效的配置文件对象,这个函数应该总是返回这个枚举的成员。如果配置文件存在并且已连接,则在运行此代码之前,这实际上可以正常工作。仅当配置文件未连接时,它才会出现上述错误。
我的代码显然有更多创建此配置文件,如果有人认为相关,我可以添加这些详细信息。但是,我可以连接这样的配置文件并在连接时检查其状态,这使我相信问题不在于配置文件创建代码。