Thursday, 31 July 2014

Using Azure Powershell to attach to your azure subscription and create a new website


As I haven't posted for a while I wanted to just quickly post my findings on using Azure Powershell.

My aim here using only powershell is to:

  • Connect to my Azure subscription
  • Create a website in azure
  • Stop the website
  • Remove the website

Ok here goes.

Firstly you will need to install Azure Powershell so go here and work it out:

Then fire it up. It should look like this and you can find it in your windows start menu.

then we need to tell powershell about our azure subscription:

We just type Add-AzureAccount and you will be prompted to log into Azure.
Look here for info on this command:

After that you will see some confirmation that it fould your azure account:

You may need to change the execution policy for you to do things from your local Azure:

So here is my Azure website list before I add a new site:

So next I run New-AzureWebsite -Name MyPowerShellTest:

You can see it looks like it did something and returned info about my new site. If you look in Azure you can see the new site:


Ok so for fun, lets stop the site:

So it worked.

Now ill delete the website completely using:
Remove-AzureWebsite -Name MyPowerShellTest

And it's gone:

Well, you will have to trust me as I cant show you nothing!


Thursday, 14 November 2013

What I found in 4 minutes in Visual Studio 2013 in an MVC5 project

I have installed Visual Studio this morning and here are a few initial things I have found.

I used the following version:

Firstly, after installation it asked my a couple of questions including what colour scheme I would like to use for visual studio. As you can see I chose DARK.
I thought this was a nice little welcome surprise instead of me burning my eyes out for weeks until I realise I could change the colour scheme.

Secondly, and I'm not sure if this is a good thing, I am logged into MSDN through visual studio:

I suppose it feels more personalised but lets see what happens down the track!

Another thing I noticed straight away is a little hint above methods that shows you how many time that method is referenced:

When you click on this it shows you the referenced code:

Another thing I think I like it that the old aspnet membership provider has been replaced with claims identification. I suppose it's still a custom database supplied by Microsoft but, as I remember as I have used claims identity before

