Skip to main content

Posts

Showing posts from March, 2018

Entity Framework Core 2.0–Owned types

EF Core 2.0 (re)introduces the concept of Complex types which are called Owned types in EF Core. I find the new naming confusing but hey that’s me… Anyway let’s have a look at how we can use these Complex Owned types. We’ll start by creating a Supplier class with an Address; Important to notice is that the Address does not have it’s own Id but is embedded into the Supplier class. To configure the Owned types mapping, you have to use the OwnedOne method in the mapping file. If you want to further configure the properties of the Owned type, you’ll have to repeat the statement for every property in the Owned type: That's it!

Debugging an ASP.NET Core project in IIS

After changing my launch target to IIS for my ASP.NET Core project, I got the following error from Visual Studio: With a recent update to Visual Studio 2017(15.3), Microsoft added support for debugging ASP.NET Core applications against IIS. To be able to use you have to modify your existing Visual Studio Installation. So open up the Visual Studio Installer and select the Development time IIS support component which is listed as optional component under the ASP.NET and web development workload. This will install the ASP.NET Core Module which is a native IIS module required to run ASP.NET Core applications on IIS. Click on Modify to start the installation. After the installation has completed, restart Visual Studio and try to run your app again. More information: https://blogs.msdn.microsoft.com/webdev/2017/07/13/development-time-iis-support-for-asp-net-core-applications/

Azure Strategy and Implementation Guide

Microsoft created another free e-book for every organisation that thinks about moving to the cloud: the Azure Strategy and Implementation Guide for IT Organizations . This book will guide you through the first steps of you cloud implementation process. What is covered: Chapter 1: Governance – This chapter covers the starting points, from the aspirational “digital transformation” to the important tactical steps of administration and resource naming conventions. Get an overview of topics such as envisioning, to cloud readiness, administration, and security standards and policy. Chapter 2: Architecture – This section takes a longer look at security, touches on cloud design patterns, and provides several visual representations to help you understand network design. Chapter 3: Application development and operations – Here, we cover backup and disaster recovery, as well as application development from an IT operations and management perspective. You’ll learn about the culture of Dev...

Impress your colleagues with your knowledge about... the DispatchProxy class.

Sometimes when working with C# you discover some hidden gems. Some of them very useful, other ones a little bit harder to find a good way to benefit from their functionality. One of those hidden gems that I discovered some days ago is the DispatchProxy class. DispatchProxy is a new class in .NET Core that allows you to apply Aspect Oriented Programming techniques using interception. Let’s try it out by creating a really simple LogInterceptor: The DispatchProxy class is a little cumbersome and we have to capture our target method using a little dynamic magic(casting would have worked as well): Now let’s create an interface and a corresponding implementation: As a final step we create an intercepted version of our class and invoke our method: Here is our output:

Swashbuckle : Allow file import through the Swagger UI

One of the API’s I’m building allows you to import files to a database. To simplify testing I wanted to allow the consumers of my API to test the API directly through the Swagger UI. Problem is that out of the box Swashbuckle(the Swagger implementation for .NET) has no clue how to interprete the IFormFile. Let’s fix that by introducing an IOperationFilter: And let’s not forget to register that IOperationFilter in our Startup.cs file: Now if we run our application and browse to the swagger endpoint(/swagger), when we try to execute our Upload operation, we get a nice Import button:

NHibernate supports .NET Core!

The NHibernate team is back on a roll! After a previous announcement where they released the long awaited(pun intended) async/await support, they are back again with some other great news. NHibernate 5.1 is released with the support of .NET Core 2.0 and .NET Standard 2.0. I’m missing way too many features in EF Core, so I’m glad that at least we got another option back.

Entity Framework Core 2.0–DbContext pooling

Version 2.0 of EF Core introduces a new way to register your DbContext that transparently introduces a pool of reusable DbContext instances. This was the code you had to use before 2.0: Typically this code resulted in creating a new DbContext for each request which introduces an extra performance penalty for your requests. In EF Core 2.0 you can replace this by: If this method is used, at the time a DbContext instance is requested by a controller it will first check if there is an instance available in the pool. Once the request processing finalizes, any state on the instance is reset and the instance is itself returned to the pool. Warning: As all DbContext instances should be completely equal, you are limited in what you can do in the OnConfiguring() method of the DbContext.

Entity Framework Core - No executable found matching command "dotnet-ef"

