Getting Method not found error when accessing to Azure KeyVault

0

I am trying to get my connection string from Azure Keyvault in the application_start event of my .Net web application.

    protected void Application_Start(Object sender, EventArgs e)
    {
        var azureServiceTokenProvider = new AzureServiceTokenProvider();
                var kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));

      RegisterRoutes(RouteTable.Routes);
   }

 public static async Task<string> GetToken(string authority, string resource, string scope)
    {
        var authContext = new AuthenticationContext(authority);
        ClientCredential clientCred = new ClientCredential(ConfigurationManager.AppSettings["clientId"],
                                                  ConfigurationManager.AppSettings["clientSecret"]);
                AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCred);

                if (result == null)
                    throw new InvalidOperationException("Failed to obtain the JWT token");

      return result.AccessToken;
  }

But I get this error below. This is a new MVC web application with .Net 4.6. The exact same code works fine on a Console app. I compared the package versions and they are the same. I also tried to put that code in the constructor of a controller but the same error. Any ideas?

Method not found: 'Void Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler[])'.

[MissingMethodException: Method not found: 'Void Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler[])'.]
WebApplication1.MvcApplication.Application_Start() in C:\Users\test\Documents\Visual Studio 2017\Projects\ConsoleApp1\WebApplication1\Global.asax.cs:27

[HttpException (0x80004005): Method not found: 'Void Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler[])'.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +10062145
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +173
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Method not found: 'Void Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler[])'.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +10043436 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +95 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

<packages>
  <package id="Antlr" version="3.4.1.9004" targetFramework="net461" />
  <package id="bootstrap" version="3.0.0" targetFramework="net461" />
  <package id="jQuery" version="1.10.2" targetFramework="net461" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.0.6" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.Web" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.2.0" targetFramework="net461" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.Azure.KeyVault" version="2.3.2" targetFramework="net461" />
  <package id="Microsoft.Azure.KeyVault.WebKey" version="2.0.7" targetFramework="net461" />
  <package id="Microsoft.Azure.Services.AppAuthentication" version="1.1.0-preview" targetFramework="net461" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.7" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration.Abstractions" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration.AzureKeyVault" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Configuration.FileExtensions" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.FileProviders.Abstractions" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.FileProviders.Physical" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.FileSystemGlobbing" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.Extensions.Primitives" version="2.0.0" targetFramework="net461" />
  <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.14.2" targetFramework="net461" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net461" />
  <package id="Microsoft.Net.Compilers" version="2.1.0" targetFramework="net461" developmentDependency="true" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net461" />
  <package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.7" targetFramework="net461" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net461" />
  <package id="Modernizr" version="2.6.2" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net461" />
  <package id="Respond" version="1.2.0" targetFramework="net461" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.4.0" targetFramework="net461" />
  <package id="WebGrease" version="1.5.2" targetFramework="net461" />
</packages>
azure-keyvault
asked on Stack Overflow Feb 27, 2018 by Aykut Ucar

2 Answers

3

I had to remove System.Net.Http from references and re-add it since the application was loading 2 different versions of that library for some unknown reason.

answered on Stack Overflow Feb 28, 2018 by Aykut Ucar
0

In our case, we solved it using the following property in projects depending on KeyVault nuget package:

<ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>

As a result, this forces a reference to System.Net.Http in GAC.

answered on Stack Overflow Jun 16, 2020 by ded'

User contributions licensed under CC BY-SA 3.0