Azure Functions binding redirect(Azure Functions 绑定重定向)
问题描述
是否可以在 azure 函数文件夹结构中包含 web.config 或 app.config 文件以允许程序集绑定重定向?
Is it possible to include a web.config or app.config file in the azure functions folder structure to allow assembly binding redirects?
推荐答案
假设您使用的是最新的(2017 年 6 月)Visual Studio 2017 函数工具,我根据以下代码片段得出了一个基于配置的合理解决方案npiasecki
在 上发布的代码问题 #992.
Assuming you are using the latest (June'17) Visual Studio 2017 Function Tooling, I derived a somewhat-reasonable config-based solution for this following a snippet of code posted by npiasecki
over on Issue #992.
如果这是通过框架管理的,那将是理想的,但至少是配置驱动的,你有更多的变更隔离.我想您还可以使用一些预构建步骤或 T4 模板来协调项目中的 nugets 版本(及其依赖项),然后再编写此配置或生成代码.
It would be ideal if this were managed through the framework, but at least being configuration-driven you have a bit more change isolation. I suppose you could also use some pre-build steps or T4 templating that reconciles the versions of the nugets in the project (and their dependencies) before writing out this config or generating code.
.. 在更新 NuGet 包时必须记住更新 BindingRedirects
配置(这通常是 app.configs 中的问题).如果您需要重定向 Newtonsoft
,您可能还会遇到配置驱动解决方案的问题.
.. becomes having to remember to update the BindingRedirects
config when you update the NuGet package (this is often a problem in app.configs anyway). You may also have an issue with the config-driven solution if you need to redirect Newtonsoft
.
在我们的例子中,我们使用的是新的 Azure Fluent NuGet,它依赖于旧版本的 Microsoft.IdentityModel.Clients.ActiveDirectory
,而不是使用的普通 ARM 管理库的版本在特定功能中并排.
In our case, we were using the new Azure Fluent NuGet that had a dependency on an older version of Microsoft.IdentityModel.Clients.ActiveDirectory
than the version of the normal ARM management libraries which are used side-by-side in a particular Function.
{
"IsEncrypted": false,
"Values": {
"BindingRedirects": "[ { "ShortName": "Microsoft.IdentityModel.Clients.ActiveDirectory", "RedirectToVersion": "3.13.9.1126", "PublicKeyToken": "31bf3856ad364e35" } ]"
}
}
FunctionUtilities.cs
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace Rackspace.AzureFunctions
{
public static class FunctionUtilities
{
public class BindingRedirect
{
public string ShortName { get; set; }
public string PublicKeyToken { get; set; }
public string RedirectToVersion { get; set; }
}
public static void ConfigureBindingRedirects()
{
var config = Environment.GetEnvironmentVariable("BindingRedirects");
var redirects = JsonConvert.DeserializeObject<List<BindingRedirect>>(config);
redirects.ForEach(RedirectAssembly);
}
public static void RedirectAssembly(BindingRedirect bindingRedirect)
{
ResolveEventHandler handler = null;
handler = (sender, args) =>
{
var requestedAssembly = new AssemblyName(args.Name);
if (requestedAssembly.Name != bindingRedirect.ShortName)
{
return null;
}
var targetPublicKeyToken = new AssemblyName("x, PublicKeyToken=" + bindingRedirect.PublicKeyToken)
.GetPublicKeyToken();
requestedAssembly.Version = new Version(bindingRedirect.RedirectToVersion);
requestedAssembly.SetPublicKeyToken(targetPublicKeyToken);
requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;
AppDomain.CurrentDomain.AssemblyResolve -= handler;
return Assembly.Load(requestedAssembly);
};
AppDomain.CurrentDomain.AssemblyResolve += handler;
}
}
}
这篇关于Azure Functions 绑定重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Azure Functions 绑定重定向
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 使用 rss + c# 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01