How and when is the configuration method in the OwinStartup class called / executed?

Before asking my question, I already went through the following messages:

Here is my project folder layout:

enter image description here

There is currently no controller or view. Just an Owin Startup file.


Startup.cs

 using System; using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(Bootstrapper.Startup))] namespace Bootstrapper { public class Startup { public void Configuration(IAppBuilder app) { app.Run(async context => { await context.Response.WriteAsync(GetTime() + " My First OWIN App"); }); } string GetTime() { return DateTime.Now.Millisecond.ToString(); } } } 


Web.config

 <appSettings> <add key="owin:AutomaticAppStartup" value="true" /> <add key="owin:appStartup" value="Bootstrapper.Startup" /> <add key="webpages:Version" value="2.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="PreserveLoginUrl" value="true" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> 


I have the following link in a Bootstrapper project:

  • Microsoft.Owin
  • Microsoft.Owin.Host.SystemWeb
  • Owin
  • System
  • System.Core


UPDATE: Forgot to add an error message:

enter image description here


Now,

  • WHY does not work?
  • What is the step-by-step process of adding and using the Owin Startup class in a very basic project (for example, access to the Home/Index )?
  • How and when is the configuration method in the Owin Startup class called / executed?


UPDATE: 10-Dec-2016

Check out Project-Folder-Layout . In the Bootstrapper project, I have the following file:

IocConfig.cs

 [assembly: PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")] namespace Bootstrapper { public class IocConfig { public static void RegisterDependencies() { var builder = new ContainerBuilder(); builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource()); builder.RegisterModule<AutofacWebTypesModule>(); builder.RegisterType(typeof(MovieService)).As(typeof(IMovieService)).InstancePerRequest(); builder.RegisterType(typeof(MovieRepository)).As(typeof(IMovieRepository)).InstancePerRequest(); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); } } } 

Now I want to execute IocConfig.RegisterDependencies() in the Owin Startup class. I do using Bootstrapper in Startup from above, but it does not work. I mean, I cannot reference IocConfig in Startup . How to solve this problem?

+5
source share
2 answers

A little late, but I found a solution on how to put the OWIN startup class in a separate project. All that you have done in your project is correct, you have to apply one change to the properties of your Bootstrapper project. Right-click the Bootstrapper project, enter the properties, click the Tab, and find the Exit path. You should see the standard bin \ debug \ patch output, which means your Bootstrapper library will land in this folder. You must change this to the bin folder where your web application is located.

For example, I created a simple solution with two projects, first it is an empty web application, and the second is a library with the OWIN startup class. In the properties of the second project, I changed the output path to. \ OwinTest.Web \ bin. This will lead to the fact that after the assembly, all dlls will fall into one folder. Now you can start your application, and OWIN Startup should work correctly.

The following is the settings screen for the Bootstrapper project properties:

enter image description here

0
source
  • Create an empty web application project
  • Install OWIN using NuGet ( install-package Microsoft.Owin.Host.SystemWeb )
  • Add an empty class to the root directory of the project "Startup.cs"

Here I will answer your third question. The launch class is the OWIN entry point and is automatically scanned. As stated in the official docs:

Naming Convention: Katana looks for a class named Startup in the namespace by matching the assembly name or global namespace.

Note that you can also choose your own Startup class name, but you must set it using decorators or AppConfig. As stated here: https://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection

This is all you need for the basic and working OWIN test:

 using Owin; using System; namespace OwinTest { public class Startup { public static void Configuration(IAppBuilder app) { app.Use(async (ctx, next) => { await ctx.Response.WriteAsync(DateTime.Now.ToString() + " My First OWIN App"); }); } } } 

If you want to use MVC (I assume that "Home / Index" means MVC), follow these steps:

  • Install MVC NuGet ( install-package Microsoft.AspNet.Mvc ).
  • Add the Controllers folder to the project.
  • Create a new empty controller in the new "Controlles" folder (right-click → add → MVC 5 Controller - Empty) and name it "HomeController".
  • Create a view page in the newly created Views / Home folder. Right click → Add → View. Name it "Index" and uncheck "Use this page for customs."

Make the page inherited from WebViewPage. Everything should look like this:

 @inherits System.Web.Mvc.WebViewPage @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <h1>Owin Hello</h1> </div> </body> </html> 
  1. Add global.asax to configure routes. Right click on the project -> Add -> New Item -> Global Application Class.

Add route definition to Application_Start method:

 protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapRoute(name: "Default", url: "{controller}/{action}", defaults: new { controller = "Home", action = "Index" }); } 
  1. Be sure to comment on the previous middleware "..await ctx.Response.WriteAsync ...". Otherwise, it will interfere with MVC.
  2. Run the project. Must work.
+5
source

Source: https://habr.com/ru/post/1261046/


All Articles