Asp.net application life cycle - c#

This question might sound silly but I never got a clear answer anywhere. I am trying to figure out how asp.net application actually works, in windows app (.net) when an application is deployed on one or more PC then user can click open the app and an instance of that application is created, if user again opens the app then another instance is created (opening multiple excel files could be an example). But on web, since the application is deployed on web server and there could be multiple user requesting same app so is there a mechanism to create an instance of asp.net web app for every unique browser request? In another words if A and B are two users and access same app then does two instance (inst A , inst B) are created on web server to process the request?

Webapplication itself has one instance. Each request is handled by different threads. Each request goes through application pipeline (different for old ASP.NET, different for ASP.NET Core )which in most common scenario creates new instance of class responsible for handling request (usually controller). Rest is defined in scopes (so if some of your classes are singletons or limited instances or you use IoC containers with defined scopes), then they can share instances between different requests.
Basically what you don't see is a main loop function provided by framework, which handles all requests, creates classes instances and passes control to your code.

Asp.net along with the web server and OS use threads to handle each request. Threads are much the same as processes you describe for separate instances of Excel. The threads allow each user session to be handled independently by the web server.

Related

Azure Web Job run in separate process?

I have the following solution setup with 3 VS Projects all running in Azure:
-ASP.NET MVC site
-Azure Web Job
-C# Data Project
MVC site and Web Job have a reference to the Data Project. In the data project I have a static List<> "cache" when keeps some data in memory when called from my MVC site.
My Web Job is processing some data, then I'm attempting to clear that static cache in the Web Job itself. Is that not possible since the Web Job simply runs in its own instance when referencing the Data Project? This appears to be what I'm seeing.
One thought I had was to expose and end point on my MVC site which could be called from my Web Job to clear it (worst case)
Any other thoughts here?
No, one process cannot access to another one's classes and instances.
The best you can do is as you said create a mechanism to intercomunicate both processes, through an end point, a socket, a pipe or the one you best like.

How to access a RIA Services WCF WebApp from another WebApp?

I have a silverlight application which accesses its data through RIA Services, from a WCF app in the server. The current structure looks like this:
DataWebServer - A Web application project, which holds the .aspx page that will call the Silverlight components, the .edmx model file and a MyService class, inheriting from LinqToEntitiesDomainService<FortWayneDB>.
Silverlight App - Contains .xaml e .cs files that will generate the .xap binary files, hosted by the WebServer. It access "DatawebServer" project through RIA Services.
It's all working fine, but now I need to create a new application, and since we will going to need it to run on platforms like tablets and smartphones, we decided to build it in HTML5, instead of Silverlight.
How can I make this new WebApp to access the data entities on "DataWebServer" project?
I think of 3 different solutions, but I'd prefer the third one, which exactly my question.
I could place the new WebApp in a folder in the same webprojeect "DataWebServer", but that wouldn't be very organized, I'd rather separate this app from the "DataWebServer".
The second alternative, which I will follow in case I can't succeed with the third, is to create WebMethods in "DataWebServer" to be accessed from my new WebApp.
the third, which I don't know how to do, is to make my new Web App to access the Entities through RIA Services, in the same way the Silverlight Client does. I've searched the Internet, but all articles I've found show how to access RIA Services from the same project. Does anyone know how I can do that?
The first method is the most sensible.
From your details, I assume the DataWebServer is "publicly" accessible; at least as much as your WebApp would be. There is little value in having WebApp data requests go to a different server, DataWebServer, as this introduces an unnecessary delay while one web server calls another. Instead of re-using the HTTP services from DataWebServer, add the WebApp functionality to the DataWebServer and re-use the LinqToEntities context.
If you desperately want the third option, you should consider creating your WebApp in a way that the JavaScript in the app calls the DataWebServer for data from the client's browser. Importantly, this approach avoids WebApp web server calling to DataWebServer for data.

Return Value from External Web Application

