简体   繁体   中英

Cannot start an Azure WebJob command line application (Pending restart status)

This is my first time trying to develop and deploy a self hosted OWIN web API application in Microsoft Azure. For the question's sake I want to try and deploy the sample application found here .

So I simply have my three files, Program.cs, Startup.cs, and 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" }; 
        } 
    } 
 } 

So when I go to my project and select 'Publish as Azure WebJob' it says it was succesfully published to the .azurewebsites address, yet when I navigate to http://.azurewebsites.net/api/values I receive the message: "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.".

If I run this locally and change my baseAddress in Program.cs to localhost it works fine and I get a response from the controller.

The classic azure portal says my web job is 'Pending restart'.

I also tried creating a WebJob project rather than a console application and tried this in my Program.cs and publishing but this also did not work:

 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();
        }
    }

How can I have my self hosted web api server continuously running?

I think you may be misunderstanding what WebJobs are for. They are for performing background work (see doc , and not for exposing a web API. You need to be using a regular Web App instead.

And note that all Azure Web Apps go through IIS, so you'll need to use HttpPlatformHandler (but that's a bit of a different topic).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM