ASP.NET Core 6 register global exception filter from library - asp.net-core

In my ASP.NET Core 6 application, I usually register filters inside the call to builder.Services.AddControllers by adding to the Filters array. If I'm writing a reusable library, and that library wants to automatically register a filter, how would I do that? While I can of course tell consumers to manually add the filter themselves, I'd rather have it in a registration extension on the IServiceCollection object.
Back in the WebAPI days, for example, there was a GlobalConfiguration type object that could be used.

Related

ASP.NET Transfer data from controller action

There is a sales service implemented as a Telegram bot. I need to create a website control panel for this service. Since the service is a .NET application I am thinking to use ASP.NET Core technology.
How do I transfer data from the controller action to the Program class containing all the functionality of the service (maybe it is worth defining the Program as a static class)?
You may have misunderstood Asp.Net Core. .net core adopts the pipeline mode, that is, when you call the action in the controller, it will enter the middleware pipeline of Program.cs(.net 5 is Startup.cs), and execute in sequence according to the order of your middleware, adopting the principle of first in, last out. This means that if you follow the normal .net core logic, the value you get in the controller (except the parameters defined in the URL), you cannot pass it into Program.cs. When you successfully enter the action of the controller, Program.cs has been executed.
Not sure what your sales service looks like, but I think you can register it as a service and use it in your controllers using dependency injection.
Helpful link: ASP.NET Core Middleware.

Initializing UserManager<TUser> in class library project

I have an ASP.NET Core 5.0 MVC application and a .NET 5.0 class library called Application.Data. Due to separation of concerns, I decided that the DbContext and migrations should be contained within the data library. The DDL migrations work perfectly, but I'm having issues seeding AspNetCore.Identity users from within the data library.
Simply put, I want to access a UserManager<MyUser> instance in order to invoke the CreateAsync/AddToRoleAsync methods, but the UserManager constructor takes eight parameters that then also need to be instantiated. My understanding is that I could inject the user manager using the AddIdentity method to the service collection of my MVC project, but since my DbContext is contained within Application.Data, I wouldn't be able to run migration commands from within the MVC project.
What is the best course of action here?

Using global state in ASP.Net Core

In my current Asp.Net application I am finding a number core services that are slowly spreading throughout the application through the use of Dependency Injection. One of those services is the need to know who the current authenticated user is. I have a UserService that encapsulates the logic for getting the CurrentUser, but it seems like I am injecting this same service almost everywhere in my application. Is there a different method I could be using for accessing this Global service without constantly growing my constructors to inject yet another object into my classes? (of course I still want to maintain testability)

How to access ASP Core HttpContext in ASP 4.5 class library project?

I have ASP 4.5 website with a dependency on the business logic project, which is a class library built using .Net v4.5. Recently to expand the development, I have planned to introduce additional website project which is ASP Core. For the same, I have added the dependency of the business logic project. The business logic currently evaluates the request, cookies and session related stuff through HttpContext.Current instance. Which isn't working when I am accessing the business logic through ASP Core website.
Access HttpContext.Current
I have gone through the above question, and can know that why HttpContext.Current appears as null when I access it through ASP Core. And the answer to it suggests to populate the reference type IHttpContextAccessor through dependency injection. Now, the problem is, this interface belongs to the library Microsoft.AspNet.Http, and I don't see a way to add this to my business logic project.
Someone please help me out to access HttpContext into my business logic project.
I had this exact same need. The way I solved it was to create my own interfaces that live in my class library and I use those to get access to the current HttpContext regardless of whether the library is running under a 4.5 http context or a MVC Core http context.
To explain further, both the ASP 4.5 framework and the ASP Core Framework have an HttpContext object with associated Request and Response objects but they are defined in different namespaces and neither framework knows about the other framework's namespace. So What I decided is that I needed my library code to have access to an IHttpContext that was defined in one of my namespaces. And that IHttpContext would use an IHttpRequest and IHttpResponse that were also defined in my library's namespace. And finally, that IHttpRequest uses ISession, IHeaders and ICookies that are defined in my namespace.
With these interfaces defined, in my ASP 4.5 website I can now at the web layer create an HttpContext class based on my IHttpContext and have that class basically wrap the ASP 4.5 HttpContext object. My HttpContext object could then be passed into my library for use.
And in my MVC Core website I can now at the web layer create an HttpContext class based on my IHttpContext and have that class basically wrap the MVC Core HttpContext object. My HttpContext object could then be passed into my library for use just like it was when I was running under the ASP 4.5 environment.
So in the end, my library doesn't know which HttpContext object (4.5 or Core) it is actually accessing under the hood because it just knows that the object is has access to confirms to the IHttpInterface defined in it's library.
One final note, to help navigate name conflicts, I actually named my interfaces this way:
IAppHttpContext
IAppHttpRequest
IAppHttpResponse
IAppHttpSession
IAppHttpRequestHeaders
IAppHttpRequestCookies
Note that not all the functionality that is available in HttpContext 4.5 is available in MVC Core. The two are very similar but there are a few differences. The biggest difference is that MVC Core Session can only store byte arrays or strings whereas 4.5 session can store objects. So my IAppSession only supports storing byte arrays and strings and I have to make sure all my library's session needs work with that (all the objects that I need to store in session need to be serializable).
As you can imagine, implementing this is a bit of work, but in the end you will have a library that can access HttpContext and not care if it's running under a 4.5 HttpContext or a MVC Core HttpContext. Kinda neat.
Good luck.
i too had this same problem. I solved it by adding following dependency in my project.json file to add the http packages to class library
"Microsoft.AspNetCore.Http.Abstractions": "1.1.0"
then i used it like below
public class sampleclass
{
private readonly IHttpContextAccessor context;
public ISession GetSession()
{
return context.HttpContext.Session;
}
}
Thank you. Happy Coding :-)
Reference : http://benjii.me/2016/07/using-sessions-and-httpcontext-in-aspnetcore-and-mvc-core/

how implement mvc 4 project using crm 2011

we have crm 2011 and we want to develop asp.net mvc 4 project which uses crm 2011 db. What is the best way to do this?
Also we have these questions:
Can we use svcutil generated classes?
Which is more suitable for such project Database first approach or code-first approach or other?
This is assuming that you're using Windows Credentials
This is our current setup:
Defined two IOrganizationService property (instance, not static, since it's not thread safe) in a base controller class that is used by all controllers that is a lazy loaded OrganizationServiceProxy. One is just called Service, and is created with the default Windows Credentials of the App Pool user. The other is called UserService and is impersonating the logged in user.
Overrode the Dispose in the base controller to ensure that the OrganizationServiceProxy is being disposed.
After this initial framework is setup, it's all basic SDK calls from here. There is no issues using the srvcUtil generated classes, except that you can't store them in the session since they aren't marked as serializable (although you can store the Attributes collection).
As far as code first or database first, you'll want to create your entities in CRM first so you can generate your early bound classes and use them in the MVC site.