我正在尝试从我的.Net Web应用程序的application_start事件中获取Azure Keyvault的连接字符串。
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;
}
但我在下面收到此错误。 这是一个带有.Net 4.6的新MVC Web应用程序。 完全相同的代码在Console应用程序上正常工作。 我比较了包版本,它们是相同的。 我也尝试将该代码放在控制器的构造函数中但是同样的错误。 有什么想法吗?
未找到方法:'无效 Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler [])”
[MissingMethodException:找不到方法:'无效 Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler [])”。]
WebApplication1.MvcApplication.Application_Start()in C:\ Users \ test \ Documents \ Visual Studio 2017 \项目\ ConsoleApp1 \ WebApplication1 \ Global.asax.cs中:27[HttpException(0x80004005):找不到方法:'无效 Microsoft.Azure.KeyVault.KeyVaultClient..ctor(AuthenticationCallback, System.Net.Http.DelegatingHandler [])”。]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的 上下文,HttpApplication app)+10062145
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的 appContext,HttpContext上下文,MethodInfo []处理程序)+118
System.Web.HttpApplication.InitSpecial(HttpApplicationState状态, MethodInfo []处理程序,IntPtr appContext,HttpContext context)+173
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr的 appContext,HttpContext context)+336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr的 appContext)+296[HttpException(0x80004005):找不到方法:'无效 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>
答案 0 :(得分:1)
我不得不从引用中删除System.Net.Http并重新添加它,因为应用程序因某种未知原因加载了该库的2个不同版本。
答案 1 :(得分:1)
在我们的案例中,我们根据KeyVault nuget软件包在项目中使用以下属性来解决该问题:
<ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>
结果,这会强制引用GAC中的System.Net.Http。
答案 2 :(得分:1)