Search code examples
c#asp.netcassinicassini-dev

Unable to connect to server


I am using the CassiniDevServer in my project. I created a object of the CassiniDevServer class and then call its start() method passing appropriate argument.

The server hosts and renders the webpages correctly in the browser. But after some time, when refreshed, browser says

Cannot connect to localhost:portno

It seems like server gets stopped after some time. Why is it so? How can I avoid this? Is it getting garbage collected?

I have a class MyCassiniDevUtil which has following method:

public CassiniDevServer launchWebsite(string path, string navigate, int _portNo)
  {
    CassiniDevServer _server = new CassiniDevServer();  ///server

    cassiniServerList.Add(_server);
    _server.StartServer(path, _portNo, "/", "localhost");
    portNo = _portNo;

    System.Diagnostics.Process.Start("http://localhost:" + portNo + 
          "/" + navigate);
    return _server;           
  }

In the calling code I create an object of MycassiniDevUtil and then call this method. This method returns the reference to the currently created server instance. I assign this reference in the calling code class.

So actually I do have references to both classes, but still I feel it is getting garbage collected. Or if not I don't understand why after few minutes when I refresh the browser it shows cannot connect to...


Solution

  • Instead of creating an object of CassiniDevServer class in CassiniDev; package, I created an object of Server class as follows (since it allows to specify the timeout property)

            System.Net.IPAddress ip = System.Net.IPAddress.Parse("127.0.0.1");
            Server _server1 = new Server(_portNo, "/", path, ip, "localhost", 1800000);
            _server1.Start();            
    

    _portNo is any port number - possibly in the range of unused port numbers. I generated one dynamically and randomaly as follows:

            Random rn = new Random();
            int _portNo = rn.Next(1500, 4000);
    

    path is a physical path to the website folder (the one which contains web.config,AppCode, AppData).

    1800000 are the milliseconds equivalent for 30 minutes.

    / is specified to get the website hosted at address http://localhost:<_portNo>/

    But can anyone clarify whats difference between CassiniDevServer and Server class in CassiniDev package.