Upgrade Issues .Net 6 - asp.net-core-webapi

IsMimeMultipartContent()
IAuthenticationFilter is not available
Read Multipart
HttpContextWrapper
I am expecting to identify how can I achieve these when I am upgrading the FW4.8 to .Net6

"CS1061: ActionExecutingContext does not contain a definition for
Request".
HttpContentMultipartExtensions.IsMimeMultipartContent is used to determine whether the specified content is MIME multipart content.
In Asp.Net Core, you can check that the request is multipart/form-data using property HttpRequest.HasFormContentType:
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
if (!actionContext.HttpContext.Request.HasFormContentType){}
}
You can also refer to Mathieu Renda's answer.
IAuthenticationFilter is not available.
Asp.net core doesn't contain the IAuthenticationFilter, if you want to authenticated the user, you can refer to Brando Zhang's answer.
Error: HttpRequest does not contain a definition for Content
You can take a look at this official document: Upload files in ASP.NET Core.
And you can also refer to these two posts to solve your problem: ReadAsMultipartAsync equvialent in .NET core 2, MultipartFormDataStreamProvider for ASP.NET Core 2.
Replacement for HttpContextWrapper
The HttpContextWrapper class derives from the HttpContextBase class and serves as a wrapper for the HttpContext class. So I think it is possible to access HttpContext directly in Asp.Net Core: Access HttpContext in ASP.NET Core.
Hope this can help you.

Related

ASP .NET Boilerplate + MongoDb

I am using ASP.Net boilerplate framework + SQL Server 2016 in my project. Recently I have faced a challenge with migration from SQL Server to MongoDB. I have found that it is possible with ASP .NET boilerplate and installed required NuGet packages, however, due to the lack of documentation the only thing I have managed to do is to define respective RepositoryBase class:
public abstract class MyRepositoryBase<TEntity, TPrimaryKey> : MongoDbRepositoryBase<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
protected MyRepositoryBase(IMongoDatabaseProvider databaseProvider)
: base(databaseProvider)
{
}
}
As far as I understand, first of all, I need to define connection string somewhere now. And then populate the database with required basic data(which previously had been done by EF Core migrations). Obviously, EF Core in the new approach is obsolete so does that mean for my DbContext class that it is obsolete as well?
Actually, there are plenty of questions in relation to ASP .NET boilerplate and MongoDB integration, therefore my current post is actually a request for provision of some kind of example of the existing integration. Thank you in advance.
You can register your module by depending on it on your web module.
[DependsOn(typeof(YourMongoDbModule))]
public class YourWebModule : AbpModule
{
}
I think you have to register the repository with:
IocManager.Register(typeof(IMongoRepository<>), typeof(MongoRepository<>), Abp.Dependency.DependencyLifeStyle.Singleton);
You can refer this sample.
Look at this comment also.
Here is a framework which maps EF Core to Mongo DB.

What is difference between normal cache class and MemoryCache class?

What is the difference between normal cache class and MemoryCache class?
Cache means data stored in memory. Then why extra class given for MemoryCache?
What is the purpose of MemoryCache class and when is it used instead of normal cache class?
Just see the below example code
private void btnGet_Click(object sender, EventArgs e)
{
ObjectCache cache = MemoryCache.Default;
string fileContents = cache["filecontents"] as string;
if (fileContents == null)
{
CacheItemPolicy policy = new CacheItemPolicy();
List<string> filePaths = new List<string>();
filePaths.Add("c:\\cache\\example.txt");
policy.ChangeMonitors.Add(new
HostFileChangeMonitor(filePaths));
// Fetch the file contents.
fileContents =
File.ReadAllText("c:\\cache\\example.txt");
cache.Set("filecontents", fileContents, policy);
}
Label1.Text = fileContents;
}
What does the above code do? Is it monitoring file content change?
HttpRuntime.Cache gets the Cache for the current application.
see here
msdn
MemoryCache is a cache stored in memory.
Represents the type that implements an in-memory cache.
msdn
Here is an excellent blog that will clear all your concerns blog
Just few lines taken from this blog.
msdn says this
The Cache class is not intended for use outside of ASP.NET applications. It was designed and tested for use in ASP.NET to provide caching for Web applications. In other types of applications, such as console applications or Windows Forms applications, ASP.NET caching might not work correctly.
Although Microsoft has always been adamant that the ASP.NET cache is not intended for use outside of the web. But many people are still stuck in .NET 2.0 and .NET 3.5, and need something to work with.
Microsoft finally implemented an abstract ObjectCache class in the latest version of the .NET Framework, and a MemoryCache implementation that inherits and implements ObjectCache for in-memory purposes in a non-web setting.
System.Runtime.Caching.ObjectCache is in the System.Runtime.Caching.dll assembly. It is an abstract class that that declares basically the same .NET 1.0 style interfaces that are found in the ASP.NET cache.
System.Runtime.Caching.MemoryCache is the in-memory implementation of ObjectCache and is very similar to the ASP.NET cache, with a few changes.

