简体   繁体   English

如何使Windows Service和ASP.NET MVC项目交互?

[英]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. 我有两个独立的项目,其中一个是Windows服务,另一个具有参考。

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(). 我从db.GetMensagensGrupoAgendadas()开始获取System.NullReferenceException。 Could it be because db is in a remote server? 可能是因为db在远程服务器中吗?

Can i call methods this way from the service? 我可以从服务中以这种方式调用方法吗?

PS: The Service is LocalSystem. PS:服务是LocalSystem。 I've tried Network Service but i get "Error 5: Access Denied" while starting the service. 我已经尝试过网络服务,但是在启动服务时出现“错误5:访问被拒绝”。

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? 您是否要通过Web服务器调用它,还是只想在服务中运行ASP.NET MVC应用程序中的相同代码?

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? 您是否要通过Web服务器调用它,还是只想在服务中运行ASP.NET MVC应用程序中的相同代码?

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. 如果要将其用作Web服务(即ASP.NET MVC代码在服务器上运行,并且您从服务中对其发出请求),则应将Web引用添加到ASP.NET MVC应用程序公开的相应URL。 ,而不是对该项目的正常项目引用。 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. 请参阅Scott关于将ASP.NET WebForms与ASP.NET MVC混合的文章,并查看带有ASMX服务的示例,以获取有关创建Web服务的更多详细信息,然后从您的服务项目中为该ASMX添加Web参考(或服务参考)。

[edited to clarify the web service option after seeing jvalente's comment] [编辑,在看到jvalente的评论后阐明了Web服务选项]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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