Taking Visual Studio Code and ASP.Net 5 For A Spin

Visual Studio Code Setup

Visual Studio Code Download

https://code.visualstudio.com/Download

Visual Studio Code (VSCode) is the latest free cross platform lightweight IDE from Microsoft. Visual Studio Code and ASP.Net 5 are being released as free and open source.

Initial Impressions for Visual Studio Code

Pros:

  • Lightweight IDE
  • Support for many Languages with IntelliSense
  • Git Integration
  • Open Folder
  • Easy Split Window Navigation
  • User and Workspace settings in JSON

Cons:

  • Does not support regions for C#
  • Keeps hanging when you use on large .Net solutions (Open Folder)

ASP.Net 5 Setup

According to Microsoft, DNX was built entirely from the ground up. The ASP.Net 5 was also created from the ground up, meaning all ASP.Net 5 projects are DNX projects. Good thing to note is that ASP.Net 5 is not anymore based from System.Web.dll. Things are built around the concept of packages and on demand references.

Windows

In order to install and run ASP.Net 5 from Windows, you will need to install the DNVM (Version Manager), DNU (Utility) and DNX (Execution Engine). The steps to install them can be found from this link.

OSX

A very cool new feature of ASP.Net 5 is that it can run and be developed in other platforms. I was very excited to try and develop ASP.Net on a non-Windows machine. It gave me enough reason to buy and upgrade my MacBook Pro 2008 to MacBook Pro 2015 ;).

Just like in Windows, you will need to install the DNX, DNVM and DNU. But this time, since its on OSX, everything will be dependent on Mono (.Net Open Source Project). You can get Mono by installing through Homebrew (package manager) in OSX. Actually, it works backwards, in Homebrew when you install the latest ASP.Net and DNX it installs Mono automatically since its a required package of ASP.Net.

Linux soon!

I do not have now a Linux machine to try and install ASP.Net 5 so next time I will post my ASP.Net 5 adventures with Linux!

Sample Project ASP.NET 5

Omnisharp log – Error logged on opening the sample project

When opening the sample ASP.Net 5 DNX project with VS Code without first installing DNX, you will encounter an error logged under the Omnisharp log. This is a good way to know the sequence where it looks for the DNX framework.

Omnisharp log

The DNX framework can be installed on your system like what I did when I followed the steps to initially setup DNVM, DNU, DNX amongst others. But you can also, make available the different DNX versions in your local by getting and compiling them from the Github repository. This way, you can test different versions of DNX from your system. Running DNVM list will show you different versions installed in your machine.

Two versions of DNX

Running the sample apps

There are sample projects up in the ASP.NET repository. You can download or clone them from Github.

I tried to open and run the web application under the HelloMVC sample folder. This is a sample MVC ASP.NET 5 project which you can open in VSCODE.

Inside the project folder, you can execute

dnu restore

to make download the dependencies of the sample project. Visual Studio Code will also prompt you to do the restore.

DNU Restore VSCode Window

DNU Restore Complete

To execute and start the hosting of the project run the command

 dnx . web 

DNX WEB

Open a browser and go to http://localhost:5001/ and you will see the sample MVC ASP.NET 5 project running.

Sample ASP NET 5 Website running

You can do the same steps to setup, compile and run the web project in OSX. As long as you have DNX installed you can start and run ASP.NET 5 projects. I’m trying to follow the latest in DNX and ASP.NET as a whole in Github – http://www.github.com/aspnet and try to contribute if possible 🙂 Thanks and until my next post!

Suppressed Exception On The Empty Catch Block

When you investigate an issue or a problem you usually get the error or exception message first and then try to replicate that exception in a lower environment such as your local machine. What if there was an error but the application did not produced a message or even a trace where the error occurred? Big problem. One such case to avoid this problem and to avoid passing this problem to the next person that will support your application, is to avoid having suppressed exception on the empty catch block on your code. Yes, having an empty catch block will not be detected by the C# compiler, bummer.

The culprit

public class SilentExceptionSample
{

public void DivideOperation(int a, int b)
{
try
{
var result = a / b;
}
catch (Exception)
{
//throw;
}

}

}

The code above is a sample method that does not return anything (void) and an operation to divide a and b. A try catch block will be able to catch any exception that may happen but throwing the exception that may happen is a different scenario. Inside the catch block, the C# compiler allows the block to be empty and not return anything. Most of the time this block can contain a logic to handle the exception such as logging and external error handlers. What happens if the developer leaves the block empty without the intention of leaving it empty? Errors in your application will not have any trace.

