# Monday, 09 July 2018
Monday, 09 July 2018 09:38:00 (GMT Daylight Time, UTC+01:00)
# Thursday, 05 July 2018

GCast 5:

Azure Databricks

Learn how to create a free library and Jupyter notebooks hosted in Azure.

Thursday, 05 July 2018 09:43:00 (GMT Daylight Time, UTC+01:00)
# Monday, 02 July 2018
Monday, 02 July 2018 09:35:00 (GMT Daylight Time, UTC+01:00)
# Sunday, 01 July 2018

7/1
Today I am grateful for:
-My first pair of prescription eyeglasses
-A visit yesterday to the Chicago Women's Park and Gardens

6/30
Today I am grateful for my first visit to IKEA.

6/29
Today I am grateful to David for taking time yesterday to answer all my questions.

6/28
Today I am grateful that I can binge-watch TV shows on demand.

6/27
Today I am grateful to hear Laretha Weathersby and the JW Williams Blues Band at Blue Chicago last night on my first visit to the venue.

6/26
Today I am grateful for a conversation with a financial adviser yesterday.

6/25
Today I am grateful to attend the Chicago Food Truck Festival yesterday.

6/24
Today I am grateful to the organizers of #BeerCityCode for an excellent conference.

6/23
Today I am grateful for:
-Drinks with J. Tower at Founder's last night
-The hospitality of Chris and Tracy last night

6/22
Today I am grateful for a nice Italian dinner with friends last night to celebrate the end of the school year.

6/21
Today I am grateful
-to go to an optometrist yesterday after years of neglect
-to go to the Art Institute of Chicago for the first time this year.

6/20
Today I am grateful to celebrate Father's Day last night with my son Tim.

6/19
Today I am grateful to spend a night in my own bed.

6/18
Today I am grateful for a week in Norway - my first visit to this country.

6/17
Today, I am grateful for:
-a walk around Bergen, Norway
-a visit to 2 art museums
-a chance to try some exotic Norwegian foods

6/16
Today I am grateful for a train ride across Norway and a boat ride through the fjords.

6/15
Today I am grateful for my first time speaking at and attending NDC-Oslo.

6/14
Today I am grateful for the #ndcoslo after-party.

6/13
Today I am grateful for a boat ride around Oslo.

6/12
Today I am grateful to work on a Machine Learning project this week with a train company in Oslo.

6/11
Today I am grateful for BBQ in Norway with Jimmy, Adam, and Dominick.

6/10
Today I am grateful for a few days in Romania

6/9
Today I am grateful to play Dungeons & Dragons in Transylvania.

6/8
Today I am grateful for another excellent #itcamp

6/7
Today I am grateful to Dan for picking me up at the CLJ airport and driving me to my hotel late last night.

6/6
Today I am grateful for free wine on European airlines.

6/5
Today I am grateful to finish a bunch of blog posts and screencasts yesterday.

6/4
Today I am grateful for dinner with Tim last night and to Adam and his daughter for stopping by.

Sunday, 01 July 2018 15:10:52 (GMT Daylight Time, UTC+01:00)
# Saturday, 30 June 2018

DarknessAtSethanonA Darkness at Sethanon concludes Raymond Feist's Riftwar Saga - a trilogy that became a tetralogy when Feist decided to rewrite volume 1, splitting it into 2 books.

An invasion by the dark sorcerer Murmandamus threatens the world of Midkemia and ultimately all other worlds in the Riftwar universe. Murmandamus is determined to capture the city of Sethanon and the powerful lifestone buried beneath it. Our heroes from the previous novels (Pug, Tomas, Arutha, and others) gather the knowledge, weapons, and allies they need to try to defeat Murmandamus.

There are twists. Characters we thought dead are found alive, former foes become allies, and Murmandamus's motivation is not what it seems.

This book is larger in scope than its predecessor as the characters travel across dimensions and even travel backwards in time to the beginning of their universe. The time travel sequence gives the characters and the reader insights into the history of the universe and its many sentient and magical races.

Feist is better at action than at character development. Not that the characters are bad - it's just that he jumps them ahead abruptly, rather than letting us watch them mature and otherwise change. And this book has plenty of action, including several large battle scenes.

I'm glad I stuck with this series, as it got better as it went along.

Saturday, 30 June 2018 16:55:28 (GMT Daylight Time, UTC+01:00)
# Friday, 29 June 2018

