Tuesday, December 5, 2017

If you are using or if you have used (I am sure you have) AutoMapper, Profiles lets you organize your mapping configurations in an easy manner.

In this post, let’s see how we can handle AutoMapper Profile dependencies using Custom Value Resolvers. As it’s always good to go with an example, let’s go with an example ASP.NET Core Web Application.

For an ASP.NET Core Web Application, AutoMapper can be configured with following easy steps.

You can add a class deriving from Profile class, and in the constructor you can setup your mapping configurations.

And here on MyClassDTO, SomeProperty can't be mapped directly, we will need to get the value by calling ISomeService.GetSomeProperty(int id). Imagine ISomeService is registered for dependency injection.

publicinterfaceISomeService
{
string GetSomeProperty(int id);
}

So what we would expect is we can get the MyProperty value as follows.

Sunday, November 26, 2017

I was trying to figure out an issue in a Web Application that I am currently working on and it has a RavenDB backend.

The code was doing some operations on the RavenDB database (behind the scene those method calls gets translated into API calls to RavenDB REST API), and I was using Fiddler to trace the API calls to RavenDB. But apparently, only some of the API calls were listed on Fiddler and most of the calls were not present. But RavenDB logs showed that it is receiving calls, so I was bit confused why Fiddler isn’t capturing the calls.

I had RavenDB IIS Web Application running on 8080 and I was using Url=http://localhost:8080;Database={MyDatabaseName} as the connection string. I just replaced localhost with my machine name and that was it. I am seeing all the API calls to RavenDB in fiddler.

So if you want Fiddler to capture all RavenDB calls being made from your application, instead of having localhost or 127.0.0.1, use the machine name.

So now everything is in-place to find out the real issue, I am back to it.

All seem be good, the application was running well. After sometime, I wanted to add a database migration, and when I tried to do Add-Migration, I was getting this weird error.

Unable to create an object of type 'T'. Add an implementation of 'IDesignTimeDbContextFactory&lt;T>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

Apparently the error turned out to be program.cs not having BuildWebHost method.

Wednesday, October 11, 2017

In this post let’s see how we can apply CORS policies for different scenarios (based on the route/path etc.) in an ASP.NET Core 2.0 Web Application.

As you already know, in ASP.NET Core projects, on the Startup.cs, we have 2 methods ConfigureServices and Configure, and these two will get called by the runtime. ConfigureServices is used to add services to the container, so we can use them through out the application. Configure method is used to configure the HTTP Request pipeline.

So moving to the topic, first thing we need to do is AddCors to IoC container, (again so we can consume this through out the application). (Note: these is an alternate way, and I will be describing that later in the post, keep reading forward)

Note: The precedence order is: Action, controller, global. Action-level policies take precedence over controller-level policies, and controller-level policies take precedence over global policies.

Custom

Imagine, you want to apply CORS policy based on HTTP Request route/path etc. You can use the power of Middleware for that kind of a scenario.

One approach would would be, you can use app.Map and app.MapWhen to branch off the HTTP Request pipeline and use app.UseCors as shown in above, but the downside is your HTTP Request pipeline is getting short-circuited. That of course you can merge back, but personally I don’t find it a good approach.

The other approach (which I personally find best) is, you can easily create a separate Middleware class for handling CORS in following way.

The whole piece of code is taken from CorsMiddleware.cs (cheers to Microsoft for going open source), and we can just modify it to match our need. Here I have modified it to apply CORS policy based on the request route/path. Now you can simply do following.

Tuesday, October 10, 2017

As you might know and as of today, for Entity Framework Core, Microsoft does not support Automatic Database Migrations as in Entity Framework Full. The reasons are described in this Issue. But it doesn't stop you from configuring automatic migrations by your own. You can just write something like below and call this inside Startup -> Configure method passing the IApplicationBuilder.

Thursday, August 17, 2017

With the release of ASP.NET Core 2.0, one of the new features that got introduced was Razor Pages. The easiest way to get yourself started on ASP.NET Core Razor Pages is with Visual Studio 2017 Update 15.3.

Once you have installed Visual Studio 2017 Update 15.3 and if you try to create a new ASP.NET Core Web Application,

New Project

You are presented with an improved dialog window.

New ASP.NET Core Web Application

When you selected ASP.NET Core 2.0 from the ASP.NET Core version selection dropdown, you are presented with multiple application templates. From there, when you select your template as Web Application, you can see that it uses Razor Pages.

Let’s go ahead and create a Web Application. Once the project is created, if you have a look at Solution Explorer, you should be able to see something like below.

Pages Folder

When I first had a look at this, for me it was familiar MVC file names and Web Forms like file structure (HTML file and a code behind file).

Now if you run the application, you should be seeing the familiar template.

