Where does ASP.NET server start code go? - c#

I created a new ASP.NET Web Application project, using the "Empty" setting. This means I wasn't given any classes, and a fairly bare Web.config file. What file or config settings will I need to change to get a static class method to run when I first start the program?
Note: I tried adding a Startup.cs class with constructor, but that did not work, and I tried adding the <appSettings> tag with the child <add> tag specifying owin appStartup, but that did not work either.

It all starts with the global.asax. The IIS pipeline compiles global.asax, which usually references a global.asax.cs, which inherits from an HttpApplication. The application is then instantiated by the pipeline and its event handlers are called as pipeline events occur. The thing that sets everything off is the construction and Application_Start event of that application, where you compose the object graph, register bundles and routes, etc.
There is a lot more to it of course but that is where it all starts, if you want to try to reconstruct a working site yourself.

A very easy way to go about this, without needing to learn complicated IIS configuration, is to self host using ASP.NET Core.
First, install .NET Core: https://www.microsoft.com/net/learn/get-started/windows
Second, instead of following their steps to create a new console app, follow these command line instructions:
Create a directory for your project mkdir MyProject
Enter into that directory cd MyProject
Create a blank ASP.NET core app dotnet new web
Run it dotnet run
This will create and run a new blank ASP.NET web application which you can visit at http://localhost:5000
Two classes will have been generated: Program.cs and Startup.cs. Startup.cs will contain the logic for handling requests and serving responses, while Program.cs will contain the Main and BuildWebHost methods.
The server start code goes in the static Main method, allowing you to initialize anything you might want before the web server is even initialized.
An added bonus of using .NET Core is that you can run this not only on just Windows, but also all *nix systems as well (Linux, MacOSX).

Related

Reading configuration from Web.config when Startup of WebAPI class is invoked from OWIN TestServer

We have an ASP.NET Web application that uses .net Framework 4.7.2 and I am supposed to develop integration tests for that app.
I have a problem with OWIN TestServer.
Our Api project uses ConfigurationManager to read settings from Web.config file, that is placed inside of that project. It successfully reads settings when i run the api project, but when i try to run my integration tests project, anytime when i try to read one of the values (example below), it returns null:
string test = ConfigurationManager.AppSettings["SomeSettingName"];
I did ctrl+f in Visual Studio to see if reading configuration from web.config file is explicitly configured somewhere, but i didn't find it. I tried to find in google how to to manually specify where that configuration is, but it doesn't help either:
ConfigurationManager.OpenExeConfiguration("C:\\Users\\myuser\\project\\full\\path\\OurApi\\Web.config");
string test = ConfigurationManager.AppSettings["SomeSettingName"];
It's still null. Does anyone have an idea how to fix this issue?

.Net Core 3 console app no Startup class or Appsettings?

Obviously I'm still new with .Net Core with this question so bare with me. I created a .Net Core Console Application through Visual Studio 2019 and it did not create a Startup.cs class automatically. All it created was Programs.cs file. From what I understand and reading there has to be a Startup class since it is effectively the Global.asx file in .Net framework.
First I want to make sure I didn't miss a step that would have created the Startup automatically? Second, I'm assuming I can copy the code out of one of my test MVC Core applications and just use that?
Same question with Appsettings.json, I'm assuming I can just copy one from another project?
When you create .net core console application then your entry point will be Program.cs file Main() function
When you create MVC application in .net core then it creates StartUp.cs file as an entry program and application.json file for configuration.
Console application does not create both files for you.
you didn't miss anything. when you create a console application then there no need for Startup.cs file.
If you want to create a web application then Startup.cs file is necessary because it's called by Program.cs file.
The main method will execute in both the application. eighter it is a console or web application. the main method is the entry point from where your application will start.
Now if u r confused to take Startup.cs file it's for configuring various middleware for the web application that will be available during you will HTTP request from your browser.
if you are confused to take appsetting.json this is the only configuration file that you can copy from any application to any application. but you will need to take care of what configuration you have done in it.

Visual Studio 2019 .NET CORE and React Template Subsite