The .NET framework provides the FtpWebRequest class to assist you in managing FTP commands.

A common use of this class is to upload a file to an FTP site.

To do this in C#, we create an FtpWebRequest object (the WebRequest object has a static factory method to do this) on the object, set our intended method, and specify the FTP site's login credentials, as shown below.

string ftpUrl = @"ftp://blah.ftp.blah.com";
ftpUrl = ConfigurationManager.AppSettings["ftpUri"];
string userName = ConfigurationManager.AppSettings["userName"];
string password = ConfigurationManager.AppSettings["password"];

string ftpDestinationFolder = "site/wwwroot/content/Giard";
string localFolderSource = @"C:\Test\_source";
string fileName = "testfile.txt";

string sourcePath = @"C:\Test\_source\testfile.txt";
string destinationPath = @"ftp://blah.ftp.blah.com/folder1/testfile.txt";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destinationPath);
request.Method = WebRequestMethods.Ftp.UploadFile;
  

A common mistake is to set only the site and folder name (and skip the filename) in the destinationPath.

To send a file via FTP, we need to read the file from disk and convert it into a Stream.

byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(sourcePath))
{
	fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
  

We then write this StreamReader to the FTP request object

request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
	requestStream.Write(fileContents, 0, fileContents.Length);
}
  

Finally, we retrieve the FTP response.

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
	var responseText = response.StatusDescription;
	Console.WriteLine("Completed uploading file {0}!", fileName);
	Console.WriteLine("Status={0}", responseText);
}
  

The full code of my console app is here:

using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;

namespace DemoFtp
{
    class Program
    {
        static void Main(string[] args)
        {
            string ftpUrl = @"ftp://blah.ftp.blah.com";
            // Note: I stored some values in app.config
            // I had to set a reference to System.Configuration to read these values
            ftpUrl = ConfigurationManager.AppSettings["ftpUri"];
            string userName = ConfigurationManager.AppSettings["userName"];
            string password = ConfigurationManager.AppSettings["password"];

            string ftpDestinationFolder = "site/wwwroot/content/Giard";
            string localFolderSource = @"C:\Test\_source";
            string fileName = "testfile.txt";

            string sourcePath = Path.Combine(localFolderSource, fileName);
            string destinationPath = Path.Combine(ftpUrl, ftpDestinationFolder, fileName);

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destinationPath);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.Credentials = new NetworkCredential(userName, password);

            byte[] fileContents;
            using (StreamReader sourceStream = new StreamReader(sourcePath))
            {
                fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            }

            request.ContentLength = fileContents.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileContents, 0, fileContents.Length);
            }

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                var responseText = response.StatusDescription;
                Console.WriteLine("Completed uploading file {0}!", fileName);
                Console.WriteLine("Status={0}", responseText);
            }

            Console.ReadLine();
        }
    }
}
  

You can find this console app in my github repository at https://github.com/DavidGiard/Demo-FTP-CSharp

.Net | C# | Web
Friday, 29 June 2018 07:38:00 (GMT Daylight Time, UTC+01:00)
# Thursday, 28 June 2018

GCast 4:

Azure Notebooks

Learn how to create a free library and Jupyter notebooks hosted in Azure.

Thursday, 28 June 2018 07:45:00 (GMT Daylight Time, UTC+01:00)
# Wednesday, 27 June 2018

A T4 ("Text Template Transformation Toolkit") template is a text generation tool that mixes raw text and code to generate text as output.

In my last article, I showed how to create a Runtime Text Template and generate output from it via the following lines of C# code:

var tt = new myT4Template();
string outputText = tt.TransformText();
  

But sometimes, it is helpful to pass parameters from your C# code and use them in your template.

The steps to do this are:

Add a T4 Runtime Text Template to your Visual Studio project  In the T4 template:

    1. Add one or more parameter directives  
    2. Use the parameters in your template

In your C# code

  1. Instantiate your T4 template object
  2. Set the Session property to a new Dictionary
  3. Pass the parameters in the Session object
  4. Initialize the T4 template object
  5. Generate the output text with the T4 template object's TransformText() method.

An example will help illustrate this.

I have a T4 Runtime Text Template named "ParamTemplate1.tt" and I want to add 2 parameters: firstName and lastName (both strings) and use them in my template.