Some unit testing

[TestFixture]
public class SuppressedExceptions
{

[Test]
public void Division_With_Exception_Test() {

var silentException = new SilentExceptionSample();

Assert.Throws(() => silentException.DivideOperation(1,0));
}

[Test]
public void Division_With_NoException_Test()
{
var silentException = new SilentExceptionSample();

Assert.DoesNotThrow(() => silentException.DivideOperation(1,1));
}

}

I created a unit test to test the exception, on this sample above its the DivideByZeroException, that the method will throw when you divide a number by zero. The test fails in the first scenario since the error suppressed exception on the empty catch block.

Test Results

Ways to avoid

There can be ways to avoid this code smell. I suggest the following…

Unit Test

– Perform unit tests on exception handling on your code.

Code Snippet

– Use the code snippets on Visual Studio. If you want to use a try catch block, typing in “try” then tab will auto complete the whole block.

Resharper

– Tools such as Resharper can help in detecting code smells. See the warning below from the Resharper engine on the empty catch block.
Resharper Warning

This type of code smell can be the source of problem you are investigating now or something you can prevent while in development. You’ve been warned! 🙂

How To Transfer App.Config ApplicationSettings to Web.config

Add Web Reference from a class library for .Net 2.0

If you still use .Net framework 2.0, the way to reference a web service to your project is to add a Web Reference.
What if you want to add the web reference from a class library project? This will result to Visual Studio automatically
adding an app.config to your project. The app.config will contain the configuration under applicationSettings of the newly added reference of the web service.

Add Web Reference - ApplicationSettings

Add Web Reference

The app.config will look like this for example.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Noel.Sample.ServiceInterface.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Noel.Sample.ServiceInterface.Properties.Settings>
<setting name="Noel_Sample_ServiceInterface_ServiceHello_Service"
serializeAs="String">
<value>http://localhost:59962/Service.asmx</value>
</setting>
</Noel.Sample.ServiceInterface.Properties.Settings>
</applicationSettings>
</configuration>

What if you want to use one config file?

Your main application is a web application and it has its own web.config file. What if you want to use and maintain one
config file? You don’t need to point the app.config section from your web.config file. You can move the section group and
the section of the web service from your app.config to your web.config.

Move app.config applicationSettings to Web.config

<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Noel.Sample.ServiceInterface.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<Noel.Sample.ServiceInterface.Properties.Settings>
<setting name="Noel_Sample_ServiceInterface_ServiceHello_Service"
serializeAs="String">
<value>http://localhost:59962/Service.asmx</value>
</setting>
</Noel.Sample.ServiceInterface.Properties.Settings>
</applicationSettings>
</configuration>

The section group can be added to the configSections. The applicationSettings can be added under the configuration section of the web.config.

Get value of service URL from Web.Config under applicationSettings

Under appSettings of a Web.config file usually you use for example ConfigurationManager.AppSettings[“WebServiceURL”] to get a value in the Web.config. But when the URL is under the applicationSettings in a Web.config this is how you can get the value of the URL.

public class ConfigHelper
{
public string GetUrl()
{
var section = (ClientSettingsSection)ConfigurationManager.
GetSection("applicationSettings/Noel.Sample.ServiceInterface.Properties.Settings");
var url = section.Settings.Get("Noel_Sample_ServiceInterface_ServiceHello_Service").Value.ValueXml.InnerText;
return url;
}

}

The last step is to remove the app.config from the class library. This is the case if the app.config was added to have the configuration of the web reference. In other cases where you need to the app.config, just remove the configSections and applicationSettings for the web service.

I spent some time looking for a way to make this work and not too much resource from web points to this approach. Hopefully it can help solving yours in less time. Thank you for this post from Stackoverflow on how solve this problem.

Comparing using CompareTo C# Method

 

A colleague asked me for a suggestion on the program module he is doing that involves determining if one value is “>” greater than, “=” equal or “<” less than another value. I first thought of a solution involving a stack but its too low level and involves much longer code. After some suggestions and trying to think of another way, he came across of a solution to use CompareTo C# method.

What is CompareTo C# Method?

First, MSDN defines IComparable as an interface that “Defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances.” It has a method called CompareTo which as defined by MSDN “Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.”

The CompareTo method is available on all objects as long as the objects has the same type. See the sample code below for comparing value types integer and a string type.

CsharpCompareTo

 

The Return Value of CompareTo

The CompareTo method has a return value of int. The integer value return will be one of the values less than 0, 0 or greater than zero as what is shown below.

