Reconfigure dependencies when Integration testing ASP.NET Core Web API and EF Core

@ilya-chumakov’s answer is awesome. I just would like to add one more option 3. Use ConfigureTestServices method from WebHostBuilderExtensions. The method ConfigureTestServices is available in the Microsoft.AspNetCore.TestHost version 2.1(on 20.05.2018 it is RC1-final). And it lets us override existing registrations with mocks. The code: _server = new TestServer(new WebHostBuilder() .UseStartup<Startup>() .ConfigureTestServices(services => { services.AddTransient<IFooService, MockService>(); … Read more

What is the simplest way to run a single background task from a controller in .NET Core?

You have the following options: IHostedService classes can be long running methods that run in the background for the lifetime of your app. In order to make them to handle some sort of background task, you need to implement some sort of “global” queue system in your app for the controllers to store the data/events. … Read more

Convert from HttpResponseMessage to IActionResult in .NET Core

You can return using hardset status codes like Ok(); or BadRequest(); Or return using a dynamic one using StatusCode(<Your status code number>,<Optional return object>); This is using Microsoft.AspNetCore.Mvc Below is this.StatusCode spelled out a little more: /* “this” comes from your class being a subclass of Microsoft.AspNetCore.Mvc.ControllerBase */ StatusCodeResult scr = this.StatusCode(200); /* OR */ … Read more

Differences between ASP.NET Web Application (.NET Framework) vs ASP.NET Core Web Application (.NET Framework)

Option 1 – ASP.NET Web Application Option 3 – ASP.NET Core Web Application Although both project templates use Full .Net Framework, Option 1 is for creating projects using legacy version of ASP.NET MVC in which we can use Global.asax. Option 3 is totally new concept in which wwwroot folder, using task runners and everything is … Read more

Logging within Program.cs

Accidentally stumbled upon the answer after googling a bit more. using System; using Microsoft.Extensions.Logging; namespace ConsoleApplication { public class Program { public static void Main(string[] args) { var logFactory = new LoggerFactory() .AddConsole(LogLevel.Debug) .AddDebug(); var logger = logFactory.CreateLogger<Type>(); logger.LogInformation(“this is debug log”); } } } Kudos to https://askguanyu.wordpress.com/2016/09/26/net-core-101-e06-net-core-logging/

How to auto log every request in .NET Core WebAPI?

ActionFilter will work until you need to log only requests processed by MVC middleware (as controller actions). If you need logging for all incoming requests, then you need to use a middleware approach. Good visual explanation: Note that middleware order is important, and if your logging should be done at the start of pipeline execution, … Read more

How to include XML comments files in Swagger in ASP.NET Core

For .Net Core 2 up to 8* versions it’s slightly different, for those who come across it using a newer version you would create your private void ConfigureSwagger(IServiceCollection services) constructor, add the reference to swagger services.AddSwaggerGen(c => { c.SwaggerDoc(/*populate with your info */); then define a new parameter which will be the path for your … Read more

Will CORS policy prevent resource access from non-browser requests?

However, does this prevent Http requests from a CURL, or other native applications/web-servers (ie. a request written and run via PHP) from successfully retrieving data from that resource? No, CORS config won’t prevent non-browser stuff from successfully retrieving your resources. The same-origin policy is enforced only by browsers. It’s not enforced by servers. (And CORS … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)