I have an on demand web job with asp.net mvc site deployed in azure. The web job attempts to send email with sendgrid.
Users of my site fill a form, optionally attach a file and click send. Server side I collect all data serialize it to json using Newtonsoft.Json. Both my web app and web job use the same version of Newtonsoft.Json.
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net47" />
Then I post the serialized data to my webjob using arguments
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)) }
}));
}
}
The serialized data gets received by my webjob which attempts to deserialize it using Newtonsoft.Json and send email using sendgrid
On running the web job in azure I get the error Unhandled Exception: System.AggregateException: One or more errors occurred. ---> Newtonsoft.Json.JsonReaderException: Invalid JavaScript property identifier character: }. Path '', line 1, position 6. at ABCD.Background.Program.d__4.MoveNext() in C:\Projects\ABCD\ABCD.Background\Program.cs:line 25
Line 25 is
DeserializedCustomer customer = JsonConvert.DeserializeObject<DeserializedCustomer>(email);
Now I've tried in vain to attach debugger to this webjob since it is on demand
Then I just copied over the web job function to a private function in my controller to check if the deserialization works in my web app. I ran my web app locally and surprisingly, it works!!!.
What could be the issue Newtonsoft.Json works in asp.net mvc app but not in on demand webjob deployed in azure?
Help
Update
Added logging to webjob using
Console.WriteLine($"SerializedEmail - {serializedEmail}");
Resulting Trace
[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()
Update
Why is the post not being received correctly by on demand azure web job?
Update
Sent a simple object with webclient to azure webjob
new System.Collections.Specialized.NameValueCollection
{
{"email",JsonConvert.SerializeObject(new {Email = "[email protected]"}) }
}));
Log shows the same
[08/28/2017 11:39:45 > 1a8d37: INFO] SerializedEmail - {email}
Is it the webclient or kudu scm?
Update
To receive the email parameter I do this but I receive nothing in my webjob
string serializedEmail = Environment.GetEnvironmentVariable("WEBJOBS_COMMAND_ARGUMENTS");
if (string.IsNullOrWhiteSpace(serializedEmail))
{
serializedEmail = args[0];
}
Still I do not receive the serializedEmail sent by asp.net mvc.
As far as I know, if you want to pass the parameters to the triggered webjobs. You should follow below types.
Besides,if you use console application as the web job. The console application get the args will remove the quotes inside a JSON.
Like this. If you send this json to the webjobs args.
The received string is:
So you should changed the converted string by using below codes.
Result: