Running .net application next to asp core app on IIS - asp.net-core

I am trying to get my code to run alongside, in process, with an asp core web site.
With .net framework there is an option to create a module. Then, in order to "inject" that module so that it is run when the site runs all I need to do is add the module to web.config or launch it from a .cs file from \app_code.
With asp core, there is a concept called middleware but in order to add a middleware, the user has to write it into their startup code.
I need a way to run my .net core code when the site has started (first page accessed) without requiring the user to change their code to do so. Changing config files after deploy is OK but not compiled files.
Anyone know how to do this?
Thanks.

I see 2 options here:
Hosting startup assemblies
An IHostingStartup (hosting startup) implementation adds enhancements
to an app at startup from an external assembly. For example, an
external library can use a hosting startup implementation to provide
additional configuration providers or services to an app.
IIS modules with ASP.NET Core - you may still be able to inject your module in case you run the app on IIS. Add a web.config manually into the root directory and configure your module in there.

Related

Can I run .Net 4.6 Application under .Net Core Site on IIS

I inherited a project that is a collection of WCF Services that target .Net Framework 4.6.1. I wrote a front end SPA application that targets .Net Core 2.2 not realizing that the hosting model was such that both the UI (.Net Core) and the API (.Net 4.6.1) need to run under one Site on IIS.
I'm trying to get this to work on my local machine. I created a new Site in IIS -- Site A. I published my .Net Core application to Site A and set it up with an application pool set to "No Manged Code".
I then added an Application to Site A called API and added all my WCF services in that folder. I configured API to use an application pool that targets ".NET CLR 4.0".
I updated the web.config in the root of Site A to include all the necessary bits for WCF.
I'm able to hit Site A and get my SPA UI, but when I do anything that attempts to hit the backend API (including trying to hit it directly -- http://localhost:464646/api/test.svc), I get the following error:
HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure
Is what I'm doing even possible? If so, what changes do I need to make in order to get this to work?

Can a Worker Service be called and/or used inside an existing ASPNET.Core web project?

I've been reading and learing about the new Worker Service features provided in .Net Core 3.0. I have been using this link from Microsoft: Background tasks with hosted services in ASP.NET Core
What I don't understand is this, can these worker service concepts be introduced into an existing ASPNET Web Project, like a Razor Pages site? Or must you create a new project and then deploy that project as a service using whatever mechanism the host OS proivdes for that?
Yes, you can host any number of hosted services (IHostedService) within ASP.NET Core applications. With version 3, ASP.NET Core uses the generic host (Host.CreateDefaultBuilder) which is the framework that is hosting these hosted services when the application starts. In fact, the ASP.NET Core web application is an IHostedService itself.
To add additional hosted services to your ASP.NET Core application, just register additional hosted services with your service collection, e.g. within the Startup’s ConfigureServices:
services.AddHostedService<MyHostedService>();
That service will then launch together with the ASP.NET Core web server when the application runs.
The Worker SDK that is mentioned in the documentation is actually a subset of the Web SDK that you are using with ASP.NET Core application. Microsoft.NET.Sdk.Worker is basically Microsoft.NET.Sdk.Web without the web-specific stuff like Razor compilation and wwwroot folder stuff. It basically sets up automatic file globbing e.g. for the appsettings.json and does some other useful things that the core Microsoft.NET.Sdk does not have.
Ultimately this means, that when you are using the Web SDK, then you already have everything the Worker SDK offers. So you do not need to specify the Worker SDK just to host additional background services.

Read configuration from a specific file - Web.config or appsettings.json based on the calling web application

I have a monolith project and I'm trying to convert one Web Application from ASP.NET to ASP.NET Core with Full Framework.
As you can see from the figure, both Web Applications are referencing Class Library 3. CL3 contains Business Logic but also logic about how to read specific configuration (till now from Web.config file).
Since the way to read configuration has changed in ASP.NET Core (appsettings.json Environment based) I have to rewrite the part that reads the configuration from CL3.
Is it possible a scenario like this?
#IF CALLED FROM ASP.NET
RETRIEVE CONFIGURATION FROM WEB.CONFIG
#ELSE (CALLED FROM ASP.NET CORE FULL FRAMEWORK)
RETRIEVE CONFIGURATION FROM APPSETTINGS.JSON
If you are refactoring, try to move configuration reads out of the shared DLLs and provide a clean abstraction that does not have hidden dependencies on framework-specific assets.
Your ASP.NET Core Application and classic ASP.NET Applications could create a config based on their config system (you could provide two different config implementations) and then use your CL3's classes and pass in configuration objects.
Alternatively, you could use a ConfigurationBuilder to read AppSettings and connection strings from appsettings.json and appsettings.{Environment}.json and make it available through the classic ConfigurationManager API. See this blog post for details on how to do this. This can be extended to configuring custom configuration sections (classic .NET) from json files.

ASP.NET Core Application Initialization on Azure

I'm trying to implement Application Initialization (warm up) on an ASP.NET Core web app running on Azure, as described for IIS 8.
The way it's described is changing web.confg to enable and configure it.
But in asp.net core we (almost) don't have web.config.
So, how do we configure Application Initialization (or any other feature that require
changing web.config)?
I finally figured out, for this question and any other that requires IIS to be configured through web.config:
It's not true there isn't web.config anymore. It's true that it doesn't play any role in the asp.net core app, but you can find a small web.config file in wwwroot project folder, which is what tells IIS how to handle the asp.net core app.
So for implementing this, or for example urlrewrite, you just need to add IIS configuration to that web.config file as it's being deployed as part of the application.

MVC 4 App with Basic Authentication

I'm getting an error with an application that's using basic authentication for IIS 8. When I start the application it takes too long to run and also it doesn't load the .js and .css files.
The weirdest thing is that the same application loads correctly in other 3 machines but mine.
I defined an Application Pool with my network credentials and on Basic Authentication I also defined the host.
In my MVC project, on Web tab the project url is something like "http://localhost/MyAppName".
Is there anything that I could do to solve this?
Not being able to load .js and .css is usually the case when your application pool is running in Classic mode. Try switching it to Integrated. Also, make sure your pool is running v4.0 .NET Framework.