CompareToReturnValues

 

Nullable Types Does Not Implement CompareTo

Take note that Nullable types like int? short for Nullable<int> does not implement the CompareTo method. It will not even compile since the method is not implemented.

CompareToNullable

This blog post will serve as a quick reference for me whenever there is a need for me to use sorting or comparison of data when programming. I hope it will serve the same for you.

 

 

Notepad++ – Source Code Editor and More

 

Using Notepad++ with Visual Studio

To compliment Visual Studio, I use Notepad++ as my other source code editor. Visual Studio is still my primary IDE but its good to have Notepad++ on the side for that quick search in files, syntax highlighting depending on the language or even when you want to compare texts or codes. There are other free code editors out there like Notepad2 or Sublime but my vote goes to Notepad++.

My Favorite Notepad++ Features

Syntax Highlighting

Notepad++ Syntax Highlighting

You get to choose from the programming languages like C#, C++, SQL, XML to Objective-C or Perl for syntax highlighting.

Find In Files

When I’m working on a big solution file in Visual Studio and I want to look for all references of a specific method, property or keyword I use Notepad++ Find In Files. Instead of waiting for Visual Studio to return to me a list of instances, I just search it with Notepad++. It will take time and it will make Notepad++ unresponsive during search but I can still continue with my coding in Visual Studio.

Notepad++ FindInFiles

 

Plugins

There are number of plugins that are available for install and download. The plugins manager is also a plugin by itself. Just like Package Manager in Visual Studio its the source where third party features can be found.

Notepad++ PluginsManager

 

Useful Plugins

TextFx

Notepad++ TextFxUnwrapText

If you find yourself working on a big chunk of text and its supposed to stay on one line, you can use the “Unwrap Text” option of TextFx. It will remove the newline and carriage return characters of the text to make it a one line.

StraightText

XML Tools

If you are working with XML messages that is sent as a one line of string you can use the “Pretty Print with Line Breaks” option of XML tools. This will arrange the XML nodes with proper indention to make it easier for you to inspect the XML string you have.

Notepad++ XMLToolsPrettyPrint

XMLFormattedText

 

I hope this little note for Notepad++ helps.

LocalDb – SQL Express for Developers

 

When you install Visual Studio 2012 or higher the Sql Server Express component is also installed on your machine. When you add a connection to a database within VS I normally use .\SQLEXPRESS as the server name.

VSSqlExpressConnectoin

The .\SQLEXPRESS server name is totally fine and it will work. But I noticed that, like when you create a new ASP.Net MVC project, the default connection to the database is pointing to a LocalDb. I do not remember installing any LocalDb server.

New Asp.Net MVC Project

LocalDB Config

Nothing in my local machine services is running as LocalDb.

SQL Express Service

What is LocalDb vs Sql Server Express?

Based on this blog from MSDN SQL Server team, SQL Server Express is still a free version of the SQL Server. However, they decided to release LocalDb as a SQL Express edition for developers that will continue to be small, low footprint and easily configurable with no admin access required.

VSLocalDbConnection

One advantage I can see from using LocalDb is that you can still attach a file database using AttachDbFileName even if the connection is living within the same instance of the sqlservr.exe.

AttachDbFileName feature is a plus for developers who can request for a sample data in a file and use it to attach to the LocalDb just like you are connected to a full version of SQL Server.

As a personal note to myself, I should blog a lot more so expect more blog posts to come from me 🙂

Visual Studio 2012 Custom Installation Option Missing

 

TopImageMemeInstaller

I have installed Visual Studio 2012 several times in the past, its just now that I want do custom installation. Thank you to the limited space of an SSD drive, I need to keep the Visual Studio 2012 installation to a bare minimum. Just like past versions of  Visual Studio I can always just install what I want, right? Not with Visual Studio 2012.

Visual Studio Installer Feature Options

In Visual Studio 2012 installation, no option will let you pick only C# as the programming language, uncheck SQL Express or just proceed with complete installation. Every time it is complete installation.

Well not options were removed, you can choose if you want to install Blend for Visual Studio, Lightswitch and other features included in the installer.

Visual Studio Installation Size

My installation of Visual Studio 2012 took 5.24GB of space with Microsoft Office Developer Tools and Microsoft Web Developer Tools features selected.

This post of the Visual Studio team from Microsoft mentioned that they included the option for customisation but its not present with the installer I used. The installer was downloaded directly from the MSDN download site so I guess they remove the option again.

