Converting Microsoft.AspNetCore.Http.HttpContext to System.Web.HttpContext - c#

My team is working on a new project that is built on the ASP.NET Core Web Application (.NET Framework) template. For legacy reasons we have another project that was build upon System.Web and directly accesses HttpContext via HttpContext.Current. Since our new project uses Microsoft.AspNetCore.Http.HttpContext we would like to know if its possible to convert the HttpContext to the older System.Web.HttpContext?

'Legacy' ASP.NET and ASP.NET Core don't play well together. In fact, I'd be surprised if they do at all. Their nature and characteristics are very different. Mocking the HttpContext in the legacy ASP.NET is pretty difficult. And even if you could, it's gonna be a hell of a job to set it correctly in the HttpContext.Current without being bitten by multi-threading issues.
My suggestion is to migrate the legacy application or expose the legacy functionality via a web API and use that to communicate between the two applications.

Related

Is it possible to set and get value in session in .NET Standard project

I need to set session variable in .NET Standard project and also get the value from same as well as .NET framework project. I am not able to see Httpcontext in .NET Standard. Also, I could not find any ways to use session in .net standard project.
How do I set session variable value in .NET Standard project and get its value in .NET Standard and .NETFramework project???
I found this URL:
HttpContext in .net standard library
Tried using Microsoft.AspNetCore.Http.Abstractions but session only accepts byte[] type.
A library for business logic directly accessing session state is no good architecture. Business logic code should not even care whether it runs in a web application or some other type of application.
Create a better abstraction. Declare an interface ISession in your library with all the methods that you want to use. Then provide a mechanism for an application that uses your library to provide an actual implementation of ISession.
In your web application, write a class that implements ISession by using the ASP.net session concept in the .net Framework.

How to using Asp.net Identity when using Business And DataModel Layer

I'm creating application with layered architecture. I have separate
+ 'DataModel' project with only model classes
+ 'BusinessLogic' project that containing my business
+ 'Core' project for run Business from ui
+ 'ViewModel' project
+ 'Web' project with asp.net core application.
My goal was to separate these projects so that Web project knows nothing about DataModel so the Web project should just reference Core and ViewModel. Everything was great until I configured Asp.Net Identity - in order to configure authorization I had to reference DataModel project which I had wanted to avoid. Is it possible to achieve my goal, and (if so) how to do it.
Note:
I'm using this how to separate model library when using asp.net identity for writing my question and i don't find accepted answer as my answer!
When you create a website that uses Identity directly, you must provide it with various Identity "stores": UserStore<TUser>, RoleStore<TRole>, etc. The default and easiest approach is to use Entity Framework Core as the backing for the store(s), and Identity comes with built-in stores to work with EF Core. However, using that requires access to the context, which means you then will need a dependency on your data layer. There is no way around that when using AddEntityFrameworkStores<TContext>.
If you want to keep your data layer abstracted, then you will need to either 1) use a centralized identity provider, such as IdentityServer or 2) create custom stores.
IdentityServer, for example, supports using both EF and Identity as a backing. That does mean it will need a dependency on your data layer, but IdentityServer would be exist in a separate project. Your actual website would handle auth via IdentityServer endpoints, and therefore would have no dependency on your data layer. In fact, it doesn't even know or care that you're using Identity at all at that point.
Creating custom stores will be a bit more difficult, obviously, and unless you provide an true abstraction layer, you'll still ultimately end up with a dependency on your data layer. That might be something like a microservice(s), where the store will actually make HTTP requests to the service to get the objects it needs, instead of making database queries directly. The microservices, then, will hold the data dependency.
One thing you might not be considering in this is that the dependency is there even without a direct reference. For example, if your Core project uses stuff from your DataModel project, and then your Web project uses stuff from your Core project, your web project has an implicit dependency on your DataModel project. If you look at the bin folder after building, for example, you're see a DLL for your DataModel project and even one for EF Core there, despite not explictly using either one in your Web project. In this case, using separate projects helps only to division the logic in perhaps a more succinct and understandable way, but it does not serve to actually abstract any dependencies.

Modular functionality with ASP.NET vNext Core CLR

