Asp.net web api 在windows服务中使用OWIN托管WebAPI

Asp.net web api 在windows服务中使用OWIN托管WebAPI,asp.net-web-api,owin,Asp.net Web Api,Owin,我使用OWIN(在windows服务中)自托管Web API。据我所知,这足以使HTTP请求进入windows服务。我可以点击WebAPI URL(http://localhost/users)本地(来自同一台机器),但不来自其他机器。我正在使用端口80,IIS已停止。其他网站(托管在IIS中,端口80)在IIS运行时工作正常 //在windows服务中: public partial class Service1 : ServiceBase { ... ... pro

我使用OWIN(在windows服务中)自托管Web API。据我所知,这足以使HTTP请求进入windows服务。我可以点击WebAPI URL(
http://localhost/users
)本地(来自同一台机器),但不来自其他机器。我正在使用端口80,IIS已停止。其他网站(托管在IIS中,端口80)在IIS运行时工作正常

//在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);
    }
}
公共部分类Service1:ServiceBase
{
...
...
启动时受保护的覆盖无效(字符串[]args)
{
Console.WriteLine(“启动服务…”);
字符串基地址=”http://localhost:80/";
WebApp.Start(baseAddress);//这是OWIN的东西。
}
...
...
}
公营创业
{
//此代码配置Web API。启动类被指定为类型
//WebApp.Start方法中的参数。
公共无效配置(IAppBuilder appBuilder)
{
//为自主机配置Web API。
var config=新的HttpConfiguration();
WebApiConfig.Register(配置);
appBuilder.UseWebApi(配置);
}
}
我需要做更多的事情才能从其他机器上工作吗?
(我感觉传入的http请求没有被转发到windows服务,而只是转发到IIS。当您在本地点击时,它可能不会通过侦听http请求的操作系统模块。只是猜测而已。)

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

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

(您应该注意到以
万维网服务开始的两个入站规则…
。这些规则是针对IIS的。我不确定启用这些规则是否足以允许您的windows服务接收请求…您可以尝试看看这是否有效。否则,如前所述,您可以创建一个新的入站规则..)

更新
根据您的评论,可能是由于您的Url注册,您无法访问该服务。以下是使用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))
{
StartOptions选项=新的StartOptions();
options.url.Add(“http://localhost:9095");
options.url.Add(“http://127.0.0.1:9095");
options.url.Add(string.Format(“http://{0}:9095”,Environment.MachineName));
使用(WebApp.Start(选项))
{
您可以在以下链接中阅读有关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))
{
    ...
}
StartOptions选项=新的StartOptions();
options.url.Add(“http://localhost:9095");
options.url.Add(“http://127.0.0.1:9095");
options.url.Add(string.Format(“http://{0}:9095”,Environment.MachineName));
使用(WebApp.Start(选项))
{
...
}

有两件事会阻止您在Owin服务中使用与“localhost”不同的东西:

  • 应用程序需要以管理员身份运行,才能打开一个具有不同主机名的端口作为“localhost”。您可以通过以管理员权限运行应用程序或使用以下命令为给定端口添加异常来解决此问题:
    netsh http add urlacl url=http://*:9000/user=
  • windows防火墙可能会阻止来自其他计算机的通信。在我的情况下,防火墙没有阻止本地通信(我可以访问
    http://localhost:9000
    http://127.0.0.1:9000
    http://192.168.1.193:9000
    -这是我在同一台计算机上的本地IP地址,但需要向防火墙添加端口例外,以允许从另一台计算机访问此服务)

  • 这实际上是一个简单的疏忽,我也被它绊倒了:如果你只在localhost、127.0.0.1等上侦听请求——没有人会看到它,因为只有你(self)知道你是那个目标。在我的机器上,我是localhost,如果我在任何端口上请求你的IP,那个服务将看到get,而不是localhost。你所要做的就是在“http://*”上服务{0}”,这样您就可以onStart()传递端口。

    类似的问题。我编写了一个ServiceStack服务,并使用AppSelfHostBase实现将其托管在Windows服务中。注意:这是作为一个Net Core应用程序编写的,可作为控制台应用程序进行编译,但通过PeterKottas.DotNetCore.WindowsService的魔力(可通过NuGet获得)可以作为Windows服务安装

    我将baseAddress设置为http://*.8088/并将其传递给我的AppSelfHostBase实现,但我只能从安装该服务的同一台机器上访问该服务,而不能从其他机器上访问该服务,这是通过尝试打开元数据页()在浏览器中。解决方案是将my console application.exe文件添加到安装该文件的服务器上Windows防火墙中允许的应用程序列表中。只需转到服务器控制面板中的防火墙“允许的应用程序”对话框,单击“允许另一个应用程序…”按钮,然后导航到Windows Service可执行文件:


    最终对我有效的是这个值:

    StartOptions options = new StartOptions();
    options.Urls.Add("http://*:9095");
    
    using (WebApp.Start<Program>(options))
    {
        ...
    }
    
    StartOptions选项=新的StartOptions();
    options.url.Add(“http://*:9095”);
    使用(WebApp.Start(选项))
    {
    ...
    }
    

    请注意,添加此选项后,您只能在Visual Studio中使用管理模式运行应用程序,并且应用程序的发布版本也需要在管理模式下运行。

    只需关闭防火墙以验证这确实是问题所在,如果它解决了您的问题,则添加规则。尝试了这两种方法,则无效。
    http://localhost/users
    有效,但
    http://127.0.0.1:80/users
    http://machine-name/users
    不工作。如果我使用端口80,我会发现404找不到,如果我使用另一个端口,我会收到400个错误的请求-无效的请求主机名。这是否意味着什么?我家里只有一台机器。我将明天检查并更新。您只需在
    启动它即可>http://*:9095/
    因此它将