2017-08-28 126 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參數是一個對象。更新了返回具體類型的方法,您的代碼開始無懈可擊地工作,非常感謝。 –

相關問題