Azure Functions 绑定重定向

Azure Functions binding redirect(Azure Functions 绑定重定向)

本文介绍了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 绑定重定向

基础教程推荐