在 Windows 服务中使用 OWIN 托管 WebAPI [英] Hosting WebAPI using OWIN in a windows service

查看:45
本文介绍了在 Windows 服务中使用 OWIN 托管 WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 OWIN(在 Windows 服务中)自托管 Web API.据我了解,这足以让HTTP请求来到windows服务.我可以在本地(从同一台机器)访问 WebAPI URL(http://localhost/users),但不能从其他机器访问.我正在使用端口 80,IIS 已停止.其他网站(托管在 IIS 中,端口 80)在 IIS 运行时运行良好.

I've self-hosted Web API using OWIN (inside a windows service). From what I understand, this is enough to make HTTP requests come to the windows service. I'm able to hit the WebAPI URL (http://localhost/users) locally (from the same machine), but not from other machines. I'm using port 80, IIS is stopped. Other websites (hosted in IIS, on port 80) work fine when IIS is running.

//在windows服务中:

//In the windows service:

public partial class Service1 : ServiceBase
{
    ...
    ...

    protected override void OnStart(string[] args)
    {
        Console.WriteLine("Starting service...");
        string baseAddress = "http://localhost:80/";
        WebApp.Start<Startup>(baseAddress);  //This is OWIN stuff.
    }
    ...
    ...
}

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.
        var config = new HttpConfiguration();
        WebApiConfig.Register(config);
        appBuilder.UseWebApi(config);
    }
}

我需要做更多的事情才能从其他机器上运行吗?(我有一种感觉,传入的 http 请求没有被转发到 windows 服务,而只是转发到 IIS.当你在本地点击时,可能它没有通过监听 http 请求的操作系统模块.只是一个猜测.)

Do I need to do something more to get this working from other machines? (I've a feeling that the incoming http requests are not being forwarded to the windows service, but only to IIS. When you hit locally, probably it does not go through the OS module that listens for http requests. Just a guess.)

推荐答案

您机器的防火墙可能会阻止传入的请求.你可以这样做:

Your machine's firewall could be blocking the incoming requests. You could do:

您可以运行 wf.msc 命令打开高级安全 Windows 防火墙并为 TCP 端口 80 添加新的入站规则.

You could Run wf.msc command to open up Windows Firewall with Advanced Security and add a new Inbound Rule for TCP port 80.

(您应该注意到一些以 World Wide Web Services... 开头的入站规则.这些是针对 IIS 的.我不确定启用这些规则是否足以允许您的 Windows 服务接收请求...你可以尝试看看这是否像之前建议的那样有效,你可以创建一个新的入站规则..)

(You should notice couple of inbound rules starting with World Wide Web Services.... These are for IIS. I am not sure if enabling these rules would be enough to even allow your windows service to receive the requests...you can try and see if this works otherwise as suggested before, you can create a new inbound rule..)

更新:
根据您的评论,可能是因为您的 Url 注册导致您无法访问该服务.以下是使用 HttpListener 注册多个 url 的一些示例.

Update:
Based on your comment, it could be that because of your Url registrations you are unable to hit the service. Following are some examples of registering multiple urls with HttpListener.

StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:9095");
options.Urls.Add("http://127.0.0.1:9095");
options.Urls.Add(string.Format("http://{0}:9095", Environment.MachineName));

using (WebApp.Start<Program>(options))
{

您可以在以下链接中阅读有关 url 注册的更多信息:
http://technet.microsoft.com/en-us/library/bb630429.aspx
http://technet.microsoft.com/en-us/library/bb677364.aspx

You can read more about the url registration in the following links:
http://technet.microsoft.com/en-us/library/bb630429.aspx
http://technet.microsoft.com/en-us/library/bb677364.aspx

这篇关于在 Windows 服务中使用 OWIN 托管 WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