Launch an MVC application through a Windows Service

Asked

Viewed 144 times

1

I have an application that was developed in the DDD standard, which uses dependency injection, it works normally, however I needed to add a Windows Service type project that will start the application, the problem is that if I make a reference to my MVC controller (where the whole process is initiated) Dependency Injection does not work (the variables that reference the classes of the other layers are not loaded and are null) I believe it is pq when I do this the global file.asax (where is the injection mapping not performed), I wonder if through my windows service I can start the MVC project as a whole? I believe that this way will work the other functionalities.

Controller of the MVC:

 private readonly IAwbApplication _awbApplication;
    private readonly IMovimentoApplication _movimentoApplication;
    public static List<string> LogAplicacao = new List<string>();

    public AwbController(IAwbApplication awbApplication, IMovimentoApplication movimentoApplication)
    {
        _awbApplication = awbApplication;
        _movimentoApplication = movimentoApplication;

    }

    public  void Index()
    {
        try
        {
            var movimentos = _movimentoApplication.ObterMovimentos();

            foreach (var movimento in movimentos)
            {
                var listaStatusWebServiceGol = _awbApplication.Pesquisar(movimento.nr_AWB.Trim()).ToList();
                var listaStatusMovimento = _awbApplication.StatusPorMovimento(movimento.id_Movimento);

                for (var i = listaStatusMovimento.Count(); i < listaStatusWebServiceGol.Count(); i++)
                {
                    listaStatusWebServiceGol.ToList()[i].id_Movimento = movimento.id_Movimento;
                    _awbApplication.InserirMovimentoTracking(listaStatusWebServiceGol.ToList()[i]);
                }

            }
        }
        catch (Exception ex)
        {

            GravarLogAplicacaoValidacao(ex);
        }

        finally
        {
            var log = new GeradorLogUtil();
            log.EscreverLog(LogAplicacao, @"C:\TrackingAwb\LogErro.txt");
        }


    }

Windows Service:

protected override void OnStart(string[] args)
    {
        Console.WriteLine(@"O Serviço começou");
        ExecutarRotina();
      //  _worker = new Timer((ExecutarRotina), null, 0, _interval);
    }

    protected override void OnStop()
    {
        Console.WriteLine(@"O Serviço parou");
    }

    public void ExecutarRotina()
    {
        // preciso chamar o projeto MVC aqui

    }
  • I couldn’t understand what you mean by startar an application. Are you referring to the host startar the application pool? Access a Controller Action? What’s the point of this?

  • Within my controler I have an Index method, which I would like to run so that when I call it through windows service, dependencies are not loaded, so calls to the other error layered methods, I believe that this occurs because when called the method of another application, the configurations that of the dependency injection that is in the global.asax is not executed, so I wanted to know if it has how to execute the MVC in general, I was able to explain what I want?

1 answer

0


If you need to simulate an access to your MVC application, you need to make a request for it, not just run a class method. After all, it is an application, not a library and depends on an entire environment to work with.

If this routine is necessary for both your MVC application and your Windows Service, it should be implemented in another layer shared by both.

And, if the purpose of the service is just to "make a hit" on the MVC application... it wouldn’t even have to be running, just schedule a task in windows itself for this.

But come on, in your proposal to achieve your goal just make the request to the url of the application.

public void ExecutarRotina()
{
    string mvcUrl = "https://answall.com"; // endereço da sua aplicação MVC;

    try
    {
        var httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(mvcUrl);
        httpWebRequest.Method = "GET";

        var httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
    }
    catch (Exception e)
    {
        throw e;
    }

}
  • @Jhonatassilva, there was some doubt?

  • guy helped me a lot, it worked thanks, just a doubt, I tested using the address with localhost msm and it worked, if I put it on another machine it would work tbm just specifying the port?

  • It will only work with localhost if the service is running on the same server as the mvc application, otherwise you have to provide the correct address

Browser other questions tagged

You are not signed in. Login or sign up in order to post.