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

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]

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

Related

Web Service Rejecting Web Application Call With 401 Unauthorized Error

Basic question: how does one setup a web application and web services where they use windows authentication to connect the web application to the web service?
I have inherited an application written in asp.net 2.0 and am working to migrate it to asp.net 4.8. The solution is designed with a project for the web UI and a project for the web services. I have deployed both projects to an IIS site on a Windows 2019 server as separate applications on the same server in the Default Web Site. Each has their own application pool with AppPoolIdentity as the identity and both use Windows Authentication method and have all other authentication methods disabled. Each application is set to use pass-through-authentication when connecting.
When I run the UI application project it tries to call the first web service and I get 401 Unauthorized at the web service call. I have added some logging in the web application and the web service and can see it is authenticating me on the web application and verified the exact url (http://localhost/{appname}/authenticate.asmx) being used to call the web service but it is rejecting the attempt to access the web service with the 401 Unauthorized error from the web application. When I independently access the webservice it is accessible, authenticates me, and works as expected verified by logging output.
What do I need to change to get the application to connect to the web service with the same authenticated user? Do I need to change the AppPoolIdentity to something else like a service account or set it to Network Service or something else? I would have thought the UI application would pass-through the authenticated user to the web wervice but that must not be happening and I am not sure what to set to enable that connectivity.
Thank you in advance.

Host a Web application and Web API Project that Use IP Address on Two different Web Servers

Current Scenario that works well:
I have a .NET solution that contains 2 projects - Web Application and a Web API Project.
When I need to Publish - I am publishing Web Application first and then Web API project on the Web Server. I am using IP Address to Communicate from Web Application to Call Web API Controllers.
The client now needs this Application to be published to two different web servers that of course have different IPs.
How do I publish because I am using an IP address that is different for both the web servers?
Even if I use hostname/server name, I would get into the same issue of both being different.
Will you have 2 different databases?
I didnt not understand you clearly what is the problem here, if you host Web Application on serv1 and Web API on serv2, Web Application will use serv2 IP address to access the Web API, usualy Web API does not need to access to the Web application since is used just to store and revive data from someware but if u need to push something to the Web Application, Web API will use serv1 IP address to access.
If the WebApp is in angular/react then create the build from VS Code or whichever ide you are using.
If as WebApp you are using Razor/Mvc then remove all the references and then at every call instead of having the call directly have a web api call.
Web Api can be published independently if no references are attached to it.

execute an action in a .NET MVC Web App from a Windows Service

I have a .NET MVC web app and a Windows Service running on the same machine. Both projects use the same database through a different Data Access Layer project.
I need the Windows Service to perform some actions on the database, and i know there are different options, just want to know which is the correct:
1.- Calling an Action on the .NET MVC web app that also performs the same actions needed by the Windows Service. To do so i would call the Action with a standard "HttpWebRequest" call.
2.- Creating a Web API controller on the .NET MVC web app and calling it from the Windows Service using the WebApi.Client library.
3.- Creating a new WCF project to create a new service and calling it from the Windows Service.
I'm not familiar with any of the options above, so please feel free to post the correct way to do it.
How about creating a class library with the code you want to run and use it in both the web app and the service? I call that option 4.

How to check an ASP.NET MVC user is authenticated and authorized from a separate WCF service application?

I have two projects:
An ASP.NET MVC 5.2 Application using ASP.NET Identity 2.2
A WCF Application SOAP XML service.
Note: The WCF service is not hosted by ASP.NET, nor is it running in ASP.NET compatibility mode. A requirement of this project is that it is interface based and ASP.NET compatibility mode does not appear to allow an interface based implementation.
The ASP.NET MVC Application calls the WCF SOAP XML service server side when a user makes a specific action request. However, the WCF service is accessed via the public Internet so in theory anyone could call it if they knew the address. I need to ensure that only ASP.NET Identity registered users who are Administrator role are able to call it. The WCF Application could directly access the database but it doesn't seem like it would be the best solution?
How can I check from the WCF service whether a user is authenticated and authorized in ASP.NET MVC 5.2 using ASP.NET Identity 2.2 using object passing? Which objects or properties should be passed and checked? Is there any other solution? Is it possible to check authentication/authorization with attributes in wcf?
Do you own both, are they in the same domain?
You could interact with a database behind the scenes to generate an auth token, then have the wcf service pass a url with the token back to the user. When the user goes to the site via the tokenized url it checks against the database from the perspective of the ASP app and authenticates. It's a bit asymmetric, but it would handle your use case without getting into domain restrictions.

How to call windows service methods from web application

I need to develop windows service which will do fortnightly transfers of files into the system. The problem is that I will also need "RunNow" method, so users can call transfer method any time by clicking to the link in the web app (asp.net mvc).
How can I call my windows service methods from external resource?
If you want to call a windows service method on the server side of your web application then take a look at the WCF or RestSharp and Nancy. Shortly, you need to create a RESTfull service in the windows service application that will be using a http://localhost/myservice/transfer address to expose the Transfer method. Then use ajax from your javascript code or RestRequest from your .net-controller class to call the address.
But if you want to call a windows service method on the client side of the application it will be a problem.
You could use Microsoft Message Queuing
The Webapplication would send a Message that the Service picks up.
Queue-Based Background Processing in ASP.NET MVC Web Application
http://msdn.microsoft.com/en-us/library/ms978430.aspx

Categories