Are controller factories neccessary when using Ninject in ASP.NET mvc 4

I am at a loss of what to do with the multitude of documentation available through google in .net as regards using Ninject with asp.net mvc 4
First of all, i want to know if Controller factories are neccessary in asp.net.
Also, is constructor injection really the only way we can do dependency injection with MVC 4 because property injection and method injection does not seem to work when i use them with my controllers
I am not an expert on Ninject but as far as i know, i am only using it to link my DataSource Interface and my EfDb Class to the rest of my application.
If you need a good book that has a Real Application built around Ninject try:
Pro ASP.NET MVC 3 Framework, Third Edition
or
Pro Asp.Net Mvc 4
There are very few lines of code i am usually concerned with
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null
? null
: (IController) ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IDataSource>().To<EfDb>();
}
}
Then register your NinjectControllerFactory in Global.asax.cs with:
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
As you can see, this class use Method Injection using private void AddBindings(). This makes it very easy if you are following Test Driven Development (TDD)
See the documentation here:
https://github.com/ninject/ninject.web.mvc/wiki/Dependency-injection-for-controllers, "The only thing that has to be done is to configure the Ninject bindings for its dependencies. The controller itself will be found by Ninject even without adding a binding."
NInject will automagically set up your controller dependencies (provided it has a binding for those types).

Using Microsoft Unity 2.0 framework with a web application

When using Unity 2.0 for dependency injection within a web application, it appears that user controls, pages, etc will all need make explicit calls to retrieve the container and "fetch" the dependencies … so using the annotations like [dependency] won't offer any value. This is likely since the location of the container (application context, http context cache, etc.) is unknown in the web configuration.
Since Unity itself provides method interception, isn't there a way to "tell" unity how to fetch the container correctly when you build your own web application? Rather than having to create base classes for page, etc.?
The problem is that the WebForms Pages and Controls are not set up to allow for construction by dependency injection, so Unity never gets invoked at all unless the class invokes Unity itself. I've found the best pattern in these cases is to invoke the DI framework in the constructor via a Service Locator and then use annotations to mark dependency properties. Something like this:
public MyPage()
{
// Injector is a wrapper class so you can change the underlying DI framework
// later if necessary.
Injector.Inject(this);
}
[Dependency]
public SomeService MyService {get;set;}

Ninject.Web (webforms extension), injecting outside of a webform page?

I've been using the Ninject.Web extension to inject business objects, repositories, Entity Framework context etc into my application. This works very well using the [Inject] attribute which can be applied within a webform that inherits from PageBase. I am now running into a snag as I am trying to write a custom membership provider that needs injection done inside of it but of course this provider is not instantiated from within a webform. Forms Authentication will instantiate the object when it needs it. I am unsure how to about doing this without having access to the [Inject] attribute. I understand that there is an application level kernel somewhere, but I have no idea how to tap into it. Any suggestions would be greatly appreciated.
You don't have to use the service locator pattern, just inject into properties of your custom membership provider in Application_Start. Assuming you've registed the providers properly you can do this with something like:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
// Inject account repository into our custom membership & role providers.
_kernel.Inject(Membership.Provider);
// Register the Object Id binder.
ModelBinders.Binders.Add(typeof(ObjectId), new ObjectIdModelBinder());
}
I've written up a more in depth explanation here:
http://www.danharman.net/2011/06/23/asp-net-mvc-3-custom-membership-provider-with-repository-injection/
You do a IKernel.Inject on the the instance. Have a look at the source for the Application class in the extension project you're using.
In the case of V2, it's in a KernelContainer. So you need to do a:
KernelContainer.Inject( this )
where this is the non-page, non application class of which you speak.
You'll need to make sure this only happens once - be careful doing this in Global, which may get instantiated multiple times.
Also, your Application / Global class needs to derive from NinjectHttpAppplication, but I'm sure you've that covered.
you may need to use the Service Locator pattern, since you have no control over the creation of the membership provider.

Resources