Running

Now to explore what ASP.NET Core Razor Pages really is, let’s open up About.cshtml and About.cshtml.cs (I am going ahead with About files as there are less static content there).

About.cshtml

About.cshtml

Here the highlighted @page directive is one of the most important things. It enables, razor file to link with it’s related PageModel.

About.cshtml.cs

About.cshtml.cs

If you prefer, to improve productivity in development, you can have the content in code behind in the razor file itself.

While above will work fine, there is a advantage, above supports run-time compilation. That is pretty handy in development, you don't need to stop the debugger to do some change in PageModel class.

When the page is requested by http://localhost:xxxx/About, OnGet method will get called and when you want to post something, you can introduce OnPost method and it will get trigged upon POST requests. For async requests, you can name the methods as OnGetAsync, OnPostAsync etc.

Thursday, August 10, 2017

There was no session slides, I was totally focusing on showing the participants Docker in action. I have demoed how we can run a containerized ASP.NET Core application in a Linux container using only docker command. Then I have showed what Visual Studio 2017 has introduced to improve the productivity in building containerized applications and the use of docker-compose command.

Tuesday, June 20, 2017

This is likely going to be a quick post. I wanted to have a Docker Container running ASP.NET Core Web Application powered by React and Redux. Luckily there are nice SPA templates introduced by Microsoft, and there we have React, Redux template.

MVC ASP.NET Core with React.js and Redux

I created the application and did a publish. Then I have added a Dockerfile with following configs.

Right now I have a Docker support enabled ASP.NET Core MVC Application created (If you don't know how, you can read my above mentioned previous post).

Note: I have created the solution using Visual Studio 2017, but you don't need to have Visual Studio 2017 to create this solution. dotnet SDK and Visual Studio Code should be more than enough. For the next steps, we only need dotnet SDK and cmd/PowerShell.

Let's proceed. I have a clean solution, no obj/bin folders present yet.

No obj/bin folders

I have a PowerShell window opened on the root folder level.

PS C:\Users\Jaliya\Desktop\DockerComposeWebApp>

Next to do a Release build what I would do is do a dotnet restore and a publish like below.

But in that case the build would be done on my local machine, instead here I want to use a Docker Container to do the building.

Now let’s open up the docker-compose.ci.build.yml in my local folder. Remember I did not write this file myself, Visual Studio 2017 created this for me when I have enabled Docker Support for my project (if you want to know how to get these docker-compose.xxxxx.yml files created when using Visual Studio Code, you can read this previous post of mine Running ASP.NET Core MVC Application inside a Docker Linux Container from Windows).

Here you can see that we have a service named ci-build which uses microsoft/aspnetcore-build Image and that is the official Image for building ASP.NET Core applications (version will be changed time to time). Then my current folder is getting mapped to src folder in the ci-build Container (which is not yet created). After Container is created and running, it’s working directory will get changed to src and the given command will get executed. And basically what the command does is a dotnet restore and a dotnet publish (here output path is set to .\obj\Docker\publish).

Wednesday, May 31, 2017

In this post let’s see how you can use Visual Studio 2017 to develop .NET Core application with Docker support enabled and see what happens when you build/debug.

Let’s start off by creating a ASP.NET Core Web Application (.NET Core) and I am naming it as DockerComposeWebApp.

ASP.NET Core Web Application (.NET Core)

Once you clicked on OK from above, you are prompted to select application type (of course, I am sure you have done this hundred times, if not thousands).

Enable Docker Support

And there is a new check box presented in the dialog window, that is Enable Docker Support. You can either enable Docker support from there, or as mentioned, you can enable it later.

And I am not enabling it right now, I am clicking on OK and the solution gets created. And to enable Docker support now, you can right click on the Project, select Add and then click on Docker Support (well, you should be able to right click on any root item, and do Add –> Docker Support, but not on Solution level nor file level).

Enable Docker Support

Once you enabled Docker support, a Dockerfile has been added to DockerComposeWebApp project and you can see a new project that is added to the solution named docker-compose with some files.

And if you have a look at the ItemGroup, that is what it gives Visual Studio the nested view in docker-compose project items. So basically there is two main docker-compose files, one is docker-compose.ci.build.yml and the other is docker-compose.yml.

Now let’s see what happens when you debug the application. Since our main interest here is in Docker side of things, let’s set the docker-compose project as the startup project. Same time you can see that the Debug target is changed to Docker (if you debug the solution having the Web App project as the startup project it will not be debugging on Docker).

Before hitting F5, let’s see is there any Docker Containers running.

docker images & docker ps -a

Nothing specific to our current project (DockerComposeWebApp).

Now let’s debug. After couple of seconds, the site is working (well, it should) and what we need to focus is the Build output in Visual Studio.