无法启动Azure WebJob命令行应用程序(等待重新启动状态)
本文关键字:等待 重新启动 状态 应用程序 命令行 启动 Azure WebJob | 更新日期: 2023-09-27 18:17:43
这是我第一次尝试在Microsoft Azure上开发和部署自托管的OWIN web API应用程序。为了解决这个问题,我想尝试部署在这里找到的示例应用程序。
所以我只有三个文件,Program.cs, Startup.cs和ValuesController.cs:
Program.cs
using Microsoft.Owin.Hosting;
using System;
namespace OwinSelfhostSample
{
public class Program
{
static void Main()
{
string baseAddress = "http://<MYSITENAME>.azurewebsites.net/";
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
Console.ReadLine();
}
}
}
}
Startup.cs
using Owin;
using System.Web.Http;
namespace OwinSelfhostSample
{
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
}
}
ValuesController.cs
using System.Collections.Generic;
using System.Web.Http;
namespace OwinSelfhostSample
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}
所以,当我去我的项目,并选择"发布为Azure WebJob",它说它已成功发布到。azurewebsites地址,然而,当我导航到http://.azurewebsites.net/api/values,我收到的消息:"你正在寻找的资源已被删除,有其名称更改,或暂时不可用。"。
如果我在本地运行这个并在Program.cs中更改我的baseAddress到localhost,它工作得很好,我从控制器得到响应。
经典的azure门户网站说我的web工作是"等待重启"。
我还尝试创建WebJob项目而不是控制台应用程序,并在我的Program.cs和发布中尝试了这一点,但这也不起作用:
public static void Main()
{
string baseAddress = "http://<MYSITENAME>.azurewebsites.net/";
var host = new JobHost();
// The following code ensures that the WebJob will be running continuously
using (WebApp.Start<Startup>(url: baseAddress))
{
host.RunAndBlock();
}
}
如何让我的自托管web api服务器持续运行?
我想你可能误解了WebJobs的目的。它们用于执行后台工作(见文档),而不是用于暴露web API。你需要使用一个普通的Web应用程序。
请注意,所有Azure Web应用程序都通过IIS,所以你需要使用HttpPlatformHandler(但这是一个不同的主题)。