C# Azure随需应变web作业未接收格式正确的json序列化字符串
我有一个在azure中部署了asp.net mvc站点的随需应变web作业。web作业尝试使用sendgrid发送电子邮件 “我的网站”的用户填写表单,可以选择附加文件,然后单击“发送”。服务器端我收集所有数据,并使用Newtonsoft.json将其序列化为json。我的web应用程序和web作业都使用相同版本的Newtonsoft.JsonC# Azure随需应变web作业未接收格式正确的json序列化字符串,c#,azure,json.net,azure-webjobs,ondemand,C#,Azure,Json.net,Azure Webjobs,Ondemand,我有一个在azure中部署了asp.net mvc站点的随需应变web作业。web作业尝试使用sendgrid发送电子邮件 “我的网站”的用户填写表单,可以选择附加文件,然后单击“发送”。服务器端我收集所有数据,并使用Newtonsoft.json将其序列化为json。我的web应用程序和web作业都使用相同版本的Newtonsoft.Json <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net47" /&
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net47" />
序列化数据由我的webjob接收,它尝试使用Newtonsoft.Json对其进行反序列化,并使用sendgrid发送电子邮件
在azure中运行web作业时,我收到错误未处理的异常:System.AggregateException:发生了一个或多个错误。-->Newtonsoft.Json.JsonReaderException:无效的JavaScript属性标识符字符:}。路径“”,第1行,位置6。在C:\Projects\ABCD\ABCD.Background\Program.d\uu 4.MoveNext()中的第25行
第25行是
DeserializedCustomer customer = JsonConvert.DeserializeObject<DeserializedCustomer>(email);
结果痕迹
更新 为什么按需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
更新
要接收电子邮件参数,我会执行此操作,但在我的webjob中没有收到任何内容
string serializedEmail = Environment.GetEnvironmentVariable("WEBJOBS_COMMAND_ARGUMENTS");
if (string.IsNullOrWhiteSpace(serializedEmail))
{
serializedEmail = args[0];
}
我仍然没有收到asp.net mvc发送的SerializedMail。据我所知,如果您想将参数传递给触发的webjobs。您应该遵循以下类型
https://{yourwebsitename}.scm.azurewebsites.net/api/triggeredwebjobs/{webjobsname}/run?arguments=yourarugment
此外,如果您使用控制台应用程序作为web作业。控制台应用程序获取参数将删除JSON中的引号
像这样。如果将此json发送到webjobs参数
{"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() ;
结果:
请将此值减少为一个数值。如果
反序列化对象
失败,那么它后面的所有代码都是无关的。很可能您的JSON实际上没有被正确检索-在反序列化之前,您应该在方法中记录email
的值。“我怀疑这不是你们所期望的。”JonSkeet在webjob中记录了电子邮件的价值,当然这不是我所期望的。Kudu是如何从webclient接收帖子的问题吗?不知道-但是你现在可以将问题缩小到这一方面,而不是JSON反序列化。@JonSkeet确定我会更新问题。非常感谢你为我指明了正确的方向。你的代码几乎完美无瑕(唯一的改变是,我不必转义序列化字符串。我必须为其他人指出的一点是,在创建电子邮件对象时,我返回了一个对象,因为JsonConvert.SerializeObject参数是一个对象。当我更新方法以返回具体类型时,您的代码开始完美工作。非常感谢。
{"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() ;