asp.net-web-api - 在 Windows 服务中使用 OWIN 托管 WebAPI

标签 asp.net-web-api owin

我使用 OWIN 自托管 Web API(在 Windows 服务内)。据我了解,这足以使 HTTP 请求到达 Windows 服务。我可以在本地(从同一台计算机)访问 WebAPI URL (http://localhost/users),但不能从其他计算机访问。我正在使用端口 80,IIS 已停止。当 IIS 运行时,其他网站(托管在 IIS 中,端口 80 上)可以正常工作。

//在windows服务中:

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请求的操作系统模块。只是猜测。)

最佳答案

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

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

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

更新:
根据您的评论,可能是由于您的网址注册,您无法访问该服务。以下是使用 HttpListener 注册多个 url 的一些示例。

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

关于asp.net-web-api - 在 Windows 服务中使用 OWIN 托管 WebAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21634333/

相关文章:

java - 本地C# API、android Studio程序

c# - 我必须向我的 Web API REST 方法添加显式线程吗?

c# - 使用控制台应用程序创建 ASP.NET Web API

c# - OWIN 启动类被多次调用

asp.net-mvc - 如何使用 Owin/Open ID Connect 添加其他 Azure AD 属性作为声明

c# - 关于web api ActionFilterAttribute、FilterProvider、Dependency Injection的一些问题

c# - 内容协商期间 HttpControllerContext.Configuration 有时为 null

c# - 如何在 Owin TestServer 创建后更新 HttpConfiguration

c# - 我应该使用 OwinContext 的环境来保存每个请求的应用程序特定数据吗

c# - 如何使用 asp.net 标识创建事务?