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

760 views Asked by At

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.

1

There are 1 answers

1
Brando Zhang On BEST ANSWER

As far as I know, if you want to pass the parameters to the triggered webjobs. You should follow below types.

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

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.

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

The received string is:

SerializedEmail - {Name:[email protected]}

So you should changed the converted string by using below codes.

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

Result:

enter image description here