Posts

Showing posts with the label .Net Core

Can I Use HttpClientFactory In A .NET.core App Which Is Not ASP.NET Core?

Answer : According to the documentation HttpClientFactory is a part of .Net Core 2.1, so you don't need ASP.NET to use it. And there some ways to use are described. The easiest way would be to use Microsoft.Extensions.DependencyInjection with AddHttpClient extension method. static void Main(string[] args) { var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider(); var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>(); var client = httpClientFactory.CreateClient(); } Thanks for replies. So it is possible to use in console app. There are a few ways to do this, depending on what way you want to go. Here are 2: Directly add to ServiceCollection e.g. services.AddHttpClient() Use Generic host e.g. Add httpclientFactory in .ConfigureServices() method See here for blog post using in console app As one of the answers suggests, you do not need ASP.NET to use it However, you need a bit of work to ge...

Cache Busting Index.html In A .Net Core Angular 5 Website

Answer : Here's what I ended up with after combining a bunch of answers. My goal was to never cache index.html. While I was in there, and since Angular nicely cache-busts the js and css files, I had it cache all other assets for a year. Just make sure you're using a cache-busting mechanism for assets, like images, that you're managing outside of Angular. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // ... app.UseStaticFiles(); if (env.IsDevelopment()) { // no caching app.UseSpaStaticFiles(); } else { app.UseSpaStaticFiles(new StaticFileOptions { OnPrepareResponse = context => { context.Context.Response.Headers.Add("Cache-Control", "max-age=31536000"); context.Context.Response.Headers.Add("Expires", "31536000"); } }); } // ... app.UseSpa(spa => { spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions ...

Cannot Find Command 'dotnet Ef'?

Image
Answer : In my case, the tools folder didn't exist inside %USERPROFILE%\.dotnet\ so I had to run the command dotnet tool install --global dotnet-ef to install dotnet ef. Then I was able to run dotnet ef... This was the result of the above install command: Note to readers: If you haven't installed dotnet ef , you need to install it first: dotnet tool install --global dotnet-ef . The question-asker already did that. You need to do that first before the rest of this answer can help. How to fix this For Linux and macOS , add a line to your shell's configuration: bash / zsh : export PATH="$PATH:$HOME/.dotnet/tools/" csh / tcsh : set path = ($path $HOME/.dotnet/tools/) When you start a new shell/terminal (or the next time you log in) dotnet ef should work. For Windows : See this question on how to add to the PATH environment variable. You need to add %USERPROFILE%\.dotnet\tools to the PATH . What's going on? The .NET Core 3.0 (preview) ...

AddEntityFrameworkStores Can Only Be Called With A Role That Derives From IdentityRole In .NET Core 2.0

Answer : Long time since I asked this question, but here's how I deal with nowadays: Startup.cs services.AddIdentity<User, Role>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddScoped<RoleManager<Role>>(); Entites: public class User : IdentityUser<int> { } public class Role : IdentityRole<int> { } For same issue, you can look at this:https://github.com/aspnet/Identity/issues/1364

Correct Way To Return HttpResponseMessage As IActionResult In .Net Core 2.2

Answer : public class HttpResponseMessageResult : IActionResult { private readonly HttpResponseMessage _responseMessage; public HttpResponseMessageResult(HttpResponseMessage responseMessage) { _responseMessage = responseMessage; // could add throw if null } public async Task ExecuteResultAsync(ActionContext context) { var response = context.HttpContext.Response; if (_responseMessage == null) { var message = "Response message cannot be null"; throw new InvalidOperationException(message); } using (_responseMessage) { response.StatusCode = (int)_responseMessage.StatusCode; var responseFeature = context.HttpContext.Features.Get<IHttpResponseFeature>(); if (responseFeature != null) { responseFeature.ReasonPhrase = _responseMessage.ReasonPhrase; } var responseHeaders = _responseMessag...