简体   繁体   English

嵌入式南希不听

[英]Embedded Nancy not listening

I read a couple of related questions which had an issue with accessing nency from a remote computer. 我读了几个相关的问题,这些问题与从远程计算机访问nency有关。 However, I am unable to access nancy from my own pc. 但是,我无法从自己的电脑访问nancy。

Here is my code: 这是我的代码:

    class Program
    {
        static void Main(string[] args)
        {
            HostConfiguration hostConfigs = new HostConfiguration();
            //hostConfigs.RewriteLocalhost = true;
            hostConfigs.UrlReservations.CreateAutomatically = true;


            using (var host = new NancyHost(hostConfigs, new Uri("http://localhost:1234")))
            {
                host.Start();
                Console.WriteLine("Running on http://+:1234");
                Console.WriteLine(host.ToString());

            }
            Console.ReadKey();
        }
    }

    public class HelloModule : NancyModule
    {
        public HelloModule()
        {
            Get["/"] = parameters => Response.AsJson("Success");

            Get["/nancy"] = parameters => Response.AsJson("Success");
        }
    }
}

I am administrator on my PC and I do not get any exception. 我是PC的管理员,我没有任何例外。 If I type http://localhost:1234 or http://127.0.0.1:1234 to my browser (with /nancy and without) I would expect a response. 如果我在浏览器中键入http:// localhost:1234http://127.0.0.1:1234 (带/ nancy和不带),则希望得到响应。 However, I do net get any reponse. 但是,我确实得到了任何答复。 Further, in the list produced with netstat -ano I do not see any process listing on port 1234. I downloaded the latest version of nancy via nuget. 此外,在使用netstat -ano生成的列表中,我在端口1234上看不到任何进程列表。我通过nuget下载了最新版本的nancy。

Do you have any idea? 你有什么主意吗?

The following line should work as expected: 以下行应按预期工作:

var host = new NancyHost(hostConfigs, new Uri("http://localhost:1234"))

But what happens with a using statement, is that anything specified between ( and ) (simply put) is disposed after the closing brace ( } ) of the same using statement. 但是using语句会发生什么,是在()之间指定的任何内容(简单地放置)都放在同一using语句的右括号( }之后。 So what is actually happening is, the host gets created, is started, and is disposed right after it printed some lines to the console. 因此,实际发生的情况是,主机被创建,启动并在将某些行打印到控制台后立即被处置。

Simply put, move the ReadKey call inside the using statement. 简而言之,将ReadKey调用移到using语句内。 There it will wait until a key is pressed, and the host will be disposed after that event has occurred. 在那里,它将等待直到按下某个键,并且在该事件发生后将处置主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM