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

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

问题描述

我已经使用OWIN自托管的Web API(内窗口服务)。据我了解,这是足以让来到窗口服务的HTTP请求。我能够打的WebAPI URL(的http://本地主机/用户)在本地(在同一台机器),但不能从其他机器。我使用的是80端口, IIS停止的。其他网站时,IIS正在运行(托管在IIS,端口80)正常工作。

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请求不被转发到窗口服务的感觉,但只给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.

(你应该注意到夫妇与启动万维网服务入站规则... 。这些是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..)

更新:结果
基于您的评论,它可能是因为您的网址注册你是无法命中的服务。以下是用的HttpListener注册多个网址的一些例子。

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登记:结果
<一href=\"http://technet.microsoft.com/en-us/library/bb630429.aspx\">http://technet.microsoft.com/en-us/library/bb630429.aspx

<一href=\"http://technet.microsoft.com/en-us/library/bb677364.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

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

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