【问题标题】:Azure Function Runs Locally But Not On AzureAzure 函数在本地运行,但不在 Azure 上运行
【发布时间】:2019-02-03 10:18:52
【问题描述】:

我的函数在本地运行,但是当我将它发布到 Azure 时出错。

错误是

值不能为空。参数名称:格式

谷歌搜索这似乎表明该函数的输入是错误的,但我发布了完全相同的 JSON,允许它在本地运行。

我不知道如何解决这个问题。有什么想法吗?

代码如下

using System;
using System.Configuration;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;



namespace MyFunction
{
    public static class Login
    {
        [FunctionName("Login")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
        {
            Boolean websiteEnabled = false;
            Guid contactId = new Guid();
            log.LogInformation("C# HTTP trigger function processed a request.");
            dynamic data = await req.Content.ReadAsAsync<object>();
            string username = data?.username;
            string password = data?.password;
            string passwordHash = "";
            User user = new User();
            OrganizationServiceProxy _serviceProxy;
            IOrganizationService _service;
            ClientCredentials clientCredentials = new ClientCredentials();
            clientCredentials.UserName.UserName = ConfigurationManager.AppSettings["OrganisationUsername"];
            clientCredentials.UserName.Password = ConfigurationManager.AppSettings["OrganisationPassword"];
            Uri organisationUri = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));
            Uri realm = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));
            using (_serviceProxy = new OrganizationServiceProxy(organisationUri, realm, clientCredentials, null))
            {
                _serviceProxy.EnableProxyTypes();
                _service = (IOrganizationService)_serviceProxy;
                QueryByAttribute querybyattribute = new QueryByAttribute("contact");
                querybyattribute.ColumnSet = new ColumnSet("cbob_websitepassword","cbob_websiteenabled","contactid","fullname", "parentcustomerid");
                querybyattribute.Attributes.AddRange("emailaddress1");
                querybyattribute.Values.AddRange(username);
                EntityCollection retrieved = _service.RetrieveMultiple(querybyattribute);
                if(retrieved.Entities.Count == 1)
                {
                    passwordHash = retrieved.Entities[0].GetAttributeValue<String>("cbob_websitepassword");
                    websiteEnabled = retrieved.Entities[0].GetAttributeValue<Boolean>("cbob_websiteenabled");
                    contactId = retrieved.Entities[0].GetAttributeValue<Guid>("contactid");

                    user.Account = retrieved.Entities[0].GetAttributeValue<EntityReference>("parentcustomerid").Name.ToString();
                    user.Email = username;
                    user.LoggedInUser = retrieved.Entities[0].GetAttributeValue<String>("fullname");
                    user.AccountID = retrieved.Entities[0].GetAttributeValue<EntityReference>("parentcustomerid").Id.ToString();
                    user.BookingID = retrieved.Entities[0].Id.ToString();
                } else
                {
                    return req.CreateResponse(HttpStatusCode.BadRequest, "Not allowed");
                }


            }



            Boolean hash = bCryptHash(passwordHash, contactId.ToString() +  "-" + password);
            Console.WriteLine(hash);

            if (!websiteEnabled)
            {
                return req.CreateResponse(HttpStatusCode.BadRequest, "Not allowed");
            }

            if (hash)
            {
                string output = JsonConvert.SerializeObject(user).ToString();
                return req.CreateResponse(HttpStatusCode.OK, output);
            } else
            {
                return req.CreateResponse(HttpStatusCode.BadRequest, "Not allowed");
            }


        }

        public static Boolean bCryptHash(string hash, string submitted)
        {
            Boolean hashPassword = BCrypt.Net.BCrypt.Verify(submitted,hash);
            return hashPassword;
        }

        public static String sha256_hash(string value)
        {
            StringBuilder Sb = new StringBuilder();

            using (var hash = SHA256.Create())
            {
                Encoding enc = Encoding.UTF8;
                Byte[] result = hash.ComputeHash(enc.GetBytes(value));

                foreach (Byte b in result)
                    Sb.Append(b.ToString("x2"));
            }

            return Sb.ToString();
        }
    }
}

【问题讨论】:

  • 如果我们看不到任何代码,就很难提供帮助。请将相关代码添加到您的问题中。
  • 你指的是什么代码?
  • 抛出你提到的异常的那个。我猜你的功能代码。
  • 我已更新我的帖子以包含代码
  • 我从 Azure 中删除了该功能,然后重新发布并开始工作。

标签: c# .net azure azure-functions


【解决方案1】:
Uri organisationUri = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));
Uri realm = new Uri(String.Format(ConfigurationManager.AppSettings["OrganisationURL"]));

我的猜测是其中一条或两条线可能是问题所在。您在这里使用String.Format,其中第一个参数是format 参数。您为该参数提供的 AppSettings 似乎不可用。确保在部署函数时这些配置值可用。

另外:如果您不向插入到字符串中的String.Format 提供任何对象,您为什么还要使用它?

【讨论】:

    【解决方案2】:

    确保您已将这些本地应用设置(即 OrganisationUsername 等在 local.settings.json 文件中)添加到应用设置。在 Azure 门户、平台功能> 应用程序设置中找到它。当我们将 Function 项目发布到 Azure 时,local.settings.json 中的内容不会发布是设计使然,因为它是为本地开发人员设计的。

    当我们使用 VS 发布函数时,有一个 friendly dialog 来更新应用程序设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多