December 2014

You are browsing the site archives for December 2014.

I finally started using GitHub for some of my projects. And as usual, I try what is possible 😉
This time, I played around with Continuous Integration. The way to go seems to be with Travis CI. It has (beta) support for .Net with Mono. And it seems to work quite well as long as your projects are Mono compliant.

Basic Travis usage in github
To use Travis in github is pretty straight forward. Just register for Travis, link to your github and add a .travis.yml file to your github project.
The basic file can look like:

language: csharp
solution: src/<yoursolution>.sln

This just compiles all the projects which are in your solution.

Skip / fix unsupported code when compiling with Mono
If you’re project is almost Mono compliant, you can think of using the precompiler directive with the variable __MonoCS__ to get it to build on mono as well but use the “real” thing in your Visual Studio.
Here’s an example which uses the CommandManager of WPF which does not exist in Mono:

#if __MonoCS__
        public event EventHandler CanExecuteChanged;
#else
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
#endif

This makes at least sure, that it compiles on Mono.

Using nUnit tests with Travis
To run nUnit tests with travis is also fairly easy. Just add your unittest project and modify the Travis file like this:

language: csharp
solution: ./src/yoursolution.sln

install:
  - sudo apt-get install nunit-console
  - nuget restore ./src/yoursolution.sln

script:
  - xbuild ./src/yoursolution.sln
  - nunit-console ./src/SomeLibrary.Tests/bin/Debug/SomeLibrary.Tests.dll

This installs the nunit-console package, makes sure that the nuget libraries are fetched, compiles your solution with xbuild and runs the test library with nunit-console.

That’s it. Now Travis compiles and tests your github project and fails in case that it can’t compile or at least one unit test fails.

Sometimes you want to output multiple lines in a kind of table form to the console or a file. The main problem here is, that you need to know for each column the maximum number of characters any element has so that you can pad them correctly.

So take this example:

Title Name Street
Mr. Roman Sesamstreet
Mrs. Claudia Abbey Road

wouldn’t it be nicer to look like:

Title   Name       Street
Mr.     Roman      Sesamstreet
Mrs.    Claudia    Abbey Road

To make it easier, I created a small function which helps in doing so:

public static class ConsoleUtility
{
	/// <summary>
	/// Converts a List of string arrays to a string where each element in each line is correctly padded.
	/// Make sure that each array contains the same amount of elements!
	/// - Example without:
	/// Title Name Street
	/// Mr. Roman Sesamstreet
	/// Mrs. Claudia Abbey Road
	/// - Example with:
	/// Title   Name      Street
	/// Mr.     Roman     Sesamstreet
	/// Mrs.    Claudia   Abbey Road
	/// <param name="lines">List lines, where each line is an array of elements for that line.</param>
	/// <param name="padding">Additional padding between each element (default = 1)</param>
	/// </summary>
	public static string PadElementsInLines(List<string[]> lines, int padding = 1)
	{
		// Calculate maximum numbers for each element accross all lines
		var numElements = lines[0].Length;
		var maxValues = new int[numElements];
		for (int i = 0; i < numElements; i++)
		{
			maxValues[i] = lines.Max(x => x[i].Length) + padding;
		}

		var sb = new StringBuilder();
		// Build the output
		bool isFirst = true;
		foreach (var line in lines)
		{
			if (!isFirst)
			{
				sb.AppendLine();
			}
			isFirst = false;

			for (int i = 0; i < line.Length; i++)
			{
				var value = line[i];
				// Append the value with padding of the maximum length of any value for this element
				sb.Append(value.PadRight(maxValues[i]));
			}
		}
		return sb.ToString();
	}
}

A sample on how to use it would be:

var lines = new List<string[]>();
lines.Add(new[] { "What", "Before", "After"});
lines.Add(new[] { "Name:", name1, name2});
lines.Add(new[] { "City:", city1, city2});
lines.Add(new[] { "Zip:", zip1, zip2});
lines.Add(new[] { "Street:", street1, street2});
var output = ConsoleUtility.PadElementsInLines(lines, 3);

Monitoring traffic from a mobile device can help in many debugging scenarios. Fiddler is a great tool to monitor and debug http(s) requests. This post will describe all necessary steps to use Fiddler to monitor any traffic from a mobile device (also for apps).

