2016-09-15 48 views
0

我有一个webjob由存储排队触发,并在收到新消息时进行web api调用。我在fiddler中测试了get请求并且它可以工作,但是当通过webjob中的httpclient getasync发出请求时,会得到500个内部服务器错误。可以通过webjob调用web api吗?Azure应用程序服务WebJob httpclient getasync请求返回500内部服务器错误

+0

我已经做了一些测试,它看起来像一个webjob调用从它附着在该API的应用程序的方法导致500内部服务器错误,但调用其他web api的作品。如果有人知道为什么会这样,或者我做错了什么,我想听听。顺便说一下,这个测试是预先部署到Azure,它是在我的本地机器上完成的。 – rjespera

回答

0

我在fiddler中测试了get请求,它工作正常,但在webjob中通过httpclient getasync发出请求时,得到500内部服务器错误。

据我所知,500内部服务器错误是由您的Web API服务器端引发的。请尝试在您的Web API应用程序中用try-catch包装您的代码,并找到详细的错误。

可以通过webjob调用web api吗?

您可以在WebJob内发送http请求。这里是我的代码示例,它可以在我和Azure上都很好地工作,你可以参考它。

Functions.cs

// This function will get triggered/executed when a new message is written 
// on an Azure Queue called queue. 
public static async void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log) 
{ 
    log.WriteLine(message); 
    string requestUrl = "https://bruce-chen-001.azurewebsites.net/api/values"; 
    HttpClient client = new HttpClient(); 
    var response = await client.GetAsync(requestUrl); 
    log.WriteLine("response:{0}", await response.Content.ReadAsStringAsync()); 
} 

结果

response:["value1","value2"]

+0

谢谢,我有一个类似于你上面的测试方法,它工作正常,当我打电话给一个应用程序api服务以外,我有webjobs关联到下面是我的代码: – rjespera

+0

public static void ProcessQueueMessage([ QueueTrigger(“queueName”)]字符串消息,TextWriter日志) { HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromMinutes(15); var response = await client.GetAsync(new Uri(“http:// localhost:1100/api/values/1”));使用(HttpContent content = response.Content){ if(response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await content.ReadAsStringAsync(); } } } – rjespera

+0

当我将webjob移动到另一个API应用程序时,相同的代码工作得很好。这也是本地测试,所有的url都是localhost。 – rjespera

相关问题