Tuesday 25 November 2014

Hosting Multiple Domains Under One Hosting Plan (Web.config, IIS, Windows)


Great blog article detailing how to host and manage multiple domains under a single hosting plan.
http://weblogs.asp.net/owscott/iis-url-rewrite-hosting-multiple-domains-under-one-site

Monday 24 November 2014

iPhone/iPad - How to Sniff iOS Traffic Using Fiddler on Windows


Here is a great article on how to sniff all traffic on an iOS device! Great way to see what servers apps are calling and what data is being passed in the background
http://www.diaryofaninja.com/blog/2010/11/09/using-fiddler-to-sniff-mobile-device-application-traffic

Saturday 22 November 2014

How to Host a Primary Domain from a Subdirectory (Using .htaccess)


Like some of you, you may have multiple domain names pointing to different servers, but sometimes, you may wish to have a domain name point to a specific server path (Without URL Forwarding). This post offers a solution on how to accomplish this using a web server that supports .htaccess

Preliminary - What is .htaccess (Read to understand the remainder of this post)
http://en.wikipedia.org/wiki/.htaccess

Modifying the .htaccess
The following code will need to be added to the .htaccess file in the main folder of your web server. You will need to insert the following code block and make modifications as noted in the (#) comments. You will need to change the two instances of example.com to your domain, and the three instances of subdirectory to the folder where you want your site.

Code Snippet
  1. # .htaccess main domain to subdirectory redirect
  2. # Do not change this line.
  3. RewriteEngine on
  4. # Change example.com to be your main domain.
  5. RewriteCond %{HTTP_HOST} ^(www.)?example.com$
  6. # Change 'subdirectory' to be the directory you will use for your main domain.
  7. RewriteCond %{REQUEST_URI} !^/subdirectory/
  8. # Don't change the following two lines.
  9. RewriteCond %{REQUEST_FILENAME} !-f
  10. RewriteCond %{REQUEST_FILENAME} !-d
  11. # Change 'subdirectory' to be the directory you will use for your main domain.
  12. RewriteRule ^(.*)$ /subdirectory/$1
  13. # Change example.com to be your main domain again.
  14. # Change 'subdirectory' to be the directory you will use for your main domain
  15. # followed by / then the main file for your site, index.php, index.html, etc.
  16. RewriteCond %{HTTP_HOST} ^(www.)?example.com$
  17. RewriteRule ^(/)?$ subdirectory/index.html [L]
End of Code Snippet


Visitors to your Web site will not be able to tell that your main domain is using a subdirectory, they will still see the Web site address as http://www.example.com/page.html.

Please note that this will not work with some website software. You may also need to modify the $base_url, $live_site or other configuration settings to finish the process.

Sunday 16 November 2014

CD and DVD File Systems Explained!


ISO9660
The original one that's been around for years and has quite limited functionality.
For Example:
You're limited to 8.3 characters for a file name (i.e. SOMEDOCU.TXT)
You're limited to all uppercase file names.
You're limited to file less than 4GB in size.

Joliet
An extension of ISO9660 and allows for longer / mixed case file names. The file size limit still applies. For Joliet to be used, ISO9660 must also be present - this is reflected in the options available.

UDF
UDF is a file system in it's own right and does not depend on another one also being present.
UDF supports long / mixed case file names and does NOT have the 4GB file size limit of Joliet and ISO9660.
It is the 'better' option for those people using up-to-date operating system. I say 'up-to-date' because older ones like Windows 95 and 98 cannot read / understand the UDF file system and will probably report the disc as being corrupt if that's the only one on it.

Standalone DVD players are only supposed to (made to) understand UDF. Being able to read ISO9660 / Joliet is optional. As such, if you're building a DVD Video disc, you at least want to make sure the UDF filesystem is present in any image you're building / burning.
A typical DVD Video disc you buy from a shop will use 'ISO9660 + UDF', so to be totally correct, that's what you should use too. That way, a standalone player can read it (due to UDF) and PC's with old operating systems can read it (due to ISO9660). A new operating system can read both but will favour UDF because it's more advanced.

More Info Here

Saturday 15 November 2014

Visual Studio Designer Errors - Could not find type 'CONTROL NAME HERE'. Please make sure that the assembly that contains this type is referenced


Problem
When working with visual development projects in Visual Studio (Can occur in WinForms and WPF applications!), you may encounter a Designer/XAML error such as the following:

To prevent possible data loss before loading the designer, the following errors must be resolved:

Could not find type 'CONTROL NAME HERE'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built using settings for your current platform or Any CPU.

The variable 'CONTROL NAME HERE' is either undeclared or was never assigned.

When these errors occur, typically you may have an error within your application. Simply fix the error, rebuild your solution, and the designer error will disappear! However.... this may not always be the case!

Who What Where Why
In Win Forms applications, Visual Studio keeps a designer file for each Form class within your application. This details where components are laid out of the form, their events, text values and more! (Basically all of their properties). Sometimes, these designer files get a little confused and they cannot sync properly with what the form contains as it's all done automatically behind the scenes. This can happen a lot when using User Controls as the project needs to be built successfully in order for the form to consume the control! (These designer errors may typically reference a User Control a lot of the time).

Resolution
If there are no errors within your application and this error appears, DO NOT EDIT THE DESIGNER FILE! (Never do this for that matter!), Visual Studio should always handle this task. Simply, re-build the whole solution, then restart Visual Studio.

Friday 14 November 2014

Task.Factory.StartNew (.NET 4.0) and Task.Run (.NET 4.5)


Task.Factory.StartNew (.NET 4.0)
Task.Run (.NET 4.5)

When to use Task.Run and Task.Factory.StartNew
http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx

Using Git and Bit Bucket with Visual Studio 2012


Great article here!
http://ilblogdipego.blogspot.com/2013/03/how-to-use-git-with-visual-studio-2012.html

Thursday 13 November 2014

c# WPF - Detecting Key Presses - Example: Detecting CTRL+C in a TextBox


The following example allows you to override the keyboard shortcut for copy (Control + C) on the KeyDown event of a WPF TextBox control.

Code Snippet
  1. private void TEXTBOX_KeyDown(object sender, KeyEventArgs e)
  2. {
  3.     if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
  4.     {
  5.         MessageBox.Show("CTRL + C has been pressed!");
  6.     }
  7. }
End of Code Snippet

Monday 10 November 2014

Visual Studio Does Not Show Unhandled Exception Message on a 64-bit version of Windows (Silent Exceptions x64)


Silent exceptions on x64 development machines

Have you ever expected an exception to present itself in Visual Studio but the application seemed to play it cool and continue to run (With very unexpected results ensued!)? Then you may have experienced Silent Exception Syndrome! This seems to occur all in 64bit versions of Windows prior to Windows 8 (Vista, 7 etc...)

The Cause
When a user mode exception crosses a kernel transition, x64 versions of Windows do not allow the exception to propagate. Therefore attached debuggers are unaware of the fact that an exception occured resulting in the debugger failing to break on the unhandled exception.

Work Around
This is a known bug with x64 versions of Windows and the way exceptions are handled. One way to work around this issue while debugging is to go to the Debug -> Exceptions and select 'Thrown' for for the exception types you are interested in. This will stop the debugger when the exception is first hit (and before Windows eats it up).

Permanent Fix
Install this Hot Fix

More Information
http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/

Friday 7 November 2014

C# WinForms and WPF - Getting an Accurate Line Count for a Multline Textbox


You may have used the TextBox.LineCount Property for a standard Textbox control, but you'll quickly notice that if lines overflow onto the next line you'll have an extra line added to your line count. Really, you would like to count every physical line (Which ends in a physical return. I.e. \r\n)

Here is a code snippet that I use, it also ignores empty lines...

Code Snippet
  1. string[] lines = System.Text.RegularExpressions.Regex.Split(MULTILINETEXTBOX.Text.Trim(), "\r\n");
  2. int lineCount = lines.Length;
End of Code Snippet

Wednesday 5 November 2014

[Return Code 550] sid: RandomSIDHere :: High probability of spam (C# System.Net.Mail Fix)


I have written an emailer application in C# using the System.Net.Mail library. The application allows me to email multiple contacts in large quantities. This is useful for many reasons but I mainly use it for notifying subscribed users of software updates and issue licence keys.

What can be frustrating something sometimes is when you email is bounced back by the receiving party's email server stating that the message could potentially be spam. You will typically receive an email back with something along the lines of: "[Return Code 550] sid: RandomSIDHere :: High probability of spam".

Incoming mail servers typically check for a "Message-Id" header as part of the email. Email clients use this to track the thread of the message among other things. You can read more about Message-Id here.

We can attach a Message-Id attribute to our outgoing mail messages in C# with one very simple line of code (Where 'mail' is a MailMessage object)

Code Snippet
  1. mail.Headers.Add("Message-Id", string.Format("<{0}@{1}>", Guid.NewGuid().ToString(), "yourdomain.com"));
End of Code Snippet

Tuesday 4 November 2014

C# and VB.NET - Google Chrome WebBrowser Control for WinForms and WPF


Anybody that has used the inbuilt WebBrowser control in WinForms and WPF has noticed that it's an old version of Internet Explorer can throws a lot of script errors, renders pages badly and is prone to memory leaks.

This blog post will provide a little insight into using The Chromium Embedded Framework (CEF) as an alternative. Chromium in a nutshell is basically a development release of Google Chrome. When a chosen release has been put through testing and deployed, it is a simply a new version of Google Chrome.

Chromium Embedded Framework (CEF) is an open source project and actively maintained (As of 2014)... These are two good things we look for when re-using existing development frameworks. You can visit the CEF development page here.

External Projects
The base CEF framework includes support for the C and C++ programming languages. Thanks to the hard work of external maintainers CEF can integrate with a number of other programming languages and frameworks. These external projects are not maintained by CEF so please contact the respective project maintainer if you have any questions or issues.

.Net - https://github.com/chillitom/CefSharp
.Net (CEF1) - https://bitbucket.org/fddima/cefglue
.Net/Mono (CEF3) - https://bitbucket.org/xilium/xilium.cefglue
Delphi (CEF1) - http://code.google.com/p/delphichromiumembedded/
Delphi (CEF3) - http://code.google.com/p/dcef3/
Go - https://github.com/CzarekTomczak/cef2go
Java - http://code.google.com/p/javachromiumembedded/
Java - http://code.google.com/p/javacef/
Python - http://code.google.com/p/cefpython/

For C# and VB.NET applications, I recommend CefSharp. Download via NuGet and add to your project with ease!

CefSharp
This project contains .NET CLR bindings for The Chromium Embedded Framework (CEF) by Marshall A. Greenblatt. A small Core of the bindings are written in C++/CLI but the majority of code here is C#. It can of course be used from any CLR language, e.g. C# or VB.

CefSharp provides both WPF and WinForms web browser control implementations. See the CefSharp.Wpf.Example or CefSharp.WinForms.Example projects for example web browsers built using this library; they are (at this moment) the best "documentation" of features. In addition see the CefSharp.MinimalExample repo for how CefSharp can actually be used via NuGet packages.

Sunday 2 November 2014

Encode and Decode to Base64 in C#


Base64
Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME, and storing complex data in XML.

Here is a simple static class that allows you to Encode and Decode a value to and from Base64 encoding!

Code Snippet
  1. public class Base64Helper
  2. {
  3.     public static string Encode(string plainText)
  4.     {
  5.         var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  6.         return System.Convert.ToBase64String(plainTextBytes);
  7.     }
  8.  
  9.     public static string Decode(string base64EncodedData)
  10.     {
  11.         var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
  12.         return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
  13.     }
  14. }
End of Code Snippet