I am integrating app insights into our AspNet Core app(Target Framework .Net 4.7.1). I have two queries regarding app insights integration.
I am using SimpleInjector IOC, so does it make sense to have below line of code to inject AI into Asp Net Core DI?
services.AddApplicationInsightsTelemetry
I'm having my own Logger class which initializes TelemetryCLient and Logger class is injected using SimpleInjector. So removing above line code will cause an issue or lack of feature from ASPNet Core perspective?
In Asp.Net when we use to add AI it uses to add ApplicationInsights.config file which contains TelemetryInitializer's and TelemetryModules. Whats the best parctice in AspNet Core 2.1 for this? How do I add following TelemetryInitializers?
HttpDependenciesParsingTelemetryInitializer
AzureRoleEnvironmentTelemetryInitializer
AzureWebAppRoleEnvironmentTelemetryInitializer
OperationCorrelationTelemetryInitializer
etc...
Thanks in advance!!!
services.AddApplicationInsightsTelemetry is the easiest way to add application insights to your project. It sets up auto-collection modules for Requests, Dependencies etc, sets up default TelemetryInitializers, TelemetryProcessors (for sampling, live metrics etc.)
if you don't use services.AddApplicationInsightsTelemetry, then you have to programmatically setup all modules/initializers/sampling etc yourself.
There is no ApplicationInsights.config file, so pretty much every customization of the config is to be done through code. Following shows how to add/remove telemetry initializers.
https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Custom-Configuration#configure-telemetry-initializers
Related
I can't find a solution to change endpoint configuration for Asp.net Core HealthCheck UI at runtime.
It can be configured in appsettings.json, or in Startup, but I need to control endpoints at runtime. (e.g. based on integration events)
Any idea?
I found a solution. Using DatabaseStorage, changes in table Configurations is reflected in UI. So implementing simple CRUD features on this table solve the problem.
So much I could understand, Kephas does not provide its own DI container, but it builds adapters on existing ones (Kephas has its own dependecy injection container. Why another framework, why not use an existing one?). Existing adapters are for System.Composition (MEF2) and, as I learned recently, for Autofac (starting with version 6.5.0, Cannot use constructors in open generic services with Kephas).
However, ASP.NET comes with its own implementation of a DI container. In this regard, is it possible to use Kephas with ASP.NET Core now, and if yes, how?
Starting with version 6.5.0, Kephas will provide also an ASP.NET Core adapter so, yes, it will be shortly possible to use Kephas with ASP.NET Core. However, the built-in Dependency Injection does not have all the features Kephas requires, naming metadata and lazy instantiation. There will also be an adapter for Microsoft.Extensions.DependencyInjection, but without the aforementioned functionality, so I do not really recommend it. The Autofac adapter is the recommended one (event the Microsoft ASP.NET Core recommends it for advanced scenarios).
On the other hand, you could let Kephas manage its dependencies using MEF2 or Autofac and provide to ASP.NET Core a service provider aggregating the default one (or the one of your choice) and the one from Kephas. This has the following drawbacks:
You will end up with two containers.
The Kephas container will not have access to the services provided by ASP.NET Core.
Is there a way to enrich Serilog log data in ASP.NET Core 2.1 application, similar to how enrichers WithUserNameand WithHttpRequestUserAgent work for classic ASP.NET (System.Web)?
I tried implementing ILogEventEnricher interface, but it does not work, because I am not able to gain access to RequestContext from my custom enricher.
As pointed out in the comments it seems like Add Username into Serilog would serve your purpose and would also be the duplicate of this issue.
I have developed a new project based on ASP.Net core.
I have moved all my EF code (Models,mappings, DbContext) into a dedicated DAL class library in order to follow the Single responsibility principle of the SOLID rules.
However, I need now to add authentication into my project and would need to add the following into my Startup.cs of my Web project as shown in different tutorials:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
It would involve adding the Microsoft.AspNetCore.Identity.EntityFrameworkCore package and it seems to me that I start breaking the SRP rule by having this package included into my Web project.
Would it possible to move all the identity code (Services, models) as an external class library as I did for the DAL.
Since identity code has both logic and UI (login/logout, register etc), it needs to be an web app.
There are two options IMO:
Make identity as a separate web app. Since Asp.Net Core Identity supports OAuth2 (OAuth2 has support on interactive grants such as code grant), users will be redirected to this web app end point during login/register process.
Combine identity controllers with yours and move identity data to your DAL library, see this: https://www.codeproject.com/Articles/1156558/ASP-NET-Core-Moving-IdentityDbContext-and-EF-model
The first option makes better option if SRP is important to you. If redirecting to a different URL seems like a bad user experience for you, then the second option may be better.
I'm having my own research about the exact same question, found this thread you can read about the implementation here, although it is not related to .NET Core class library in particular.
I believe the principal is similar and you can find your way through it.
I also assume that it is not has to be implemented via a web app application as mentioned here.
yes you can just install Microsoft.AspNetCore.Identity.EntityFrameworkCore into class library
I'm very new to Asp.Net Web Apis(which Microsoft has made a part of MVC templates though we can use Web Api template independent of MVC)....Just a little background.
Coming back to my problem when my Web Service is called by a user then along the line of what my Web Service is serving comes a point where I have to deserialize a Json file to a generic C# collection and cache it in-memory and then the code inside one of the Controller actions(which is obviously a get method) checks for the in-memory cache and if it has that deserialized C# collection it gets it from there else its calls another method inside the controller which caches this generic collection in memory.
My question is ...is this possible to cache the stuff for a Web Api like what I described above...I'm quite familiar with Asp.Net page life cycle,caching and sessions etc. But not with Web Api....And my above explanation is just an abstract idea...not sure how to execute it, will it work? If yes then what namespaces would come in handy like System.Runtime.Caching or System.Web.Caching.
Your answers will be highly appreciated....
In the .NET Framework 3.5 and earlier versions, ASP.NET provided an in-memory cache implementation in the System.Web.Caching namespace. In previous versions of the .NET Framework, caching was available only in the System.Web namespace and therefore required a dependency on ASP.NET classes. In the .NET Framework 4, the System.Runtime.Caching namespace contains APIs that are designed for both Web and non-Web applications. ASP.NET Web API doesn't have dependency in System.Web.dll so I recommend you to using System.Runtime.Caching, you can put your caching logic anywhere even in separate .dll file and use it in your ASP.NET Web API project.