With ASP.NET 4.5 it is possible to use Assembly.Load() or AppDomain.CurrentDomain.Load() to dynamically load an assembly at runtime. This can be used to add new functionality to a running web application in the form of modules without having to rebuild or even restart the app.
I would like to know how this can be done with the new ASP.NET vNext (5.0?) targeting the Core framework. I know this can be done with the full framework as System.AppDomain is available along with an overloaded Assembly.Load(). When I change to target the Core framework, I no longer have System.AppDomain available and Assembly.Load() becomes limited.
Is there a way to get this dynamic modular functionality in the ASP.NET 5.0 Core framework?
I'm not sure what a good answer would be, because Asp.Net 5 is so new, and doesn't have a lot of full documentation.
It should theoretically be possible, it'll just be different than what you're used to.
Looking at the source there is an interface IAssemblyLoadContext which will allow you to get the assembly. There is also IAssemblyLoader.
These are used by the "kre" host, which is the underlying piece of Asp.Net 5 that basically boostraps your application. So you would need to add your IAssemblyLoader to the underlying host, so that the kre.
Unfortunately I'm not seeing very many extension points in the code as of yet. Will we be getting those extension points? I do not know. You can log an issue on the github page. It's also possible there is an extension point I'm not seeing currently.
To come back to the question, can you add that kind of extensiblity to Asp.Net 5 Core? Yes, you could create your own host, similar to the default host, that would then include your custom loader, and then start your application with that.
You can try ExtCore framework
It allows to have modular and extendable ASP.NET 5 applications out of the box.
In short
It allows to use both modules from dlls and from nuget packages at the same time.
It allows all modules to work with storage in the single context and to have their own models and repositories.
Every extension can have controllers, views, static content etc.

Where is MVC in Web API?

I am trying to get a hold on WebAPI so please excuse if the question is too naive to ask.
Been hearing that WebAPI is using MVC and is integrated in to it.
Just created a sample WebAPI project but under References I find not System.Web.Mvc dll.
Why? Or am I missing anything important?
EDIT:
About System.Web.Mvc, the MSDN says the following paragraph:
The System.Web.Mvc namespace contains classes and interfaces that support the ASP.NET Model View Controller (MVC) framework for creating Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial view, model binders, and much more.
And it should not be there. ASP.NET Web API is very similar to MVC, buit is not integrated with it or whatever. Web API can live completely outside of MVC framework perfectly fine. Which is good - while building HTTP service you wouldn't want to include any HTML rendering features whatsoever into your project.
Think of it that way - System.Web.Mvc contains end-user-facing features that MVC uses, like views, view results. WebAPI is not a end-user-facing thing, it does not generate HTML or run javascript. Purpose of Web API is to provide means for creation of HTTP services - very different from MVC framework.
MVC and Web API share several similar approaches. Take controllers for example. Looks like they behave similar in both frameworks. However you may discover that implementations are different. MVC uses System.Web.Mvc.Controller, while Web API uses System.Web.Http.ApiController. More detailed explanation on the reason why is here. On the other hand - routing. Both frameworks use the same implementation of routing which resides in System.Web.Routing.
Frameworks are similar, but can exist completely separately. That is why there is no reference between them.
For Visual Studio Express 2013 for Web do the following:
New Project > Web > ASP.NET Web Application > Web API > OK
This should work, the Add folders and core references for: MVC and Web API should be automatically ticked by VS.
100% working - I've just created a project and all the references are there.

Can asp.net identity 2.0 run in it's own project?

I have just created a new asp.net mvc 5 project, but would like to abstract all of the identity logic into it's own project. I did something like this with asp.net identity 1.0, but through lack of understanding or otherwise, found it less than ideal as it appeared that I needed to duplicate several references for each project as well as the entity framework connection string to get it to work. I tried this same tactic with the latest version of identity 2.0 with no success. So is there a suggested way to do this, or is it better to just keep all the identity logic in the main mvc startup project?
There should be nothing preventing you from writing a shared class library that abstracts identity apis for you. Your MVC app would depend on the shared class library and use any apis exposed there, while your class library can reference identity (or whatever else you want) to implement its functionality.

Categories