SignalR IIS hosting without OWIN StartUp.cs - signalr

Is it possible to host a SignalR application in IIS without using the OWIN start-up class ? I have an empty ASP.Net application with SignalR hub class and an html page in this same application.
If it is possible, how can we register the SignalR in OWIN pipeline ??

Add following config entry:
<add key="owin:AutomaticAppStartup " value="false" />
Then add following line to start Owin once the connection string is available by setting this using a static public property in Owin Startup class.
WebApp.Start("http://localhost:8080");
WebApp will be in Microsoft.Owin.Hosting namespace.

Related

Using ASP.NET Core middleware in ASP.NET 4.x app

I have a library project targeting .NET Standard 2.0 with ASP.NET Core middleware. It works fine with apps targeting asp.net core to .NET 6. I like to be able to use the same middleware/library in asp.net 4.x apps.
Is there an adapter class or can I create one in the library that will allow 4.x apps to use the asp.net core's middleware? So basically the adapter will tap into 4.x app's pipeline and intercept the http request matching a path. Then pass those calls to asp.net core middleware or business logic layer in the library.
Alternatively, can I have two different middleware; one for asp.net core app (current) and another for asp.net 4.x apps in the same library? Both middleware will share the same backend libraries.
In order to work with an ASP.NET MVC 4 app (I'm assuming that's what you mean) and view a Request you need to interact with the HttpContext from the System.Web library, which is not available in .Net Standard. This is a bit of an oversimplification, but you can read more here.
To overcome this issue, one option would be to house your library across 3 projests. The business logic project would target .Net Standard 2.0. A project with your .Net Core middleware would target .Net Core x.x (or .Net Standard 2.1) and reference the business logic project. And a third project would target .NetFX 4.6+ and reference your business logic project. This of course produces 2 separate DLLs/NuGet packages and is less than ideal, but probably the easiest solution.
As for the original question, ASP.NET MVC 4 doesn't have the concept of middleware baked in. You can add the Microsoft.Owin.Host.SystemWeb package to introduce it into the MVC4 app.
Information from Microsoft on OWIN/Middleware
However, you wouldn't be able to reuse the same middleware class for both MVC 4 and .Net Core as their dependencies and signatures are different. It also requires some altering of your existing MVC app.
Another option would be to create an IHttpModule that hooks into the HttpApplication life cycle stage that best suits your purpose and executes your business logic there.
public class FakeMiddlewareHttpModule : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpContext context = app.Context;
var businessLogic = new BusinessLogicClass();
businessLogic.PerformATaskOnTheRequest(context.Request);
}
}
You must register the module in your web.config:
<!-- Registering in Classic mode -->
<configuration>
<system.web>
<httpModules>
<add name="FakeMiddlewareHttpModule" type="FakeMiddlewareHttpModule"/>
</httpModules>
</system.web>
</configuration>
<!-- Registering in IIS7 Integrated mode -->
<configuration>
<system.webServer>
<modules>
<add name="FakeMiddlewareHttpModule" type="FakeMiddlewareHttpModule"/>
</modules>
</system.webServer>
</configuration>

ASP.NET Core How to read config properties as I do in a Spring?

I'm very newbie with ASP.NET, but not with Spring (Java).
I was wondering how the ASP.NET Core applications are normally configured ? The place I used before for my config in a web applications was application.properties (or others under /src/main/resources cause I used allways maven) and later I just had to add #Configuration Bean referencing the attributes in the POJO.
So is there a similiar way to do config stuff onLoad time on ASP.NET ? (easily)
How do you normally configure a ASP.NET or Where do you put the configuration files ?
How is the right way to do it ? Do you normally have a schema of files and directories to follow ?
Try this:
Class
using System.Configuration;
private string varName = ConfigurationManager.AppSettings["key"];
Web.config
<appSettings>
<add key="key" value="vaue" />
</appSettings>
Bit of a late answer here, but the approach in ASP.NET Core is to use .NET Configuration abstractions, including IOptions which is detailed on the same page. There are providers for application.json, environment variables and a variety of other options.
Coming from Spring, you may also be interested in config providers offered by Steeltoe, probably most notably options for using Spring Cloud Config Server

