2017-08-28 125 views
1

我有一个asp.net mvc站点部署在天蓝色的按需web作业。网络作业尝试使用sendgrid发送电子邮件。Azure按需web作业没有收到正确格式化的json序列化字符串

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

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

然后我使用后的参数

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序列化的数据得到由我webjob它试图利用Newtonsoft.Json反序列化,并使用sendgrid发送电子邮件收到

在azure上运行web作业时出现错误未处理的异常:System.AggregateException:发生了一个或多个错误。 ---> Newtonsoft.Json.JsonReaderException:无效的JavaScript属性标识符字符:}。路径 '',第1行,在C处ABCD.Background.Program.d__4.MoveNext()位置6:\项目\ ABCD \ ABCD.Background \的Program.cs:行25

线25是

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

现在我妄图调试器附加到这个webjob,因为它是on demand

然后我就复制了网站工作职能的私有函数在我的控制器检查,如果反序列化在我的工作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() 

更新

为什么这个帖子没有通过点播azure web工作正确接收?

更新

发送一个简单的对象与Web客户端天青webjob

new System.Collections.Specialized.NameValueCollection 
       { 
        {"email",JsonConvert.SerializeObject(new {Email = "[email protected]"}) } 
       })); 

日志显示相同

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

它是Web客户端或捻SCM?

更新

要接收电子邮件参数我这样做,但我没有收到我的webjob

string serializedEmail = Environment.GetEnvironmentVariable("WEBJOBS_COMMAND_ARGUMENTS"); 
     if (string.IsNullOrWhiteSpace(serializedEmail)) 
     { 
      serializedEmail = args[0]; 
     } 

不过我没有收到由asp.net的MVC发送的serializedEmail。

+2

请这减少到[MCVE。鉴于'DeserializeObject'失败,所有后面的代码都是不相关的。看起来你的JSON很可能没有被正确地检索 - 你应该在反序列化之前在你的方法中记录下'email'的值。我怀疑这不是你所期望的。 –

+0

@JonSkeet在webjob中记录了电子邮件的价值,当然这不是我所期待的。问题是如何从webclient接收帖子的问题? –

+0

不知道 - 但现在您可以缩小问题的范围,而不是JSON反序列化。 –

回答

1

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

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

此外,如果您使用控制台应用程序作为Web作业。控制台应用程序获取参数将删除JSON中的引号。

像这样。如果你发送这个json到webjobs参数。

{"Name":"[email protected]"} 

接收到的字符串是:

SerializedEmail - {Name:[email protected]} 

所以,你应该使用下面的代码更改转换后的字符串。

string para = JsonConvert.SerializeObject(new Customer { Name = "[email protected]" }); 

    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() ; 

结果:

enter image description here

+0

非常感谢你指点我正确的方向。你的代码几乎完美无缺地工作(只是改变了我不必逃避序列化的字符串。我必须指出的一点是,当创建电子邮件对象时,我返回一个对象,因为JsonConvert.SerializeObject参数是一个对象。更新了返回具体类型的方法,您的代码开始无懈可击地工作,非常感谢。 –

相关问题