Create a Windows Service that calls an MVC

Asked

Viewed 82 times

1

Good people, I already have a project in the DDD model, which runs through a MVC project, only now I want the application to run as a windows service, has some practical way for it?

Method in MVC I want to start is Index:

 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");
        }


    }

Metodo Windows Service:

  public  void ExecutarRotina()
    {
        //AwbController.Index();
    }

It is worth noting that I cannot put my vc Index method as Static

  • Your question is a little subjective. You have a Webapi and want to make it a Windows Service?

  • @Andersonsouza at first was a normal web application (however there was no iteration with the user) only now I want it to run as a windows service, so I added in my solution a project like windows service, ai do not know exactly how to that apart from windows service I am creating it perform the method I have in mvc, give to understand? sorry for the difficulty in explaining

  • Calm down, come on. The first thing is to determine the trigger for the call of this method, if it is via MVC project, you have to refocus the Windows Service (WS) project on the MVC project. If the trigger is in the WS project, you have to reference the MVC project in the WS project. To refocus right-click 'References' > 'Add Reference.. ' > 'Projects' > 'Solution' > (and the project). To use the declare using (referenced project) method and the method to be used.

  • Yes I did this but when I try to call the Index method for example I can only reference it in windows service if it is with Static, but I can’t leave it like this, because if I don’t need to change the whole method of MVC and it would affect other features

  • So ta facil rs, put the method you are calling in the MVC and how you are calling it in the WS!

  • @Andersonsouza put the code in the question

Show 1 more comment

1 answer

0


Your controller Awbcontroller has a builder:

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

Soon to the instance you must pass the required parameters Iawbapplication and Immovimentoapplication which apparently are an interface.

Then deploy these two interfaces in your Windows Service class and try this:

 public void ExecutarRotina()
    {
        using (var awb = new AwbController(instanciaInterfaceIAwbApplication, instanciaInterfaceIMovimentoApplication))
        {
            awb.Index();
        }
    }
  • the only problem is that the value arrives on the other side as null, I believe it is because Automapper is configured in Global.asax of MVC and when I call from another place does not occur this configuration loading, would have to do only a "start" MVC or configure Automapper in windows service tbm?

  • I couldn’t tell you without seeing the project, but I think I can.

Browser other questions tagged

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