ASPNET Core Server Sent Events / Response flush

Client Side – wwwroot/index.html On page load, create an EventSource for the http://www.somehost.ca/sse url. Then write its events to the console. <body> <script type=”text/javascript”> var source = new EventSource(‘sse’); source.onmessage = function (event) { console.log(‘onmessage: ‘ + event.data); }; source.onopen = function(event) { console.log(‘onopen’); }; source.onerror = function(event) { console.log(‘onerror’); } </script> </body> Server Side … Read more

ASP.NET Core MVC controllers in separate assembly

Still an issue in ASP.Net Core 1.0, not sure if it’s by design now. Easiest solution is to do this in Startup.cs/ConfigureServices services.AddMvc() .AddApplicationPart(typeof(<class in external assembly>).Assembly) .AddControllersAsServices(); AddApplicationPart explicitly includes the assembly in searches for controllers. The call to AddControllersAsServices() will add all the discovered controllers into the services collection, and if you put … Read more

Alternative to System.Web.Security.Membership.GeneratePassword in aspnetcore (netcoreapp1.0)

Here’s a class/method, based on the source of Membership.GeneratePassword of that works on .NET Core: public static class Password { private static readonly char[] Punctuations = “!@#$%^&*()_-+=[{]};:>|./?”.ToCharArray(); public static string Generate(int length, int numberOfNonAlphanumericCharacters) { if (length < 1 || length > 128) { throw new ArgumentException(nameof(length)); } if (numberOfNonAlphanumericCharacters > length || numberOfNonAlphanumericCharacters < … Read more

Error handling (Sending ex.Message to the client)

Here is an simple error DTO class public class ErrorDto { public int Code {get;set;} public string Message { get; set; } // other fields public override string ToString() { return JsonConvert.SerializeObject(this); } } And then using the ExceptionHandler middleware: app.UseExceptionHandler(errorApp => { errorApp.Run(async context => { context.Response.StatusCode = 500; // or another Status accordingly … Read more

Access Web.config settings in Asp.Net Core App?

I kinda found the solution. The key to figuring it out was realizing that the AppDomain.CurrentDomain.SetupInformation.ConfigurationFile property wasn’t pointing to the web.config file but rather to the exe.config file for the executable running the website. Remember, under .net core, the website runs in its own process and it has its own exe. So the config … Read more

Value cannot be null. Parameter name: connectionString appsettings.json in starter

First of all, the “Data”: { “ConnectionStrings”: { “DefaultConnection”: “Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true”}, } Is slightly different from the structure you get when you add a “Asp.NET Configuration File” in Visual Studio. When you do that you get “ConnectionStrings”: { “DefaultConnection”: “Data Source=server;Initial Catalog=dbase;Trusted_Connection=True;MultipleActiveResultSets=true”}, without the “Data” JavaScript Object. So that’s why the extension method isn’t … Read more

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