使用 webjobs 调用另一个网站的 api
Call another website's api with webjobs
我是 azure webjobs 的新手,但我正在尝试使用它们在需要时对 bing 地图 api 进行 api 调用。最终这将由我网站上的请求触发,但目前我只是想弄清楚 webjobs。我创建了一个控制台应用程序并将其作为 Web 作业手动添加到示例网站。我所有的测试 webjobs(简单的 hello world 类型的东西)都运行良好,但是当我尝试调用 bing 映射 api 时,webjob 说它成功完成但只执行了部分代码。这是网络作业的代码:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.MakeAsync();
}
public async void MakeAsync()
{
Console.WriteLine("I'm about to call");
CallMe c = new CallMe();
bool x = await c.CallBingMapsAsync(49931);
Console.WriteLine("I called and the result was {0}", x);
}
}
public class CallMe
{
public async Task<bool> CallBingMapsAsync(int zip)
{
RootOb lookups = await BingMaps.GetLocations(zip);
Resource here = lookups.resourceSets[0].resources[0];
string city = here.address.locality;
double lat = here.point.coordinates[0];
double lon = here.point.coordinates[1];
Console.WriteLine("I looked up {0} and found it at ({1} N, {2} W).",city, lat, lon);
return true;
}
}
BingMaps.GetLocations()
检索数据并将其转换为对象格式。当我 运行 在 visual studio 中执行此操作时,一切正常并且正确的内容打印到控制台等。
当我压缩它并将其添加为网络作业时,api 调用要么没有发生,要么网络作业在它发生之前就完成了。当我在 运行ning 之后查看日志时,我看到控制台消息 "I'm about to call" 发生,然后状态更改为成功并且 webjob 完成而没有 运行ning 任何其余的代码。这样的 webjob 是否不能与 api 调用一起使用,或者我只是做错了什么并且有修复?同样,我是 webjobs 的新手,所以任何简单的建议都会很棒。谢谢!
您可能需要等待响应发生。您正在调用 "async" 方法,但静态入口点本身不支持在关闭可执行文件之前等待这些方法完成。试试这个:
static void Main(string[] args)
{
Program p = new Program();
p.MakeAsync().Wait();
}
public async Task MakeAsync()
{
Console.WriteLine("I'm about to call");
CallMe c = new CallMe();
bool x = await c.CallBingMapsAsync(49931);
Console.WriteLine("I called and the result was {0}", x);
}
我是 azure webjobs 的新手,但我正在尝试使用它们在需要时对 bing 地图 api 进行 api 调用。最终这将由我网站上的请求触发,但目前我只是想弄清楚 webjobs。我创建了一个控制台应用程序并将其作为 Web 作业手动添加到示例网站。我所有的测试 webjobs(简单的 hello world 类型的东西)都运行良好,但是当我尝试调用 bing 映射 api 时,webjob 说它成功完成但只执行了部分代码。这是网络作业的代码:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.MakeAsync();
}
public async void MakeAsync()
{
Console.WriteLine("I'm about to call");
CallMe c = new CallMe();
bool x = await c.CallBingMapsAsync(49931);
Console.WriteLine("I called and the result was {0}", x);
}
}
public class CallMe
{
public async Task<bool> CallBingMapsAsync(int zip)
{
RootOb lookups = await BingMaps.GetLocations(zip);
Resource here = lookups.resourceSets[0].resources[0];
string city = here.address.locality;
double lat = here.point.coordinates[0];
double lon = here.point.coordinates[1];
Console.WriteLine("I looked up {0} and found it at ({1} N, {2} W).",city, lat, lon);
return true;
}
}
BingMaps.GetLocations()
检索数据并将其转换为对象格式。当我 运行 在 visual studio 中执行此操作时,一切正常并且正确的内容打印到控制台等。
当我压缩它并将其添加为网络作业时,api 调用要么没有发生,要么网络作业在它发生之前就完成了。当我在 运行ning 之后查看日志时,我看到控制台消息 "I'm about to call" 发生,然后状态更改为成功并且 webjob 完成而没有 运行ning 任何其余的代码。这样的 webjob 是否不能与 api 调用一起使用,或者我只是做错了什么并且有修复?同样,我是 webjobs 的新手,所以任何简单的建议都会很棒。谢谢!
您可能需要等待响应发生。您正在调用 "async" 方法,但静态入口点本身不支持在关闭可执行文件之前等待这些方法完成。试试这个:
static void Main(string[] args)
{
Program p = new Program();
p.MakeAsync().Wait();
}
public async Task MakeAsync()
{
Console.WriteLine("I'm about to call");
CallMe c = new CallMe();
bool x = await c.CallBingMapsAsync(49931);
Console.WriteLine("I called and the result was {0}", x);
}