简体   繁体   中英

Embedded Nancy not listening

I read a couple of related questions which had an issue with accessing nency from a remote computer. However, I am unable to access nancy from my own pc.

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. 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. 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.

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. 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. There it will wait until a key is pressed, and the host will be disposed after that event has occurred.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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