简体   繁体   中英

How to get Windows Service and ASP.NET MVC project interacting?

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.

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).

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]

我使用Windows服务执行的ASP.NET MVC应用程序中的Web服务解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM