我创建了一个简单的控制台应用程序并将其作为Azure webjob运行,得到以下错误。相同的代码在本地工作正常。
[10/10/2016 18:15:48> 494acb:SYS INFO]状态已更改为正在初始化 [10/10/2016 18:15:48> 494acb:SYS INFO]运行脚本' ConsoleApplication1.exe'使用脚本主机 - ' WindowsScriptHost' [10/10/2016 18:15:48> 494acb:SYS INFO]状态已更改为正在运行 [10/10/2016 18:15:49> 494acb:ERR] [10/10/2016 18:15:49> 494acb:ERR]未处理的异常:Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400)错误请求。 ---> System.Net.WebException:远程服务器返回错误:(400)错误请求。 [10/10/2016 18:15:49> System.Net.HttpWebRequest.GetResponse()上的494acb:ERR] [10/10/2016 18:15:49> 494acb:ERR]在Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync [T](RESTCommand
,作业失败1 cmd, IRetryPolicy policy, OperationContext operationContext) [10/10/2016 18:15:49 > 494acb: ERR ] --- End of inner exception stack trace --- [10/10/2016 18:15:49 > 494acb: ERR ] at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand
1 cmd,IRetryPolicy策略,OperationContext operationContext) [10/10/2016 18:15:49> 494acb:ERR]在Microsoft.WindowsAzure.Storage.Table.TableOperation.Execute(CloudTableClient客户端,CloudTable表,TableRequestOptions requestOptions,OperationContext operationContext) [10/10/2016 18:15:49> Microsoft.WindowsAzure.Storage.Table.CloudTable.Execute(TableOperation操作,TableRequestOptions requestOptions,OperationContext operationContext)中的494acb:ERR] [10/10/2016 18:15:49> 496acb:ERR]在ConsoleApplication1.Program.getStockPriceFromGoogle() [10/10/2016 18:15:49>在ConsoleApplication1.Program.Main(String [] args)494acb:ERR] [10/10/2016 18:15:49> 494acb:SYS INFO]状态已更改为“失败” [10/10/2016 18:15:49> 494acb:SYS ERR]由于退出代码-532462766
控制台应用代码:
class Program
{
static void Main(string[] args)
{
getStockPriceFromGoogle();
}
public static void getStockPriceFromGoogle()
{
try
{
CloudStorageAccount storageAcount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudTableClient tableClient = storageAcount.CreateCloudTableClient();
CloudTable googleStockTable = tableClient.GetTableReference("GoogleStock");
googleStockTable.CreateIfNotExists();
const string tickers = "RELIANCE,SBIN";
string json = null;
try
{
using (var web = new WebClient())
{
var url = $"http://finance.google.com/finance/info?client=ig&q=NSE%3A{tickers}";
json = web.DownloadString(url);
}
}
catch (Exception ex)
{
Trace.WriteLine("Error calling Google service , error :" + ex.ToString());
}
//Google adds a comment before the json for some unknown reason, so we need to remove it
json = json.Replace("//", "");
var v = JArray.Parse(json);
foreach (var i in v)
{
var ticker = i.SelectToken("t");
var price = (decimal)i.SelectToken("l");
GoogleStock googleStock = new GoogleStock(ticker.ToString(), DateTime.Now)
{
Price = price,
PriceInLocalCurrency = i.SelectToken("l_cur").ToString(),
id = (int)i.SelectToken("id"),
Exchange = i.SelectToken("e").ToString(),
Chanage = (float)i.SelectToken("c"),
ChnagePersontage = (float)i.SelectToken("cp"),
LastTradeTime = (DateTime)i.SelectToken("lt_dts"),
};
try
{
TableOperation insertOperation = TableOperation.Insert(googleStock);
googleStockTable.Execute(insertOperation);
}
catch (Exception ex)
{
Trace.WriteLine("Error When saving data , error :" + ex.ToString());
throw;
}
Console.WriteLine($"{ticker} : {price}");
}
}
catch (Exception ex)
{
Trace.WriteLine("Error When saving data , error :" + ex.ToString());
throw;
}
}
}
class GoogleStock : TableEntity
{
public GoogleStock(string StockName, DateTime InsertionDate)
{
this.PartitionKey = StockName;
this.RowKey = InsertionDate.ToUniversalTime().ToString();
}
public int id { get; set; }
public string StockName { get; set; }
public string Exchange { get; set; }
public decimal Price { get; set; }
public string PriceInLocalCurrency { get; set; }
public DateTime LastTradeTime { get; set; }
public float Chanage { get; set; }
public float ChnagePersontage { get; set; }
}
我在azure portal中添加了app设置键,但仍然没有帮助。
答案 0 :(得分:2)
当我们对Table服务执行操作时,许多factors可能会导致400错误的请求错误。例如,属性名称无效,指定的值无效等。
相同的代码在本地工作正常。
我在我的本地测试你的代码,你的代码不起作用(它返回400错误的请求错误)在我的本地。我发现您使用基于日期时间的行键(this.RowKey = InsertionDate.ToUniversalTime().ToString();
),这可能会导致此类型问题。行键值在我的本地显示为 10/11/2016 3:14:03 AM (具有正斜杠(/)字符)。但是,PartitionKey和RowKey属性的值中不允许使用正斜杠(/)字符。请查看“Characters Disallowed in Key Fields”部分。
您可以尝试使用以下代码将Ticks中的当前时间值作为行键值。
this.RowKey = InsertionDate.Ticks.ToString("d19");
此外,您可以查看this article,其中介绍了根据日期时间生成分区键的两种方法。