Azure 按需 Web 作业未收到格式正确的 json 序列化字符串

Azure on demand web job does not receive correctly formatted json serialized string

我有一个随需应变的 Web 作业,asp.net mvc 站点部署在 Azure 中。 Web 作业尝试使用 sendgrid 发送电子邮件。

我网站的用户填写表格,可选择附加文件并单击发送。服务器端我收集所有数据使用 Newtonsoft.Json 将其序列化为 json。我的网络应用程序和网络作业都使用相同版本的 Newtonsoft.Json.

<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net47" />

然后我 post 使用参数

将序列化数据发送到我的 webjob
  private static void TrySendEmail(Customer customer)
    {
        using (WebClient client = new WebClient())
        {
            string responsebody = Encoding.UTF8.GetString(client.UploadValues(ConfigurationManagerExtension.WebJobUrl,
                "POST",
                new System.Collections.Specialized.NameValueCollection
                {
                    {"email",JsonConvert.SerializeObject(CreateCustomerEmail(customer)) }
                }));
        }
    }

我的 webjob 收到序列化数据,它尝试使用 Newtonsoft.Json 反序列化它并使用 sendgrid

发送电子邮件

在 运行 天蓝色的 Web 作业上,我收到错误 未处理的异常:System.AggregateException:发生一个或多个错误。 ---> Newtonsoft.Json.JsonReaderException:无效 JavaScript 属性 标识符字符:}。路径 '',第 1 行,位置 6. 在 ABCD.Background.Program.d__4.MoveNext() in C:\Projects\ABCD\ABCD.Background\Program.cs:line 25

第 25 行是

  DeserializedCustomer customer = JsonConvert.DeserializeObject<DeserializedCustomer>(email);

现在我尝试将调试器附加到此 webjob 但没有成功,因为它是 on demand

然后我将 Web 作业函数复制到我的控制器中的私有函数,以检查反序列化是否在我的 Web 应用程序中有效。我在本地 运行 我的网络应用程序,令人惊讶的是,它有效!!!

可能是什么问题 Newtonsoft.Json 在 asp.net mvc 应用程序中有效但在 azure 中部署的按需 webjob 中无效?

帮助

更新

使用

向 webjob 添加了日志记录
  Console.WriteLine($"SerializedEmail - {serializedEmail}");

结果跟踪


[08/28/2017 11:39:44 > 1a8d37: SYS INFO] Status changed to Initializing
[08/28/2017 11:39:44 > 1a8d37: SYS INFO] Job directory change detected: Job file 'ABCD.Backgr.exe' timestamp differs between source and working directories.
[08/28/2017 11:39:45 > 1a8d37: SYS INFO] Run script 'ABCD.Backgr.exe' with script host - 'WindowsScriptHost'
[08/28/2017 11:39:45 > 1a8d37: SYS INFO] Status changed to Running
[08/28/2017 11:39:45 > 1a8d37: INFO] SerializedEmail - {email}
[08/28/2017 11:39:45 > 1a8d37: ERR ] 
[08/28/2017 11:39:45 > 1a8d37: ERR ] Unhandled Exception: System.AggregateException: One or more errors occurred. ---> Newtonsoft.Json.JsonReaderException: Invalid JavaScript property identifier character: }. Path '', line 1, position 6.
[08/28/2017 11:39:45 > 1a8d37: ERR ]    at Newtonsoft.Json.JsonTextReader.ReadUnquotedPropertyReportIfDone(Char currentChar, Int32 initialPosition)
[08/28/2017 11:39:45 > 1a8d37: ERR ]    at Newtonsoft.Json.JsonTextReader.ParseUnquotedProperty()

更新

为什么 post 未被按需 Azure Web 作业正确接收?

更新

用 webclient 发送了一个简单的对象到 azure webjob

  new System.Collections.Specialized.NameValueCollection
                {
                    {"email",JsonConvert.SerializeObject(new {Email = "johndoe@se7ev.com"}) }
                }));

日志显示相同

    [08/28/2017 11:39:45 > 1a8d37: INFO] SerializedEmail - {email}

是webclient还是kudu scm?

更新

为了接收 email 参数,我这样做了,但我的 webjob

什么也没收到
string serializedEmail = Environment.GetEnvironmentVariable("WEBJOBS_COMMAND_ARGUMENTS");
        if (string.IsNullOrWhiteSpace(serializedEmail))
        {
            serializedEmail = args[0];
        }

我仍然没有收到 asp.net mvc.

发送的序列化电子邮件

据我所知,如果你想将参数传递给触发的webjobs。您应该遵循以下类型。

https://{yourwebsitename}.scm.azurewebsites.net/api/triggeredwebjobs/{webjobsname}/run?arguments=yourarugment

此外,如果您使用控制台应用程序作为网络作业。控制台应用程序获取 args 将删除 JSON.

中的引号

像这样。如果您将此 json 发送到 webjobs args。

{"Name":"johndoe@se7ev.com"}

收到的字符串是:

SerializedEmail - {Name:johndoe@se7ev.com}

因此您应该使用以下代码更改转换后的字符串。

  string para = JsonConvert.SerializeObject(new Customer { Name = "johndoe@se7ev.com" });

    string escapedString = para.Replace("\"", "\\"");
    string url = @"https://{yourwebappname}.scm.azurewebsites.net/api/triggeredwebjobs/{webjobsname}/run?arguments=" + escapedString;
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentLength = 0;
    //you could find the user name and password in the webjobs property
    string logininforation = "${username}:{password}";
    byte[] byt = System.Text.Encoding.UTF8.GetBytes(logininforation);
    string encode = System.Convert.ToBase64String(byt);

    httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Basic " + encode);


   HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse() ;

结果: