Hangfire with multiple projects in .net solution - asp.net

I am looking to implement hangfire into my asp.net web api and asp.net MVC website projects.
The way I have structured my solution is as follows:
Solution - My Solution
1: Model - (Project containing Entity Framework Objects and classes)
2: Services (Where I implement all my> business logic, changes etc.) This is where I will most likely make use of HangFire.
3: Web API (my asp.net api project)
4: Web UI ( mvc 5 Admin interface website)
Both project 3 and 4 make use of the 2:Services project to do work and call services which execute business logic. This is where most tasks will be spun off.
How would I go about implementing hangfire, so that they respective iis sites can both make use of the same "instance" of hangfire. but it will obviously run on the associated app pools?
or maybe it cant work like that and I have to have it running in one place?
What are my options, and furthermore what is the recomendd approach?

The biggest take-away for me was that HangFire will not continue past a work pool shutdown (i.e., idle timeout), which is my core problem anyway, and recommends altering the server configuration to never shut down work pools. If your app is going to be in constant use 24/7, then this shouldn't be an issue for you although your work pool could still be recycled for various reasons, but for an app that will experience peaks and troughs in users then you may want to consider an out-of-process HangFire server.
The approach I'm taking is the later. I'm building a proof-of-concept that has a Windows service (built using Topshelf - highly recommended for this) that hosts the HangFire server (and dashboard), a shared core library, and a client (which will be my WebAPI in production, but is a WPF app for the PoC). The client enqueues a job using a class instance from the shared library, which the HangFire server also has access to.
I'm assuming from your description that the WebAPI controller actions call corresponding methods in class from the service layer? If this is the case, then I would opt for a similar solution to mine, with the HangFire Windows service having access to your services and models as required.
If your app is going to be heavily trafficked and work pool recycles don't bother you, then I'd host the HangFire server in your WebAPI directly.

Related

Benefits of WCF Service Layer on a ASP.NET Website?

I need advise on the architecture of my new application. In the environment I work in, we normally create Win based apps and these compose a UI layer, Business Layer, WCF Service Layer, and Data Access Layer. These are on a 3 tier architecture with Services Server, Database Server and clients machines accessing the application via Citrix on an Application Server. I just created a web aplpication and my admin has suggested this should be hosted on a separate web server, my question is, is there a need for my web application to use a WCF Service Layer, since it can be hosted on the same application server? Basically, do websites need to use wcf/service layers and what are the benefits?
1.
Read this:
http://www.codeproject.com/Articles/10746/Dude-where-s-my-business-logic
My advice:
At the least, you should use WCF, and use "named pipes". You can deploy both the Host (WCF) and the Client (Asp.Net website) on the same machine.
The Asp.Net website will be the "client" and access the service.
Then, if you ever want to horizontally scale, all you have to do is change the .config settings to use a different protocol besides named-pipes.
Aka, you'll have the "logical separation", even if you deploy both on the same physical machine(s).
They (websites, any project) don't "need" to do anything.
You can write inline sql code behind buttons on webforms if you want.
But its not maintainable. And its not scalable. And it is probably foolish.
But putting in the time to SOA a mid to large sized application is worth the effort in the beginning, IMHO.
This is a question without a concrete possible answer, do you need a wcf? well that depends on the requirements and the architecture you want to put in place.
based on the small context you provide maybe you can get some the following benefits
WCF Provide state for operations
Fast processing (binary serialization)
WCF are more likely 'to be testable' than legacy asmx web services
You can access the same backend you built for your desktop applications
regarding if than can be accessed from the same server, the short answer is yes (obviously you need to measure your server capabilities)
I've done some app's that doesn't use a wcf at all, and other that use it for certain operations you can take a look to this SO answer to get a good example where a wcf could be a good candidate in an app.

VB6 and ASP.NET interoperability

I am supporting an ASP.NET app, which is installed on a web server and a VB6 app installed on a different app server. There is code duplication in the VB6 app and the ASP.NET app. I want to use some of the code in the ASP.NET app in VB6. I believe I have three options:
Expose the required functionality in an ASP.NET web service. The VB6 app will consume the web service.
Rewrite a small section of the vb6 app in .NET and extend the asp.net app. This will eliminate some of the code duplication.
Setup a class library for the ASP.NET app. Install the vb6 app on the web server. Expose the required functionality from the class library in a type library.
Which option is best? I believe option 2 is best.
Option 1. That leaves your shared, already-tested code on the most modern platform.
This is very hard to answer, as it varies for each company and each situation.
As a general rule, I'm very much in favor of using web services where possible, especially if multiple applications are using the same logic for the following reasons:
If I have to change the logic, I can do it in one place and fix all apps that depend on it
The same can be said for database connection strings, etc.
A bug fix can also often be fixed in one place.
I've had difficulties with a particular database that I need to deal with, where the vendor's updates tend to break their .NET adapter. Twice I had to modify/recompile a ton of apps to resolve this. Since then, we made it a policy to connect to that DB only via web services, so I'll only need to update one app in the future.
When developing mobile apps, the simple fact that we already had all our code in web services makes it that much easier to write apps that are strictly UI and leaving the business/database access logic as-is in existing web services.
All of those are pretty much "Standard" arguments for the SOA approach.
All things considered, my first recommendation, not knowing your specifics would be option #1.
There is a fourth option - a total rewrite of the VB6 app, if it's feasible, and if you can convince those who control the budgets and time allotment. Even with that, you can use the Service Oriented Architecture and split much of the logic into web services.

ASP.NET warmup/initialize

I'm trying to eliminate (or at least minimize) startup/warmup times for my .NET applications. I'm not really sure on how to do this even though it's a common concern.
There's a ton of questions about slow startup of .NET applications. These are easily explained by pool recycles, worker process startup, dynamic compilation of .aspx files, JIT etc. In addition, there are more things that may need to be initialized within the application such as EntityFramework and application caches.
I've found alot of different solutions such as:
ASP.NET Precompilation
IIS 8 Application Initialization (and for IIS 7.5)
Auto-Start ASP.NET Applications
However, I'm not entirely satisfied with any of the solutions above. Furthermore I'm deploying my applications to Azure Websites (in most cases) so I have limited access to the IIS.
I know that there are some custom "warmup scripts" that uses various methods for sending requests to the application (e.g. wget/curl). My idea is to create a "Warmup.aspx" page in each of my ASP.NET applications. Then I have a warmup service that sends an HTTP GET to the Warmup.aspx of each site every ... 5 minutes. This service could be a WorkerRole in Azure or a Windows Service in an on-premise installation. Warmup.aspx will will then do the following:
Send an HTTP GET to each .aspx-file within the application (to
dynamically compile the page)
This could be avoided by precompiling the .aspx pages using aspnet_compiler.exe
Send a query to the database to
initialize EntityFramework
Initialize application caches etc
So, my final question is whether there are better alternatives than my "Warmup.aspx" script? And is it a good approach or do you recommend some other method? I would really like some official method that would handle the above criteria.
Any and all suggestions are welcome, thanks!
Did you try this IIS Auto-Start feature described here ?
https://www.simple-talk.com/blogs/2013/03/05/speeding-up-your-application-with-the-iis-auto-start-feature/
You could have two instances of the site. When you need to deploy a new version, and therefore suffer a startup cycle, remove one instance out of load balancer rotation, deploy and start it, set it in and do the same for instance 2. A rolling deployment.

Running ASP.NET from hosting process

I'm responsible for a .NET process running under windows.
The process is running as a windows service.
I would like to have the ability to be able to get some info from the service in a web browser.
For that matter I would like to write a small ASP.NET web service.
The problem is that I want the run the web service within my process.
As far as I know, I can't do that since ASP.NET must run from within an IIS.
so...my question is, is it possible to host ASP.NET server within another process?
I know that in the common scenario, I should have the "process code" run as "code behind" the ASP.NET but in my case, the .NET service is already a part of our product so in this case I'd like to have the opposite.
Thanks a lot.
You don't have to host ASP.NET inside your service. You can use inter-process communications techniques to communicate between your service and a new or existing ASP.NET webapp. Here's an example using named pipes. Or if you're using .NET 3.5 or higher you can use WCF.
If you really want the service to host its own site I don't think embedding ASP.NET is possible but you can use an http component like this one.

Create one big web project or split some features into web services?

At what point (if at any) does it make sense to take some of the features of a .NET web application and split them into separate web services?
For example, we have a very large web application that also calls a series of long running operations (our core business logic). We also have a dll with client-specific custom features that is called directly from the web project. Sometimes we need to move very quickly to change these client functions, often in hours.
However, if we make a change to the client-specific features (or an emergency change to the core features) and publish a new project, then it would kick all the users out the system as the app restarts. It would seem like there's a better way ... but I'm not sure what it might be ...
First of all, it makes sense to remove everything from the web project that is not involved with the UI. Put that in separate class libraries.
Do not create web services unless some other application will be calling them. Simply placing the code into separate projects will be enough. Web services should only be created if they are a requirement.
I think it makes sense to create service app (not like Web Service, but more like Windows Service). You could communicate between your webapp and service app using Message Queue.

Resources