Looking for ideas. From within a web application (app #1), I need to call another web application (app #2), allow web app #2 to PRESENT FORMS TO THE USER, do some work, and then receive a result or return value into the original web app. The apps must be completely separated code bases. More than likely app #2 will need to be contained in a popup window but that is not entirely clear as of yet. There is a possibility that the single window may navigate to the second app. The apps are currently using ASP.NET and C#.
I originally thought of sending a "callback URL" to app #2, and allowing app #2 to post the result to app #1 through that URL, but then someone mentioned the need to do this as a modal window and NOT app #1 to navigate away from the calling page. On the other hand, if the receiving page was maintained on the back end, then it should always be possible to simply have the original app check for a session variable (or similar) since, as long as the browser for app #1 is not closed, the session content would still exist.
Thanks, in advance, for taking the time to respond.
If you want to communicate between two web applications, I would set up a Web Service interface in the second application. Then your first app can send "requests" to the Web Service that lives in the second app, and receive a response back.
MSDN has a lot of detailed information about this topic here: XML Web Services Using ASP.NET
The more specific places to get you started would be the guide to using Visual Studios web service creation wizard, and the examples of how to call a Web Service method.
This Stack Overflow answer has another way to call Web Service methods that might be more relevant to your use-case (calling via the URL).

ASMX service sharing a single class instance

I have a webservice with a .asmx extension which points to a class in my web application. After adding some code to output a debug log on application startup, I can see a new debug log is being created every time a user visits the page.
I was hoping that I could configure this web service to only ever use a single instance.
Meaning, when the first user visits the site it creates an instance of my webservice, and then all requests after are routed through that same class, sharing the same state.
Is this even possible?
Web sites are stateless in nature. Meaning, each request is generally unrelated to any other request.
That said, you could set the log as a static variable in your global.asax file. Be aware of threading issues.
Alternatively you might look at Elmah.
You seem to have a number of problems.
First of all, the ASMX technology is all but obsolete, and should not be used for new development. WCF should be used instead.
Next, yes, you can have multiple calls to the service share the same instance of your class. However, you'll have to prevent simultaneous access to this class unless it is thread safe. You should expect multiple service calls to call into the same class instance at the same time.
Finally, ASMX does not support singleton web services. The best you can do is to have multiple service calls all share the same object (carefully locked).

Integrate an E-Mail server into ASP.NET

I've a general design question:
I have a mailserver, written in C#.
Then I have a web forum software, written in for ASP.NET in C#.
Now I'd like to integrate the mailserver into the ASP.NET forum application.
For example, I'd like to make it possible that one can create a mailinglist from the forum, and give users the oportunity to add oneselfs to the mailinglist members in the forum, and then add the new list-members to the respective mailinglist on the server.
Since the server is a separate console/winforms/service application, I first thought I'd best use .NET remoting for this.
But my second thought was, that some users might host their forum on a host where
(a) they don't have a virtual machine where they can do what they want
(b) the admin of the host might not want to install an additional mailserver or charge extra for this
(c) the user might have a service plan that only permits to add a web-application, not external programs (very likely)
Now, I wanted to ask:
Is it possible to fully integrate a mailserver into an ASP.NET application somehow ?
(I have the full source of the server + ASP.NET application)
Well, it probably won't be a page or a ashx handler, but something like a http module ?
Or what's the general way to integrate TCP/IP applications into asp.net ?
(Of course I'm assuming the respecive ports are available/forwarded - and I'll make it possible to also run it with the e-mail server as external application)
In the ideal case I'd do the following:
Set it up on your own server(s) and expose a WCF/web service that your web app will/can interact with.
If you can't or don't want to afford to keep it running on your own, you could then charge a subscription fee for it.
It's probably not a very great idea, but you can start a thread in Global.asax and do background processing while the application pool is running/the web app is not reloaded. So you could start your server there, but you have no control over the lifetime of it
Adding to chris166's comment... you also wouldn't get control over when the application is started. [Since the application won't be loaded until a page is requested...] Its probably a better idea to setup some sort of integration between the web app and the console/service app.
I'd probably tend towards setting up a near-realtime integration where the mailserver polls the forum app for requested changes.

Categories