Configure method startup.cs asp.net core

When i request to the page. Why can't debug in configure method in file startup.cs?
Are configure method run only once when application start?
Yes, the Configure method in the Startup class of an ASP.NET Core application executes once at application start-up. It configures the middleware components that control how your app will respond to HTTP requests.
More information on this method can be found here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-2.1
And here's a nice overview of the ASP.NET Core startup process: https://developer.telerik.com/featured/understanding-asp-net-core-initialization/

Get current owin context in self host mode

I need to run my application which provides some ASP.NET Web API services on both IIS and .NET CLR self host modes.
I developed my ASP.NET Web API services based on OWIN and it is working fine on both hosts.
For now I need something like this:
public class OwinContextInfrastructure
{
public static IOwinContext Current
{
get
{
if (HttpContext.Current != null)
{
return HttpContext.Current.GetOwinContext();
}
else
{
// What should I do here ?
return null;
}
}
}
}
to get current owin context whenever I need in my application.
My code is working fine on IIS, but what should I do in .NET Self Host mode ?
You can use Request.GetOwinContext() for both web-hosting and self-hosting. GetOwinContext is an extension method for HttpRequestMessage and is defined in the System.Web.Http.Owin.dll assembly.
UPDATE
I have answered your original question, which is how to get OWIN context in both web-hosting and self-hosting. Now, through your additional question in the comment, you have significantly broadened the scope of your question. There is a fundamental problem though. IOwinContext is not a OWIN thing, it is a Katana thing. You cannot expect any framework hosted on OWIN to provide a context in the form of IOwinContext. ASP.NET Web API does but not every framework is supposed to. IOwinContext is an abstraction over OWIN environment dictionary and this dictionary will be available to any OWIN middleware. However, by working on top of a framework, you no longer can access the OWIN environment directly but only through how that specific framework has decided to expose the context.
For Nancy, you have to use NancyContext to get to the Items dictionary and look for the value corresponding to the key "OWIN_REQUEST_ENVIRONMENT". For SignalR, Environment property of IRequest gives you access to OWIN environment. Once you have the OWIN environment, you can create a new OwinContext using the environment.
First, I've to correct my question.
HttpContext.Current is available in applications which are based on ASP.NET and integrated IIS pipeline.But We can't use this class without asp.net anywhere, even on IIS integrated pipeline.
Answer:
1- Anywhere you need IOwinContext, you've to get it, using dependency injection, for example by constructor injection.
2- Configure everything to work based on Owin, SignalR is Owin based only, but use Web Api & owin together, and use nancy for server side views if any. Instead of writting IIS or ASP.NET handlers and modules, develop owin middlewares.
3- Using Autofac.Owin & AutoFac.WebApi & AutoFac.WebApi.Owin & Autofac.SignalR, you can setup dependency injection working across all owin middlewares you've in your application.
4- Autofac will instantiate web api controllers, signalr hubs and owin middlewares, and it will pass IOwinContext instance to classes you want using constructor injection.
My tests are ok on Owin IIS/Helios (without asp.net) , Owin SelfHost and even Owin Test Server.
This approach is similar to asp.net vNext. You can easily migrate your app to asp.net vNext, when it is production ready.

WCF using it's own web.config

I have a MVC3 app that is calling a WCF Service Application. The WCF Service App has its own web.config file (comes when you create the project automatically). In this .config I added an appSetting section with key to retrieve.
When I run the MVC app and it calls the WCF svc and the svc cannot see this appsettings value. If I move the appSettings section over to the MVC web.config the service application sees the value.
I would expect this from a calling application if it were a Winform or client based application calling a DLL but not where I have 2 separate apps where I actually want separate configuration files.
For example, I want to configure unity in my web services to perform dependency injection. I don't want the calling web application to know or have to define these values. The service should have them.
The issue I had was with the Unity configuration in the MVC app. Originally I had been pointing at a class library for my services layer, I swapped this over to use WCF. When I did this I left in the old type registrations which unity resolved and caused it to look at the new WCF project (same namespaces/class names) as a class library instead of using the endpoints that I registered.
Ripped out those specific class registrations leaving just the interfaces and endpoints and it worked like a charm.

Resources