At the top of the template file, add the following directive:

<#@ parameter name="firstName" type="System.String" #>
<#@ parameter name="lastName" type="System.String" #>
  

Notice that we included the Namespace in the parameters' data types.

Now, use the parameters in your template, as in the following code:

Hello <#=firstName #> <#=lastName #>!
  

In your C# program, add the following code

var pt1 = new ParamTemplate1();
pt1.Session = new Dictionary<string, object>();
pt1.Session["firstName"] = "David";
pt1.Session["lastName"] = "Giard";
pt1.Initialize();
var outputText1 = pt1.TransformText();
  

This passes the values "David" and "Giard" to the firstName and lastName parameters, respectively.

We can even pass in a custom data type, based on a type we create.

Imagine I have created a Person class with a FirstName and a LastName properties (both strings) and I want to pass a parameter of this type into my T4 template named "ParamTemplate2.tt" and use that parameter inside the template.

Here is an example of my custom Person class:

namespace DemoT4Parameters
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}
  

In my T4 template, I again add a parameter directive, as shown below:

<#@ parameter name="person" type="DemoT4Parameters.Person" #>
  

Notice the custom namespace in the Type.

Now, I can use the parameter and its properties in my template, as shown below:

Hello <#=person.FirstName #> <#=person.LastName #>!
  

My C# code is very similar to the first example.

var pt2 = new ParamTemplate2();
pt2.Session = new Dictionary<string, object>();
var person = new Person()
{
	FirstName = "Satya",
	LastName = "Nadella"
};
pt2.Session["person"] = person;
pt2.Initialize();
var outputText2 = pt2.TransformText();
  

You can find this code in my GitHub repository at https://github.com/DavidGiard/Demo-T4-Parameters.

In this article, I showed how to pass parameters into a T4 Runtime Text Template.

Wednesday, 27 June 2018 08:44:00 (GMT Daylight Time, UTC+01:00)
# Tuesday, 26 June 2018

A Text Template Transformation Toolkit (or "T4") template is a text generation tool that mixes raw text and code to generate text as output.

Visual Studio contains tools to assist with the creation and execution of T4 templates.

To add a T4 template to your project, right-click on the project, and select Add | New Item from the context menu.

In the "Add New Item" dialog, the T4 templates are listed under the General category; but you can find them quickly by typing "T4" in the "Search Installed Templates" textbox.

There are two kinds of T4 templates: a Text Template and a Runtime Text Template. Each of these starts with a file with ".tt" extension, but is designed to generate at least one other file.

The Text Template regenerates the final output file every time you save the ".tt" file. This is a quick way to generate simple files, but it lacks flexibility.

The Runtime Text Template generates a VB or C# class that you can call from your code to generate the output file. We will focus on the Runtime Text Template in this article.

A Runtime Text Template consists of raw text (which will be exactly duplicated in the output file) and executable code, which the template will run. Any output of that code will be included in the output file. Code is delimited from the raw text by placing it between the <# and #> symbols. If you just want to output the results of an expression, you can surround that expression with the <#= and #> symbols.

For example, to output the current date and time, include the following line in your template.

<#=System.DateTime.Now #>
  

You can combine this with text as in the following example:

(This file was generated at <#=System.DateTime.Now #>!)
  

which would output something like the following:

(This file was generated at 06/25/2018 20:33:14!)

You can include more complex code between the <# and #> symbols, as in the following example:

<#
for (System.Int32 I = 0; I < 5; I++)
{
	// WriteLine is a utility function that outputs text. 
	WriteLine(I);
}
#>
  

The result of the above code is:

0
1
2
3
4

When you save a Runtime Text Template, Visual Studio generates code for a public .NET class with the same name as your tt file. You can instantiate this class as you would any other public class and call the TransformText() method to generate the output as a string.

For example, if I created a Runtime Text Template named "myT4Template.tt", the following code would generate the output and save it to a variable named "outputText".

var tt = new myT4Template();
string outputText = tt.TransformText();
  

T4 templates give you the flexibility to generate text and text files quickly and the flexibility to change the template without worrying about the associated data.

Tuesday, 26 June 2018 08:50:00 (GMT Daylight Time, UTC+01:00)
# Monday, 25 June 2018
Monday, 25 June 2018 09:10:00 (GMT Daylight Time, UTC+01:00)