我有自己托管的Web API使用OWIN(在Windows服务内)。从我的理解,这是足够让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); } }
我需要做更多的事情,以使这项工作从其他机器?
(我有一个感觉,传入的http请求没有转发到Windows服务,但只转发到IIS。当你打到本地,可能是没有通过监听http请求的操作系统模块只是猜测)
解决方法
您的计算机的防火墙可能阻止传入的请求。你可以做:
您可以运行wf.msc命令以使用高级安全打开Windows防火墙,并为TCP端口80添加新的入站规则。
(你应该注意到几个入站规则从万维网服务开始…这些是为IIS。我不知道启用这些规则是否足以甚至允许您的Windows服务接收请求…您可以尝试并看看这是否如之前建议的工作,你可以创建一个新的入站规则..)
更新:
根据您的意见,可能是因为您的网址注册,您无法打服务。以下是使用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)) {
您可以在以下链接中阅读有关网址注册的更多信息:
http://technet.microsoft.com/en-us/library/bb630429.aspx
http://technet.microsoft.com/en-us/library/bb677364.aspx