Restart WCF service hosted by c # console application

I have a WCF service hosted in a C # console application. Is there a way to restart this service, preferably by calling the endpoint in the service itself (for example, myService.Restart ()).

thanks

+4
source share
5 answers

To answer my question, I solved the problem by doing the following:

  • Separating code loading DLL files from WCF service code into another class library project
  • Create an interface with the same method signatures as the files that load the DLL files into the new project (this interface is now used by both projects)
  • In the web service, upload another project to the new application domain. Thus, DLL files are blocked by the new application domain, and not by default.

If I want to update my nunit DLLs, all I have to do is to unload the application domain from the web service, update the files, and finally create a new application domain.

AppDomain remoteDomain = AppDomain.CreateDomain("New Domain"); IClass1 class1 = (IClass1)remoteDomain.CreateInstanceFromAndUnwrap( "Test1.dll", "Test1.Class1"); 

Note. IClass1 is a common interface between projects.

+1
source

I need to do something similar when I perform an automatic update of a remote WCF service. In your Restart () method, close the host:

  try { host.Description.Endpoints.Where(x => !x.Address.ToString().EndsWith("MEX")).ForEach(endpoint => _log.InfoFormat("Closing {0}", endpoint.Address)); host.Close(TimeSpan.FromSeconds(5)); } catch (Exception) { host.Abort(); } 

I wait for my update to be applied, and then after success or failure I will open the host again using the same code that I used to run it first.

If you just want to restart right away, you can just call host.Open (), or you can set a timer to call it, etc.

 try { host.Open(); host.Description.Endpoints.Where(x => !x.Address.ToString().EndsWith("MEX")).ForEach(endpoint => _log.InfoFormat("Host opened at: {0}", endpoint.Address)); } catch (Exception ex) { _log.Error("Unable to open host.", ex); } 
+1
source

you definitely cannot "restart" the failed service from calling the same service. In theory, you could host 2 services in the same process. put the one you want to "restart" in the public static variable, and restart it in another service. The problem would be to restart the restarter service if it is mistaken ... :) and you definitely want to limit the “administrator” in your restart service so that unauthorized users cannot do this.

0
source

This is a bit kludgy, but I suppose you could put a callback on your service, which the host can connect to and take appropriate action when it starts. This will give your host the opportunity to decide what “restart” means and how to proceed. More importantly, it allows you to decide whether to do something extreme, for example, to leave the observer process, and then independently or elegantly trash and reuse your service (preferably).

Mmmmmm ... kludge ....

0
source

You cannot request a reboot from the service. Consider a Windows service (a service hosted in a container with windows) that has RESTART functionality. Here, RESTART functionality is not provided by a service, but by a container. The container controls how to stop the service and start it.

Similarly, in your case, you should try to find options if your container can provide the functionality you need. Since you want to manage it remotely, the container must also be remotely deleted, which is not possible if the container is a console application. Instead, it should be another web service or web application.

0
source

Source: https://habr.com/ru/post/1342379/


All Articles