简体   繁体   中英

Installing an OWIN service in Windows not working

I'm quite new to OWIN as a technology. I have a service, and my routing looks something like this:

[Route("myMessage/{message}")]
[AcceptVerbs("GET", "POST")]
[HttpPost]
public void myMessage(string message)

I have installed the service like this:

installutil My.Service.exe

And checked that the service is running under Windows (8).

However, when I try navigating to this URI http://localhost:1010/myMessage/test

I get a 404 error. Is there a way to debug or view whether this is correctly running (for example, in IIS I could select browse)?

Your route cannot be resolved.

It seems to me that you're trying to use attribute routing .

Make sure you have MapHttpAttributeRoutes configured in your Startup class:

class Startup
{
    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();

        config.MapHttpAttributeRoutes();

        //config.Routes.MapHttpRoute(
        //    name: "DefaultApi",
        //    routeTemplate: "api/{controller}/{id}",
        //    defaults: new { id = RouteParameter.Optional }
        //);

        appBuilder.UseWebApi(config);
    } 
}

As you can see I've disabled the standard routing system ( config.Routes.MapHttpRoute ) as I much prefer to define my own routes.

Now your api controller must have an attribute where you configure the prefix for your route with RoutePrefix .

Normally you would use some api/controller naming:

[RoutePrefix("api/messages")]

Your controller should look something like this:

[RoutePrefix("api/messages")]
public class MessagesController : ApiController
{
    [Route("myMessage/{message}")]
    [AcceptVerbs("GET", "POST")]
    [HttpPost]
    public string myMessage(string message)
    {
        return message;
    }
}

Your controller's name could even be something totally different:

public class FooController : ApiController
{

}

cause what it matters now is the RoutePrefix .

Your controller now should be able to accept request to:

localhost:<port>/api/messages/myMessage/<yourmessage>

Don't forget to define the right port in your ServiceBase :

protected override void OnStart(string[] args)
{
    myServer = WebApp.Start<Startup>(url: "http://localhost:1010/");
}

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