Search code examples
c#networking.net-remoting

Geniune channel server in a windows service?


I am developping a server with geniune channel in a windows service , it seems that it's not as simple as the console application. When the thread that creates the server finishes his job , all variables that were declared in the thread scope are destroyed so the client cannot connect to that server anymore. With Console application we can avoid that with console.readline() but it's not the same case for windows service. So the first block of code is for windows service thread that creates the server and the second one is for the console application.

public void startServer()
{
 System.Configuration.ConfigurationSettings.GetConfig("DNS");
 GenuineGlobalEventProvider.GenuineChannelsGlobalEvent += new GenuineChannelsGlobalEventHandler(GenuineChannelsEventHandler);
 var Path = AppDomain.CurrentDomain.BaseDirectory;
 RemotingConfiguration.Configure(Path + "Server.exe.config");
 RemotingServices.Marshal(new Agent(), "Server.rem");
}


static void Main(string[] args)
{
 System.Configuration.ConfigurationSettings.GetConfig("DNS");
 GenuineGlobalEventProvider.GenuineChannelsGlobalEvent += new GenuineChannelsGlobalEventHandler(GenuineChannelsEventHandler);
 var Path = AppDomain.CurrentDomain.BaseDirectory;
 RemotingConfiguration.Configure(Path + "Server.exe.config");
 RemotingServices.Marshal(new Agent(), "Server.rem");
 Console.ReadLine();
 }

Have I missed somthing ? How can i achieve the availability of the server ?


Solution

  • Try to store the Agent in your service class when Start is called and dispose of it in the service shutdown when Stop is called.

    On a side note, if that's the problem, there's no good reason your console app works either. C# isn't C++, there are no destructors that are called when you leave scope. Also, why remoting instead of something more supported?