Install and configure Fiddler

  1. Install the latest version of Fiddler.
  2. Install the CertMaker for iOS and Android (overrides the default cert-maker from Fiddler to get compatibility with Android/iOS)
  3. Open Fiddler Options (Tools -> Fiddler Options)
  4. Switch to Connections tab
  5. Check the Fiddler listens on port (should be 8888, but you can change it if needed)
  6. Make sure that “Allow remote computers to connect” is checked
  7. Switch to HTTPS tab
  8. Make sure that “Capture HTTPS CONNECTs” is checked
  9. Make sure that “Decrypt HTTPS traffic” is checked

    Note: If you previously already had a certificate made with the default or another generator than the CertMaker, you might need to regenerate the certificate (by deactivating “Decrypt HTTPS traffic”, then clicking “Remove Interception Certificates” and then reactivating “Decrypt HTTPS traffic” again).

  10. Make sure “Ignore server certificate errors” is checked
  11. Restart Fiddler

Setup proxy on Android

  1. Open Settings -> Wi-Fi
  2. Edit the network you’re currently using
  3. Choose “Show advanced options”
  4. Change “Proxy settings” to “Manual”
  5. Enter the IP of the Windows PC with Fiddler under “Proxy host name”
  6. Enter the Fiddler port (default 8888) under “Proxy port”
  7. Save the settings

Setup proxy on iOS

  1. Open Settings
  2. Edit your current network
  3. Go to “Http-Proxy” and set it to “Manual”
  4. Enter the IP of the Windows PC with Fiddler under “Server”
  5. Enter the Fiddler port (default 8888) under “Port”
  6. Make sure “Authenticate” is not checked

Install the Fiddler certificate on the device (needed for HTTPS)

  1. Open any browser on the mobile device
  2. Browse to http://<your windows ip>:<fiddlerport> (eg: http://192.168.1.30:8888)

    Note: You might get a “Connection Refused” here. If this is the case, you can try to disable the “Enable IPv6 (if available)” checkbox in the “General” tab.

  3. Click on “FiddlerRoot certificate”
  4. Install the certificate (might need your device pin or password)

Hint: Chrome traffic not captured

If the traffic from the chrome browser is then not visible in Fiddler this might be because of the “Reduce data usage” setting of chrome. This means that the traffic is redirected thru a google proxy and therefore does not work with your Fiddler proxy. To fix this, just disable the “Reduce data usage” and “Preload webpages” option in Chrome (Advanced -> Bandwidth management)

An application folder can look quite crowded if you have some assemblies or 3rd party references in use.

Like in this image: proj_folder_before Wouldn’t it be much nicer to have it look like:proj_folder_after

When it comes to having dll-files somewhere else than in the “app-folder, things can get quite cumbersome.

Here’s how to achieve this:

Moving files into subfolder

There are multiple ways to achieve this.

One way is to add a solution folder with the desired name of the subfolder and add the assemblies / dependencies as files (best way is to add them as links) there with build action=none and copylocal=true. If you add assemblies there used as references as well, set the copylocal for the references to false.

Another way is to leave the dll’s where they are (just adding dependent dlls as files/links into the project’s root directory) and create a post-build event or (my preferred way) add an AfterBuild task to the project to move all *.dll files into the subfolder.

For the post-build event just edit the project’s properties and add the commands into the post-build in the Build Events.

For the preferred way, just edit the csproj file (unload the project, then right-click and edit, open in any text-editor or use the visual studio addon EditProj) and add the following code at the bottom:

<Target Name="AfterBuild">
	<ItemGroup>
		<FilesToMove Include="$(OutDir)*.dll" />
	</ItemGroup>
	<Move SourceFiles="@(FilesToMove)" DestinationFolder="$(OutDir)Libs" />
</Target>

This example code uses the folder “Libs” for the dlls. Feel free to adjust it to your needs.

Telling the application to search other directories for libraries

This is actually quite simple. The current way of doing this is to edit the app.config and adding the following code into the configuration:

<runtime>
	<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
		<probing privatePath="Libs" />
	</assemblyBinding>
</runtime>

That’s it. With those two things you can have a “clean” application folder where all the assemblies or 3rd-party dependencies (like sqlite or sqlce dlls) are nicely stored in a subfolder

I just found a nice summary to help you find out what could be an optimal Windows Lumia phone for you, since the overwhelming amount of numbers could be confusing:

Lumia naming conventions
5x Cheap phones
6x, 7x Midrange phones
8x Cheaper flagship phones
9x Flagship phones
10x 41mp phones
13x Cheaper phablets
15x Flagship phablets
25x Tablets