I am having a hard time getting this template to work as a subsite.
The steps that I took to create the project were:
Launch Visual Studio
Create a new project
Select ASP.NET Core Web Application
Gave the details for where the project should be stored, etc
Create
At this time I am not interested in redux so I just selected the "React.js A project template for creating an ASP.NET core application with React.js"
After that I thought that the steps to make this work at a sub-site would be:
Right-Click and Properties on the Web App Project, on the debug tab I set the App URL to what was there + /mysub, for example: http://localhost:57197/mysub
Inside the Client App Folder I modified package.json and set the "homepage" to "/mysub"
When I debug the application nothing renders in the browser. The browser's console shows that it is getting invalid characters when trying to load resources such as manifest.json, and even bundle.js, which tells me that the client side router is probably intercepting the requests for those files, and when I look at the network tab it looks pretty more obvious that is what is going on. The strange thing is though that tag does appear to be right and thus it is setting the %PUBLIC_URL% environment variable as I would expect, but for some reason it just will not work at this subsite location. I suspect that maybe there is more that I may need to do in Startup.cs, or even appsettings.json but I am not sure what that might be, or if even that is the route to go. Any suggestions would be great.

Adding a runnable script to a ASP.NET Core Application

I currently have a ASP.NET Core application implementing a basic web API. I have a need to write a script that hooks into the same environment with access to at least the database, but can't find a place to add new entry points to the application. I can easily look at string[] args in the Main method in Program.cs and not start the server, but I don't see how I can load up the environment and run arbitrary code without just standing up the normal Kestrel server. I'm pretty new to the .NET world so I'm hoping there is an easy way to do this that I've just failed to find in the documentation.
Basically I'd like to be able to run something like dotnet run foo which would execute some synchronous piece of code on the server instead of starting up the normal kestrel server.
You can do this by adding new projects. The idea is to have three projects co-exist in your solution:
the ASP.NET Core application, and
a .NET Core Console App containing your executable scripts, and
a .NET Class Library containing common dependencies of the two and containing service collection configuration, so that you can use the ASP.NET DI container from within your scripts
Steps to go from a single-project ASP.NET Core application to the configuration above (assuming that you're using Visual Studio) look something like:
Right-click your solution in Solution Explorer, go to Add -> New Project..., and select the project type Console App (.NET Core). Give it a name (e.g. Scripts) and click OK.
Add another project with type Class Library (.NET Core), named e.g. Common.
Migrate any classes that you want to use from the ASP.NET Core project to the Common project.
Add a CommonStartup class to your Common project. Give it a public static void ConfigureServices(IServiceCollection services) and cut and paste across any service configuration from your ASP.NET Core application's Startup.ConfigureServices method that you want to share with your Scripts project. Add a call to CommonStartup.ConfigureServices(services) to Startup.ConfigureServices method in the ASP.NET Core project. (You'll need to add references to Microsoft.Extensions.DependencyInjection; and Microsoft.Extensions.DependencyInjection.Abstractions; to the Common project and a reference to Common to the ASP.NET Core project; Visual Studio should offer these as sidebar actions.
At the start of your Scripts project's Main method, add:
var services = new ServiceCollection();
CommonStartup.ConfigureServices(services);
var provider = services.BuildServiceProvider();
(You'll once again have to add references.)
You can now use the DI system from within your Scripts project to get instances of classes, by writing e.g. var widgetFactory = provider.GetService<IWidgetFactory>().
You can build and run your script by running dotnet run from within the folder of your Scripts project.
If you want multiple executable scripts, just add a switch statement to your Main method that does something different depending upon the value of args[0], then call your scripts with dotnet run wibble, dotnet run wobble, etc.

How to disable HttpHandlers for production server

My website is using the MSCaptcha library for captcha functionality. The implementation requires handlers to be defined in the web.config file, a web/httphandler for older IIS and the development server, and a webServer/handler for IIS7+.
Now, every time I publish my project to the production server (which runs IIS7), I get an internal server error message because http handlers have been defined in the config file. Once removed, the website runs perfectly. However, when I am testing locally I need to have http handlers defined as the dev server doesn't seem to read webServer/handlers.
Obviously one solution is to simply remove the httphandlers temporarily every time I publish my website to the production server, but since I do this pretty often it is a bit cumbersome. Is there any way to specify when the http handlers should and should not be included in the config file?
Use configuration file transforms and build names to accomplish this. Create web.config that has the dev/local setup, then create a transform for release mode that removes your entry and creates the proper IIS7+ one.
Then whenever you deploy release mode it'll be right for your release environment!
This functionality is built into VS, but for non-web apps it won't work without using something like SlowCheetah.
Examples:
http://msdn.microsoft.com/en-us/library/dd465326.aspx

Categories

Resources