views:

294

answers:

2

I've two separated projects being one of them a Windows Service having another one has a reference.

I want my Service to call a method from the referenced project, something like this:

protected override void OnStart(string[] args) {
    MessageSystem msg_system = new MessageSystem();

    IQueryable<MensagemGrupo> mensagens = 
        msg_system.GetScheduledMensagensGrupo();

    foreach (var msg in mensagens) {
        msg_system.ConfirmaEnvio(DateTime.Now, msg.id);
    }

The code i'm invoking throw the Service:

public class MessageSystem {
    private StorageModelDataContext db = new StorageModelDataContext();

    public IQueryable<MensagemGrupo> GetScheduledMensagensGrupo() {
        IQueryable<MensagemGrupo> mensagens = db.GetMensagensGrupoAgendadas();

        return mensagens;
    }
}

I'm getting a System.NullReferenceException starting at db.GetMensagensGrupoAgendadas(). Could it be because db is in a remote server?

Can i call methods this way from the service?

PS: The Service is LocalSystem. I've tried Network Service but i get "Error 5: Access Denied" while starting the service.

A: 

Do you want to call this via a web server, or do you just want to run the same code that's in your ASP.NET MVC app within your Service?

If it's the latter and you're calling a remote server that uses integrated authentication, your service has to run as a user that is valid on the remote server (that user will need 'logon as a service' rights to be able to run the service).

If you want to use it as a webservice (ie. the ASP.NET MVC code runs on a server and you make requests to it from your service), you should add a web reference to the appropriate URL your ASP.NET MVC application exposes, not a normal project reference to the project. See Scott's post on mixing ASP.NET WebForms with ASP.NET MVC and look at the example with the ASMX service for more details on creating the web service, then add a web reference (or service reference) to that ASMX from your service project.

[edited to clarify the web service option after seeing jvalente's comment]

Jonathan
Service and MVC app are in the same machine. Now i realized that i can't even get a string from the model. I'm doing something wrong. Is there any particularity to get a service "talking" with the model?
jvalente
A: 

I solved that problem using a Web Service in the ASP.NET MVC app that is executed thought the windows service.

jvalente