Bootstrapping a .NET Core console application with EntityFrameworkCore and database first approach

So today I started implementing a small prototype for our search index microservice, which I should implement as a .NET Core console application. The datasource of this index process is a MS-SQL database. I decided to connect to it via the library EntityFrameworkCore. As I am expected to work with an existing database, I will also choose to implement this using a database first approach. After the initial setup I realized, that this was not as easy as it was with the .Net Standard EntityFramework Library, so I will share my learnings with you in this small post.

Create a new .Net Core Console Application

To start off, create a new .NET Core Console Application. I used Visual Studio 2017 15.8.7 for this tutorial.

Click File – New Project and choose “Console App” under the category “Visual C# – .NET Core”. Give it a name and choose the location where you want to save your source files and click on OK to create the empty console project. A hello world example console application will be created for you.

Add required nuget packages

After creation, add the following nuget packages to your project. Either by using the package manager console or the GUI (right click on the project – “Manage NuGet packages…”.

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.SqlServer.Design
  • Microsoft.EntityFrameworkCore.Tools

Here is a small snippet you can use to install those packages with your package manager console:

Create model from database – Database-First approach

As this is a database first approach, I will show you how to create your model classes from an existing database. I’ve used one of the Adventure Works sample databases from microsoft for this task. I’ve restored one of the .bak files into a local SQLExpress instance running on my local development machine.

After restoring your sample database (you may skip this step and just use your development database) we can start to generate our model classes. I recommend to create a folder in your project that should contain all model classes, I have named it “Data”:

After that, open your package manager console once again and execute the following command to create your model classes:

After running this command, your model classes and database context class should be created in the data directory. Please refer to the documentation of dotnet ef dbcontext scaffold for further command line options.

If the following error occurs, build your project before running the model creation:

Your startup project ‘ConsoleApp1’ doesn’t reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

Add configuration string to appsettings.json

In my case I was required to manage the configuration string to the database with the appsettings.json file. Add a new appsettings.json file and enter your connection string to it, it should look like this:

Don’t forget to change the Copy to output directory setting of the appsettings.json file to “Copy if newer”.

To read this configuration file, add the following nuget packages to your project:

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json

Or paste the following commands into your packages manager console. Don’t forget to build your project after completion of the commands.

Add the following using to your Program.cs file:

Then you can read your connection string using the following snippet.

Work with your created database context

Before we can create a new database context instance we need a way to pass the connection string to the context constructor. To achieve this, I’ve decided to implement add a partial class for the database context and add the constructor which initializes the context with a connection string parameter. See the following code snippet as an example:

Now you can initialize a database context with your connection string from the appsettings.json file:

After creating the new database context instance, you may begin to implement your queries. I’ve used linq to showcase a small example query to the AdventureWorks database:

Conclusion

This blog post should give you a good starting point for a microservice connecting to a MS-SQL database using a database-first approach. You can download the full solution here: Example project EF Core

Please also consider to visit our new company homepage Jack Project, we would be happy to help you to increase your W.A.P.D. perfomance!

Quickstart: Accessing local IBM WebSphere MQ with .NET C# (Part 2)

In the last post about IBM WebSphere MQ, I described how to install the current developer edition (v9.05) on your local Windows 10 Environment. In this blog post I will show, how to code a simple .NET C# Client to send and receive messages over the created message queue. This tutorial is based on the given code samples from IBM you get after installing the developer edition of the IBM Websphere MQ Server. In my installation they were located at “E:\Programme\ibmmq\Tools\dotnet\samples\cs\base”.

Go to part 1 of this series

Creating the new project

I used Visual Studio 2017, Community Edition, to code this sample.

You can download the full sample solution at: https://github.com/Kingviech/mq-cisharp-sample

To start, create a new C# Console Project start with file new project and choose the “Console-App (.NET Framework)” project template and name it “MqTest”.

After hitting the OK-Button your project is created and the Program.cs file is opened for you.

Adding references to the MQ-Libraries

Next up we will add the necessary references for the example application. To start this, right click on the references in the solution explorer of your created example project and click on the “Add reference…” menu entry.

Next click on the section “Explore” to search in the file explorer for the DLL needed for implementing the MQ-Client.

The DLL needed for this project is located inside the installation folder of your local IBM Websphere MQ installation, in my case it was located at the following file path: E:\Programme\ibmmq\Tools\Lib\amqmdnet.dll

After successfully adding the reference, it appears in the list of project references in the solution explorer.

Connecting to the Queue Manager

In the last part of this tutorial we created the queue manager QMA which we will connect to in this part of the tutorial.

Before starting out, add the following usings at the top of your Program.cs file:

After that we create an instance of Type MQQueueManager with the queue manager name as a constructor parameter:

Connecting to the queue

After successfully connecting to the queue manager we are ready to connect to the simple local queue we created in the last part of the tutorial which was named “QMA1”. We can create a connection to this queue in a similar way as we created the queue manager. We create an instance of type MQQueue and pass the queue name as constructor parameter. Additionally, we add flags to the second constructor parameter to tell the MQQueue instance, that allow to read the output of the queue and allows us to create messages in the queue. Additionally, the flat “MQOO_INQUIRE” was added, to be able to read attributes from the queue object itself.

Sending a simple “Hello, World!” to your queue

For sending a simple text message to the queue, we must prepare an instance of the MQMessage class and put it on the queue. The following code snippet shows how to do this.

 

After executing our code, we can look at the messages of our queue and we can see the sent message from our client (you can look at the messages of the queue in your IBM MQ Explorer installation, as described in the last part of this tutorial)!

Receiving messages from the queue

For receiving a message, the GET method of the queue object can be used. This method requires a target message object to write any received message to and message options for defining parameters like wait timeout. In the following code a simple get method is executed (you may want to execute this code in a loop, to keep receiving new messages from your queue).

When we run this program now, we receive the message sent previously by our program.

Cleaning up

After all your receiving is done, you may want to close the connections to the queue and disconnect from the queue manager. This is done by calling the corresponding methods of the MQQueue and MQQueueManager instances.

Conclusion

In this series I have shown how to connect to a local developer edition of IBM Websphere MQ. The last part, connecting to a remote MQ-Server, will be available after I have implemented the customer request in the next 2 months, stay tuned!

I hope this was helpful to anyone, any feedback is greatly appreciated!

 

Quickstart: Accessing local IBM WebSphere MQ with .NET C# (Part 1)

I was recently tasked to implement an integration between two systems of a customer. The task was to receive a XML-payload via a IBM WebSphere MQ Server hosted in their infrastructure, so my job was to implement a simple proof of concept.

We decided to give it a go with a .NET client, as almost all of our team has expertise in the C# programming language, so I started examining the .NET ressources for this API.

Go to part 2 of this series

Installing the MQ-Server locally

After reading through the documentation, I decided to install the developer edition of the MQ-Server on my local machine (win10) to give it a try. You can download the developer edition here (Current version as of this blog post was 9.05).

Unzip the installation files into a directory of your choice and start the Setup.exe.

After starting the installation wizard, you will be prompted to check if the system requirements are met by your computer.

In the tab “Network Configuration” choose the second option “No” if you don’t wish to run the MQ-Server with an AD-User.

In the last step you can choose the language of your local installation and then start the installation on your system.

For my first installation I chose the installation type “typical”, but you can change the installation location by choosing the installation type “custom”. You can also change the data folder and installation name if you choose the custom type. You can leave all other options at their default values for the time being.

After the server finished the installation, a second wizard opens for the configuration of the service instance. You can leave the default values if you install the server on a machine that is not in a network with a domain controller. After the configuration finished, you can start the IBM MQ Explorer.

Create a new Queue Manager

After starting the IBM MQ Explorer you will be greeted with the welcome page of the explorer. To understand the components of the IBM MQ Infrastructure, I recommend to read the redpaper “Websphere MQ Primer: An Introduction to Messaging and WebSphere MQ”.

To give a brief explanation: The MQ-Server consists of several queue managers which may contain several queues that can be used for asynchronous communication between applications.

We start off by creating a new Queue Manager in the MQ Explorer. To start this off, you can right click on the left navigation node named “Queue managers”:

Choose a name for your new queue manager and hit the finish button to create the queue manager.

After a short waiting time your new queue manager will be visible in the left navigation panel of your MQ-Explorer.

Create a new Queue

After creating the queue manager you are set for message delivery. Your last step before sending your first message is to create a queue for your queue manager.

To create a new queue, expand the navigation node of your queue manager, right click on “Queues”, expand “New” and choose “Locale queue”.

Enter a name for your new queue in the opened dialog and hit the finish button to create your new queue.

If the creation was successful, your new queue will be shown in the list of queues of your queue manager.

Sending test messages to your queue

To send a test message to your queue, right click on the queue in the list view of queues of your queue manager and click on “Queue test message…”.

You can now enter any message you like to send to your queue. I went with the famous “Hello, World!” string.

After hitting the send button, you can send more messages of close the dialog if you are done with sending messages to the queue. To view the messages in the queue you can right click on the choose the option “Show messages” in the context menu.

In the opened dialog you can see all messages, that are currently in your queue and waiting for processing. This view may come in handy in the next part of this series, when developing the C# client.

Conclusion

In this blog post I have shown how to install the developer edition ofIBM WebSphere MQ Server locally. In the next part of this series I will show you how to write a simple C# client that can send and receive messages from your created queue.