After adding the EntityFramework nuget package to my ASP.NET Core project and creating our DbContext(see below) I was ready to generate a first migration : I opened the Package Manager Console in Visual Studio, browsed to the project folder(!),  and executed dotnet ef . I ended up with the following error message: dotnet : No executable found matching command "dotnet-ef" At line:1 char:1 + dotnet ef + ~~~~~~~~~     + CategoryInfo          : NotSpecified: (No executable f...and "dotnet-ef":String) [], RemoteExcep tion     + FullyQualifiedErrorId : NativeCommandError To solve this install the following 2 extra packages: Microsoft.EntityFrameworkCore.Tools: Contains the Entity Framework Core Package Manager Console Tools. Includes Scaffold-DbContext, Add-Migration, and Update-Database. Microsoft.EntityFrameworkCore.Tools.DotNet : Contains the Ent...

ASP.NET Core Docker error: An assembly specified in the application dependencies manifest (MyApp.deps.json) was not found

Yesterday I had some trouble to get my docker build up and running. However after I succeeded and tried to run my container using docker-compose up , I got the following error message in the container log: An assembly specified in the application dependencies manifest (MyApp.deps.json) was not found:     package: 'Microsoft.AspNetCore.Antiforgery', version: '2.0.1'     path: 'lib/netstandard2.0/Microsoft.AspNetCore.Antiforgery.dll'   This assembly was expected to be in the local runtime store as the application was published using the following target manifest files: aspnetcore-store-2.0.3.xml The problem is caused by the fact that the ASP.NET Core SDK version on the build image(2.0.0) and the referenced version in my project(2.0.3) didn’t match. Easiest solution I could find was resetting the package reference to 2.0.0 in my csproj file: <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> ...

ASP.NET Core Docker Build error - COPY failed: no source files were specified

Last week I had some time to play with the support for Docker in Visual Studio. Creating the Docker enabled project First thing I tried was creating a simple ASP.NET Core application with Docker enabled: Open Visual Studio Go to File –> New Project . Select the Web section and choose the ASP.NET Core Web Application template. Click OK. On the next screen choose the Web Application template(or any other) and make sure that the Enable Docker Support checkbox is checked. Click OK to create the project. 2 projects are created: One Docker-Compose project that contains a yaml configuration to compose multiple containers One web application project that contains a Dockerfile to compile and build your docker container. Running the application in a docker container Now that the application is created, it is time to build and run the application. I opened a command prompt with the path set to the project containing my dockerfile a...

Drawing your future

During my flight to Seattle last week I had time to watch some Tedx videos. One of the videos that really inspired me was Draw your future by Patti Dobrowolski. Time to take out your pencils and start drawing…

Angular error: Cannot find module '@angular-devkit/core' (again)

I blogged previously about the following error message: Cannot find module '@angular-devkit/core' I got some feedback that the second solution I shared(updating the Angular CLI version) didn’t work for everyone. Turns out that you also need to update the local version of the Angular-CLI: npm install --save-dev @angular/cli@latest npm install More information about updating the Angular CLI can be found in the documentation: https://github.com/angular/angular-cli

Time to give Resharper another try

Over the years I always had a love/hate relationship with Resharper. I loved the rich refactoring options but in the end I always removed Resharper again as the impact on performance was too big. I always prefer a faster IDE(one of the reasons why I’m using VSCode a lot today). But last week I decided to give Resharper another try. After installing the extension, I started my Visual Studio instance. The first message I got was not very promising: But we’ll see, I promise that I will try it again for a month and share my experiences in a future blog post. Happy refactoring!

ElasticSearch–Should clause

A colleague asked me for help when his ElasticSearch query wasn’t returning the results he expected. Let me first explain what he tries to achieve using a simplified example: In our search index we have products that are linked to a specific category. We want to search for specific product values AND only return results for the categories we specified In SQL this would translate to a query similar to the following: ‘SELECT * FROM PRODUCTS WHERE (ProductBrand=’Apple’ OR ProductName=’Apple’ ) AND CATEGORY=’Hardware’’   Here is the first attempt to write this using NEST: We noticed however that instead of executing an AND, we got OR behavior instead. We got all products back where category is ‘Hardware’ but not limited to ‘Apple’ products. Why is this happening? First important to notice is that the query above does a full text search and is not filter query. Combining multiple clauses with and , or and not logic becomes more subtle. Queries dec...