I’m happy with the improvement of Visual Studio 2012’s performance and features but giving me an option to just install what I want would not hurt. Not at all.

 

Quick Tip – Multi Project Solution in VS11

I’m using Visual Studio 11 since it was released as Beta. What can I say, I really get the new design and layout (monochrome and gylphs) which not every developer appreciates. But beyond design and layout I really like the new search feature and the navigation features added in the Solutions Explorer section.

Going to the quick tip, if you have created a project in Visual Studio 11 Beta and would want to add another project under the same solution you would get this:

Solution explorer without add new project

Yes no option to add new project! Normally (like in VS 2010) you will use the solution explorer to add a new project but there is no option on VS11 Beta.

 

Solution explorer in VS2010 with Add Project option

Solution:

To add another project just go to File – New Project – Choose any Template. On the same window at the lower portion you can find the Solution option. You have the option to Create a new solution or Add to solution.

Solution option under file-new project

This is where you can add existing projects to your solution. Note that this option is also available in Visual Studio 2010.

Solution Explorer not showing the Solution file?

If the solution is not showing in the solution explorer check if the Always show solution is enabled in your Visual Studio

Visual Studio Options screen

Go to Tools – Options – Projects and Solutions – General – Tick the Always show solution option.

There it is, a quick tip on working around Visual Studio 11. Enjoy VS!

 

 

 

Quick Look on Visual Studio 11 Beta

Less is more cup RS

Quick Look on Visual Studio 11 Beta

Aside from releasing a consumer preview of Windows 8, Microsoft also released Visual Studio 11 Beta version recently. As a .Net developer, I use Visual Studio more often than any other IDE so I am interested to know what is new. I do not think VS2010 is broken by any means but any enhancements are welcome.

Visual Studio 2011 Beta Install

Visual Studio 11 Installer

Quick Info on How To Get Visual Studio 11 Beta:

Installer for Visual Studio 11 Beta below:

Visual Studio 11 Beta Installer – https://www.microsoft.com/visualstudio/11/en-us/downloads

System Requirements:

– Supported Operating Systems: Windows 8 Consumer Preview
– Supported Architectures: 32-bit (x86) or 64-bit (x64)
– Hardware Requirements:
– 1.6 GHz or faster processor
– 1 GB of RAM (1.5 GB if running on a virtual machine)
– 5.0 GB of available hard disk space
– 5400 RPM hard drive
– DirectX 9-capable video card running at 1024 x 768 or higher display resolution

Visual Studio 11 Impressions

I will mention my top 3 favorite enhancements on this beta version but feel free to check the Visual Studio blog post made to introduce Visual Studio 11 Beta

Monochrome

The most obvious change for this version is the color and layout of the toolbars. My first reaction is this is very similar on how Xcode theme is. Xcode is the IDE for developing Objective-C projects. There is very limited use of color on the toolbars and the icons. This is a major change from the icons in Visual Studio which are usually presented like the toolbars you can find on other Microsoft products like Microsoft Office (Photographic icons). I like the monochrome theme and totally get why the Visual Studio team would say that this will make the developer more focused on the code. I do not see any value on adding another section of options presented in colored icons if I can just use the quick search to find them using their name (more on quick search next).

Visual Studio 11 Beta  IDE

Visual Studio 11 Beta Monochrome IDE

Visual Studio 11 Beta  Xcode IDE

For comparison, here is the Xcode IDE

Quick Search

On my Mac, my favorite tool and mostly used feature is the Spotlight (Apple button + spacebar). You can type in the name of the application or file you want to go to and there it is with a few keyboard strokes. On Visual Studio 11 Beta, they also added the same feature on searching for your build files and for searching commands and configuration options. No more coursing through each menu option from File to Help just to look for toggle for a specific option. This will really help improve efficiency and help save time for developers.

Visual Studio 2011 Beta Menu Explorer
Visual Studio 11 Quick Search Menu (Ctrl + 😉

Visual Studio 2011 Beta Solution Explorer
Visual Studio Quick Search Solution (Ctrl + Q)

Image QuickView

Another really useful enhancement added is the image quick view on solution explorer. You might say this is a minor feature but its very useful when you are building your UI during coding or you need to debug and find what exact image you need to reference on your code. No need to double click, just hover and see the image. Neat!

Visual Studio 2011 Beta Solution Explorer Image Preview
Image Quickview right on Solution Explorer

There are other features in the Beta version of Visual Studio 11 and I’m sure there will be changes with the final release. The Visual Studio team is in the right path on adding these features. Happy coding!

by