( it should be more loosely coupled and allow you to write cleaner code without being totally locked inside the membership provider. As far as I remember I was using dependency injection and the OLD membership provider kept on making me write ugly code.

They have also added some controller example tests by default. This is great as I remember when I started writing controller test I did not have any examples.
This should motivate developers to write more tests from the start which is good.

I have always hated the Visual Studio test GUI but now it seems it looks a little bit better and is now more like re-sharper:

I am not sure if this was here before but there seems to be a new notification window to give you updates on what is new. For example, an update to the nuget package manager.

Code coverage is also included which is something I would have needed to pay for before:

The final thing I found was when I was in a controller, for example, I could right click and select code map:

And then I can drag classes onto the canvas to see their relationships:

That's my 2 cents worth and that's all for now.


Friday, 20 September 2013

Building .Net 4.5 Projects with Jenkins

I wont write much here but just wanted to say thanks to Rik Leigh for posting this.

It helped me out. My build was spitting out 100s of warnings and now it is fixed.


Thursday, 12 September 2013

Stop resharper adding a dll reference from a randon project and make it update from Nuget


We have been having issues where a developer (ME) uses Resharper to add a reference. The issue is, it doesnt look at nuget and update your packages file it just adds the nearest dll it can find (from a neighbouring project).

This is not good.

Anyway, I just found and installed this plugin from Resharper:

Here is a good post on it:

You just download the zip file:
(make sure you check for the latest verion on the site above (1))

Then you just run the batch file that matches your version:

Then when you add a reference:

It updates from nuget:

This is awesome!


Tuesday, 2 July 2013

Continuous Integration with Jenkins and MSTest

I have just set up CI with Jenkins for a client. I will now demonstrate how I did this. I will create a demo MVC project that basically does nothing in the browser. It does however have some unit tests in it that test some very simple business logic using Moq.
Here is my sample MVC4 project and then tests:

The unit tests within my project. You can see that they just test some arbitrary business logic and mock out a repository but it doesn’t matter what they really do as this post is about making Jenkins run MSTests.
Now I want to get Jenkins to:
  • poll my SVN server to check for new commits.
  • Build the code
  • Run my 2 unit tests
  • Create a unit test report
  • Send the report to email recipients.
Ok so first we want set up Jenkins.
I needed to install the following first as MSTest was not available:
Visual Studio Agents – ISO:
There are some config options with this but I just used the defaults and it all worked just fine.
Following that I began setting up Jenkins.
Here is my Jenkins project name:

Now we want to set up polling (currently 15 minutes). If an SVN commit is detected Jenkins will begin running a build. 

Then we need to point to our solution file for the build to work:
We then need to point to our unit testing DLL so Jenkins can tell MSTest where our tests are:
Then we need to tell a 3rd part tool (you don’t have to use this tool) to convert our trx file to Html format so it is easier to read:
After that, we want to ensure we actually publish our trx file:
And then set up email notifications to tell us when certain thing occur like the build failing:
We can then run the build:
After the build output we can now see the MSTest output:
If we now look in the file system we can see the original trx file and the newly generate Html version:
When we look at the Html we can see a nice summary of our passing test:
And if we force the tests to fail we can see this also:

You can also look at the build trends in Jenkins and see what's been happening:

And drill down on these:

That’s all there is to it. I have been meaning to set this up for ages and finally got the opportunity to do it.
Ps. Waiting on another Jenkins Plugin to be installed so I can email the Html version of the trx report to email recipients.

Tuesday, 18 June 2013

Setting up Sql Database mail for a client

I had a requirement to periodically send a data extract from an SQL database. I had a few ideas about how I could accomplish this but decided to take a look at SQL server to see what it had to offer as I hadn't used it to send anything for the last few years.

Anyway, I came across SQL database mail:

Here is what Microsoft says about it:
Database Mail is an enterprise solution for sending e-mail messages from the SQL Server Database Engine. Using Database Mail, your database applications can send e-mail messages to users. The messages can contain query results, and can also include files from any resource on your network. Database Mail is designed for reliability, scalability, security, and supportability.

So back to the task I needed to complete. I needed to periodically schedule a SQL Server Agent job to query my database and send the results to an email recipient.

Here's what I did.

I needed to send the following data:
SELECT P.Id, FirstName, LastName, 
        Email, AddressLine1, Suburb, PostCode, ST.Name, FutureCommunication, [Uid] 
        INNER JOIN 
        STATES ST
        ON P.State = ST.Id;

EXEC msdb.dbo.sp_send_dbmail

You can find the documentation on this here:

I came up with the follow script:
EXEC msdb.dbo.sp_send_dbmail
    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'DatabaseMailProfile',
        @recipients = ';',
        @query = @parentEmailQuery,
        @subject = @parentEmailSubject,
        @execute_query_database = 'DATABASE_NAME',
        @query_result_header = 1,
        @query_attachment_filename = @parentCsvFileName,
        @query_result_separator = '|',
        @query_result_no_padding = 1,
        @attach_query_result_as_file = 1 ;

This is basically a list of options that database mail needs.
Some useful ones I will point out are:

  • @profile_name = 'DatabaseMailProfile' - I will show you what this relates to shortly.
  • @query = @parentEmailQuery - This the query I showed you above but I included: SET NOCOUNT ON; before my query and SET NOCOUNT OFF; after my query. This was because the following: "n rows were returned" kept on appearing in my final out put and I did not want this.
  • @query_result_header = 1 - this shows or hides the column names. When it is set to 1 it also adds in an ugly row of dashes after column headings. This was ok for me but you may want to investigate writing a custom query to write out the header instead.
For this script to work we now need to talk about setting up SQL database mail.

Firstly we need to set up an account to use to send emails:

 the database mail node can be found in the SQL management studio tree:

 Right click on that and choose:

 You need to first set up an account so choose from the following screen:

and then this screen:

and enter the details of for your account:

After you have created an account you need setup a profile. As you will notice in the above mentioned query, there is a reference to the profile name:
EXEC msdb.dbo.sp_send_dbmail
   @profile_name = 'DatabaseMailProfile',

So here we go. Choose the option to add a profile and then add the account you created to it:

Choose what security options you want:

In the following screen, leave as is or change if you need to:

You will also need to run the following:

After that you can run a test if you like:

now you can run the script above:
EXEC msdb.dbo.sp_send_dbmail
    EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'DatabaseMailProfile',
        @recipients = ';',
        @query = @parentEmailQuery,
        @subject = @parentEmailSubject,
        @execute_query_database = 'DATABASE_NAME',
        @query_result_header = 1,
        @query_attachment_filename = @parentCsvFileName,
        @query_result_separator = '|',
        @query_result_no_padding = 1,
        @attach_query_result_as_file = 1 ;

Obviously I have removed my real world values.

This will do the following:
  • Look for the profile we created
  • Run the query to get the data we want to send
  • Set up some email params
  • Send our data as a csv file attachment with headers. The data will be separated with a pipe (|). You could use comma separation if you like.
Here is the email that was sent:

Following on from this you could set up a SQL Server Agent scheduled job to run periodically to
send your data.