Search code examples
c#uwpwin-universal-appwindows-server-2019

Metadata file could not be found when trying to compile from the cmd


Here is my sample:

using System;
using Windows.Networking.Vpn;
static void main()
{VpnManagementAgent mgr = new VpnManagementAgent();
 VpnNativeProfile profile = new VpnNativeProfile() { AlwaysOn = false,
    NativeProtocolType = VpnNativeProtocolType.L2tp,
    ProfileName = "MyConnection",
    RememberCredentials = true, RequireVpnClientAppUI = true,
    RoutingPolicyType = VpnRoutingPolicyType.SplitRouting,
    TunnelAuthenticationMethod = VpnAuthenticationMethod.PresharedKey,
    UserAuthenticationMethod = VpnAuthenticationMethod.Mschapv2, }; 
    profile.Servers.Add("vpn.example.com"); 
    VpnManagementErrorStatus profileStatus = await mgr.AddProfileFromObjectAsync(profile);
    Console.WriteLine($"{profileStatus}\n");  }

Here is how I'm trying to compile (from Developer Command Prompt for VS 2019):

csc program.cs /r:Windows.Networking.Vpn.dll

Here is a screenshot of my installed tool-kits:

enter image description here

Here is my output:

Microsoft (R) Visual C# Compiler version 3.100.119.28106 (58a4b1e7)
Copyright (C) Microsoft Corporation. All rights reserved.

error CS0006: Metadata file 'Windows.Networking.Vpn.dll' could not be found

Here is the link from msdn:

Assemblies:Windows.Networking.Vpn.dll, Windows.dll


Solution

  • The types that you are looking for are defined in Windows Runtime metadata files and implemented in native code. You'll need to reference the winmd's. There are some shortcuts (like referencing the metadata installed wit the OS) but that will make your project brittle. Typically, you'll want to reference the installed SDK version. You can use path variables from a VS command prompt to help a bit, e.g. (with the 17763 SDK):

    csc Program.cs \
    -reference:"%WindowsSdkDir%\References\%WindowsSdkVersion%\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd" \
    -reference:"%WindowsSdkDir%\References\%WindowsSdkVersion%\Windows.Foundation.UniversalApiContract\7.0.0.0\Windows.Foundation.UniversalApiContract.winmd"
    

    However, this will still have some brittleness as the contract version numbers in those paths will change with SDK upgrades. The VS project system reads the current contracts from "%WindowsSdkDir%\Platforms\UAP\%WindowsSdkVersion%\Platform.xml" or "%WindowsSdkDir%\Platforms\UAP\%WindowsSdkVersion%\PreviousPlatforms.xml" to get correct API information for the target operating system version.