Start an MVC application through a Windows Service

1

I have an application that was developed in the DDD standard, which uses dependency injection, it is working normally, but I needed to add a project of type Windows Service that will be the start of the application, the problem is that a reference is made my MVC controller (where I start the whole process) dependency injection does not work (the variables that make references to the classes of the other layers are not loaded and become null) I believe that is because when I do this the global file. asax (where the injection mapping is not executed), I would like to know if through my windows service I can start the MVC project as a whole? I believe this will work the other features.

MVC Controller:

 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

    }
    
asked by anonymous 16.02.2018 / 18:33

1 answer

0

If you need to simulate an access to your MVC application, you need to make a request for it, do not just run a class method. After all, it's an application, not a library, and it depends on the whole environment to work.

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

And if the purpose of the service is just to "make a hit" in the MVC application ... it would not even need to be running, just scheduling a task in windows itself for that.

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

public void ExecutarRotina()
{
    string mvcUrl = "https://pt.stackoverflow.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;
    }

}
    
16.02.2018 / 19:11