Posts

Showing posts with the label asp.net-core

Moq class with constructors ILogger and options netcore 2.1 vs2017 getting error

Moq class with constructors ILogger and options netcore 2.1 vs2017 getting error I need to mock a class that has parameters in the constructor by I cannot figure out how you do it using moq. It crashes Constructor arguments cannot be passed for interface mocks. See my attempt below: [Fact] public async Task MyTest() { var mySettings= GetMySettings(); var mySettingsOptions = Options.Create(mySettings); var mockLogger = Mock.Of<ILogger<MyClass>>(); var mock=new Mock<IMyClass>(mySettings,mockLogger); mock.Setup(x=>x.DoSomething(It.IsAny<string>().Returns("todo"); } public class MyClass : IMyClass { private readonly ILogger<MyClass> logger; private readonly MySettings mySettings; public MyClass(IOptions<MySettings> settings,ILogger<MyClass>logger) { this.logger = logger; this.mySettings = settings.Value; } ...

Entity Framework core : DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

Entity Framework core : DbContextOptionsBuilder' does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver' I'm new to EF core and I'm trying to get it to work with my asp.net core project. I get the above error in my startup.cs when trying confiugure the dbcontext to use a connection string from config. I'm following this tutorial : https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro startup.cs The problematic code in startup.cs : using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.SpaServices.Webpack; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.EntityFrameworkCore; using tracV2.models; using tracV2.data; namespace tracV2 { public class Startup { // This metho...

My application runs slower after consecutive http calls

Image
My application runs slower after consecutive http calls I am running .Net Core 2.1 and NPGSQL 4.0 on a mac. I am doing performance tuning and trying to run 100 http requests and measure performance using Jmeter . As you can see below the 1st request takes 550 milli-seconds, 25th request takes 3.9 seconds and the last final 100th request takes 12.7 seconds . Is there something that I can do to improve the performance of my application ? I ran these tests in release mode and the database is returning every request within 40 milli-seconds. The requests are suppose to be executed all-together but by the times you can see that every request takes longer than the previous one . public class HomeController : Controller { public static string ConnectionString = "Host=localhost;Username=postgres;Password=password;" + "Database=dbname;port=port;CommandTimeout=50000;TIMEOUT=1024;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=100"; public async Task<String> Te...

ASP.NET Core 2 - Angular & JWT Authentication

ASP.NET Core 2 - Angular & JWT Authentication Problem : I seem unable to fetch the User or any user-related data (e.g. UserID ) in any controller after the token has been recorded to browser local storage. User UserID I've set a breakpoint and studied HttpContext member of ControllerBase instance (the client app makes request + the auth_token is kept in local storage at this stage). HttpContext ControllerBase auth_token You only can extract the referrer url from Headers but there's no info about tokens . Headers tokens Even if you create a cookie for the token - the Request doesn't have it (0 cookies found at all). Request Perhaps I misunderstand the concept of how authorization works. Here's the bit I misunderstand most - how does ASP.NET Core fetch the token from the request made by client app - it must be kept in headers? ASP.NET Core token Also, could anyone share a working example of JWT Authentication where Angular & ASP.NET Core are separate sol...

Injecting IOptions to static class

Injecting IOptions<> to static class I would like to keep the class as static. Is there any workaround to inject IOptions<EncryptionSettings> without modifying the access modifier? IOptions<EncryptionSettings> public static class Encrypter { private static readonly Encoding encoding = Encoding.UTF8; private static readonly EncryptionSettings _encryptionSettings; public static Encrypter(IOptions<EncryptionSettings> encryptionSettings) { _encryptionSettings = encryptionSettings.Value; } public static string Encrypt(string plainText) { (...) } public static string Decrypt(string plainText) { (...) } static byte HmacSHA256(String data) { (...) } } 'Encrypter.Encrypter(IOptions)': access modifiers are not allowed on static constructors 'Encrypter.Encrypter(IOptions)': a static constructor must be parameterless why is it a static cla...