Categories
.NET Continous Integration Docker MS SQL Server Programming Project Management

Automate Testing: Gitlab, Dotnet

I always want to test my .NET based application on my personal Gitlab based repo. But since, my old project is too much a mess to do the effort for. So, when we got to rewrite the whole project, I was happy to get my experience on Unit Testing with automation of CI. I still don’t do much of CD as my development environment is still limited to development only.

So, I read about it and using Gitlab’s YML configuration, I was able to run the xUnit based Unit Test easily. It was simple too, call a MS/dotnet docker as base image, then dotnet build and later dotnet test .

But the problem arise when I have to connect to database and then I cannot get SQLCMD command to work, though I get the MS SQL Server based service to started, but until that command runs which is not present in ms dotnet docker, I cannot reload my database easily. For that I have to write code to bootstrap and create database. So, I try to search for it, and after lot of fail attempt and surprisingly no example of how to use database with YML configuration other than basic example for Mysql or Microsoft’s own docker file example. I found no info on this.

So, I decide to create a new docker image, I want to do it my way. So, I use a Ubuntu Docker image, install dotnet 2.2 on that image with mssql-cmd tools. I didn’t install MS SQL Server as I can use that as service. So using this docker image, I get my Unit testing working. This help me automate my project workflow and help me get best out of me, without need to worry about hidden errors.

My this docker image is available for anyone need it at Docker hub:

https://hub.docker.com/r/archergod/dotnetcoredb

Categories
.NET C#.NET Programming

Extensions Method in .NET

Recently I use extension method in C# project. I knew those for years, but really never get a chance to use them where they are most effect. I have couple of extension method in my project, such as ToInteger(), for a string to convert a String to Integer, which I used more frequently in my project. But that doesn’t seems a good use of technology to me.

But I now found a perfect use for extension method. i.e. I was upgrade my code from old ASP.NET project to MVC and for this I am making a class library of all objects. Now, this scenario need me to make a conversion from old enum to new enum  which are now part of namespace of my class library, rather than sitting around in App_Code folder. Here I make a extension method that convert a OLD enum to new name space base. This ease lot of my works.

Method goes like:


public static NewType GetSaleType(this OldType s)
{
	NewType r = NewType.Regular;
	switch (s)
	{
		case OldType.BK: r = NewType.BK; break;
		case OldType.IL: r = NewType.IL; break;
		case OldType.MM: r = NewType.MM; break;
		case OldType.PC: r = NewType.PC;break;
		case OldType.Regular: r = NewType.Regular; break;
		case OldType.Trigger:r = NewType.Trigger; break;
	}
	return r;
}
Categories
.NET ASP.NET

ASP.NET MVC 5: Session not working

Just encounter an issue with ASP.NET MVC 5, the sessions are not working. i.e. when I put a value in controller like Session[“mysession”], it do not shows the same value in Views or in fact anywhere else. Search online, some create custom classes to manage them, and some give different patch, finally got the easiest solution. Just add “Session_Start” function call in Global.asax file and we are good to go.

Categories
.NET C#.NET

C#: Code to load Object from Database

It is often when managing old project that do not use ORM coding, we need to load object from database. In such scenarios it is quite boring to code all 30-40 property to load from DB. So, I wrote this simple code which does it quite well for me, the only catch is you need to define object property same as their DB column name. So, here is the quick code for the same.

public void LoadRS(DataRow rs)
    {
        foreach (PropertyInfo propertyInfo in this.GetType().GetProperties())
        {
            try
            {
                if (propertyInfo.CanRead)
                {
                    if (rs.Table.Columns.Contains(propertyInfo.Name))
                    {
                        
                        switch (propertyInfo.PropertyType.Name)
                        {
                            case "String":
                                propertyInfo.SetValue(this, Convert.ToString(rs[propertyInfo.Name]), null);
                                break;
                            case "Int64":
                                propertyInfo.SetValue(this, long.Parse(Convert.ToString(rs[propertyInfo.Name])), null);
                                break;
                            case "Int32":
                                propertyInfo.SetValue(this, int.Parse(Convert.ToString(rs[propertyInfo.Name])), null);
                                break;
                            default:
                                propertyInfo.SetValue(this, Convert.ChangeType(Convert.ToString(rs[propertyInfo.Name]), propertyInfo.PropertyType), null);
                                break;
                        }
                    }
                }
            }
            catch { }
        }
    }

  The code use the System.Reflection and then it read the Object property [see this.GetType().GetProperties() in foreach], iterate over properties and then checking if DataRow contain the same name column, if so, it load them. Basically the Default statement in Switch is enough for most cases, but I still leave it as is insight that in future I might need to cast some particular DB field to other field here. Oh, yes that one condition is boolean field. 

There are lot of upgradation coming in my mind as I write this blog entry, but currently no time to upgrade it and will add this function in my class library for future use. 

Categories
.NET C#.NET VB.NET/VB WPF

Self Hosted Web Service: WebServiceHost

So today I learn about WebServiceHost. Just a normal class but it does a lot for me today. This host the web service within you Desktop/Windows Service application. i.e. you don’t need to install any Web server or other stuff, just create a Service Contract Class and use WebServiceHost to host it within your application. 

I need this as part of my project, because I have to pass some server information to my application. I initially create a Seperete web application for this, but why maintain another application and increase setup time when I can host it within. I need it for just one particular value to be pass in JSON format to my client side. However one thing I must tell here is that the Host server application must be running as Administrative privilege or the service won’t start. For Windows Service run application as “LocalSystem” rather than default LocalService to keep it working.

It solve lot of problem and architectural issue I had in my project.  

Categories
.NET C#.NET VB.NET/VB WPF

WPF ComboBox: DataBinding with DataTable

Well it seems easy thing, but it took a while for me to get it working. Frankly I still don’t know why it works this way, but at least it works. With WPF I was expecting something as easy as giving DataTable object to ComboBox by it’s property and define the Data Field and Value field as in ASP.NET dropdownlist and it will bind, but this was not the case or at least the syntax is little different. So it goes like

ComboBox cb = new ComboBox(); /// You might already have object of it, so I just create it for sake of understanding here.
cb.ItemSource = MyDataTable.Rows; /// Yup, DataTable will not bind but it's rows collection will.
cb.DisplayMemberPath = ".[" + YourColumnNameVariable + "]"; ///in other other ".[columnname]" is what is expected;
cb.SelectedValuePath = ".[" + YourColumnNameVariable2 + "]"; ///in other other ".[columnname]" is what is expected;

This is all we need to do to get it working.

Categories
.NET ASP.NET C#.NET VB.NET/VB

ASP.NET: Why my cookie is not saving

Funny, today after years I thought to finally use Cookies in one of my project. It is probably my first attempt to use Cookie in ASP.NET in last 5 yrs. WOW, that is amazing. Anyways, I just notice one thing and thought it might help any first timer, so here is the key note I want to share. In ASP.NET if you want to read Cookie, use REQUEST.Cookies, but if you want to add new Cookie use RESPONSE.Cookies. This is very minor difference and I really waste 30 minutes before I release I was using Request.Cookies to add new cookie, but it won’t get added that way.