Chris Pietschmann

husband, father, hacker, entrepreneur, futurist, innovator, autodidact

NAVIGATION - SEARCH

Code Tip: Simpler Performance Timer Logging in C#

At this point application logging is pretty trivial as there are tons of logging libraries available for use. However, logging of performance timing is a bit ambiguous. There are many ways to do it, and one of the most common is to use the System.Diagnostics.Stopwatch class. Rather, than just embed the Stopwatch class code within and mixed with your application code; in this post I'll cover a more graceful approach to using a simple class along with the C# using keyword. If you're... [More]

5 Common C# tasks in Apple Swift Programming Language

There are many simple tasks performed by C# developers over and over. These are common things necessary in all kinds of applications. This post will give an overview of a few of the most common tasks performed by C# developers with examples on how to perform those tasks using the Swift programming language from Apple. Type Casting There are many reasons you might need to convert a primitive type to another. One of these is to convert an integer to a string for string concatenation or some other ... [More]

Basic Comparison of Functions in C# and Apple Swift Programming Language

Previously I wrote a basic comparison of the C# and Apple Swift programming language syntax. In this article we'll dive a little deeper into a comparison in to how defining and calling Functions work in both. Functions are first class citizens in both C# and Apple Swift, so lets see how they compare against each other! Define Basic Parameterless Function At the most basic level there really aren't many differences between the 2 languages as you can see below. Even the way you return a value from... [More]

Basic Comparison of C# and Apple Swift Programming Language Syntax

Recently, Apple announced and released a beta version of the new Swift programming language for building iOS and OSX applications. Swift is a modern language with the power of Objective-C without the "baggage of C." While we can't argue that Objective-C has it's difficulties being tied closely to C, but the real question is... How does Swift compare to a modern language like C#? Please, keep in mind that this post is not supposed to be an Apple vs Microsoft post. There are a lot of developers th... [More]

What “var” really means in C#–It’s not “variant”

With the release of C# 3.0 came the addition (among many others) of the “var” keyword. I’ve seen MANY people confused about what this keyword is, does and means. Among these people with incorrect assumptions to the function of the “var” keyword, I have seen programmers/developers with varying amounts / years of experience. So, I’ve decided to write up a simple explanation; actually the same one I’ve told and/or emailed people over the last couple years. “var” is not variant! Since “var” is the... [More]

Using MiniProfiler with SqlDataSource ASP.NET WebForms Control

I recently implemented MiniProfiler into an existing ASP.NET WebForms application that makes use of databinding to the SqlDataSource control. Since the SqlDataSource uses a DbProviderFactory internally, it is fairly simple to exend the control to utilize MiniProfiler through inheritance and overriding a single method of the SqlDataSource. Here’s a very simple class that inherits from the SqlDataSource control and injects MiniProfiler support to be able to profile the SQL query used by the contr... [More]

Mocking HttpRequestBase.ServerVariables using Moq

Mocking is a very handy tool for unit testing code, especially when it comes to mocking the HttpContext for web application code. However, it’s not as straight forward as you might think to mock the HttpRequestBase.ServerVariables, however once you know what to do it really is pretty simple. Here’s a code example (using moq) that shows how to create a mock HttpContextBase that contains a mock HttpRequestBase with a mock ServerVariables property: public HttpContextBase CreateMockHttpContext() ... [More]

Design Patterns: Basics of Dependency Injection

Dependency Injection and Inversion of Control (IoC) have become popular buzz words in the .NET development world over the last couple years. However, It seems that not very many developers really know what they are. Like most design patterns they are rather simple in idea, but the various implementations can become complex to someone new to them. Also, it seems that these two are often considered that they must go together or even mistaken as the same thing. This prompted me to write up this sho... [More]

SQLinq: Use LINQ to generate Ad-Hoc, strongly typed SQL queries

SQLinq is a new library that allows ad-hoc SQL code to be generated at runtime in a strongly typed manner that allows for compile time validation of your SQL code. Why SQLinq? SQLinq is built with the core idea of simplicity and ease of use. SQLinq wont get in your way like other Data Access Layers will. SQLinq is not so much a Data Access Layer (DAL) as it is a code generation tool. Although, it’s not a code generator like others you may be used to. If you look at Entity Framework, you’ll se... [More]

Unit Testing with SqlException in .NET: Only with help from Reflection

If you are writing you code to be able to easily unit test each method, and you would like to unit test situations where a SqlException exception is thrown, then you’ll definitely run into the issue of the SqlException objects constructor being marked “internal.” Members marked “internal” can be a nightmare for unit testing, and they are found all over the place in the .NET Framework. Basically you can’t call “internal” methods, constructors, fields, etc from you own code because it doesn’t resi... [More]

Entity Framework Tips and Tricks: Use GetObjectByKey When Querying a Single Entity

I’ve been using Entity Framework a lot lately and it makes data access really nice. It’s fairly simple to use and maps all your tables to a .NET object model. However, just like any other library, it can be misused and have it’s own issues. I’ve been figuring some things out as I’ve been using it, so I thought I’d share a few tip and tricks that I’ve learned. Here’s the first one: Use “GetObjectByKey” when Querying for a Single data entity Take the following query: var person = (from p in con... [More]

Mixing some Dynamic-ness with IronJS in .NET 4

I recently started messing around with IronJS again, and I must say that this project is pure awesomeness! One of the first things I noticed is that when you call the “Execute” methods to run some JavaScript code it returns an “object.” Now, while this works just fine, I would prefer if it returned a “dynamic” object while running against .NET 4.0. There may be some reason that Fredrik Holmstrom (the author) decided to return an ‘object’, so I don’t mean to sound negative, but you have to admit ... [More]

Odd .NET Namespace Scoping Causes Compile-Time Exception

This isn’t really a bug in the .NET compilers, but it sure is an interesting namespace scoping issue that could easily be encountered. If this teaches you anything, be it that you should never name a nested namespace level the same name as one of its parents in the hierarchy. In C# First here’s the Compile Time Exception that I was getting: The type or namespace name ‘Data’ does not exist in namespace ‘MyApp.Data.MyApp’ (are you missing an assembly reference?) At first I was like “What do yo... [More]

Silverlight: Modify MEF to load plugins from DLL and XAP files

I’m using MEF in a new project, and the limitation that I’ve found in using MEF with Silverlight is that it only supports downloading and loading plugins/assemblies from XAP files. What I would really like to do is support plugins that are compiled into either individual DLL’s or more complex plugins within a XAP. Update: I have also posted this to the Issue Tracker on the MEF CodePlex site. Why individual DLL’s? The really answer to this is flexibility. I don’t think they creators of MEF mad... [More]

Use New Bing Maps Road Imagery In Silverlight Map Control (Unofficially and Unsupported)

Recently the consumer facing Bing Maps site changed the map imagery that is displayed for the Road map mode. This map imagery has not been officially made available for Bing Maps for Enterprise developers to use within their applications. The imagery is only officially available to the Bing Maps consumer website. Unofficially, you can still access the imagery if you know the URL format to get it. Below is an example of using the URL format for the new imagery to display it within the Bing Maps S... [More]

Loading Microsoft CDN Hosted OpenStreetMap Imagery in Silverlight Bing Maps Control

Recently a new Bing Maps App for OpenStreetMap (OSM) was released. This new feature of the Bing Maps consumer facing website is hosting the OpenStreetMap imagery using the Azure CDN, rather than relying on OpenStreetMap’s server. There has been some question as to whether developers can use the Microsoft hosted OpenStreetMap imagery within their own applications. I have not seen anything official from Microsoft on this, so I assume that doing this is against their terms of use. You assume... [More]

Prototype of OpenStreetMap Silverlight Control using DeepEarth and Bing Maps SDK

I’ve decided to expand a little on using OpenStreetMap imagery with the new Bing Maps Silverlight Control in response to the following comment posted by John O’Brien on my previous “Display OpenStreetMap Imagery using Bing Maps Silverlight Control v1” post: “Very close Chris but you will still need to enter a Bing Maps AppID. If however you create your own map from MapCore and don't use the Bing Maps services then you don't need creditials” Yes, it is t... [More]

Getting Started with Bing Maps Silverlight Control v1.0

It’s been 8 months since the CTP release of the Bing Maps Silverlight Control at MIX’08, and finally this week Microsoft released the Final v1.0 release of the control to the web.  Now, we can all finally start using the Bing Maps Silverlight Control in Production! If you’ve worked with the CTP, then you’ll want to take a look at Ricky’s “Migrating from Bing Maps Silverlight CTP to Production Version” article. It explains all the stuff you’ll... [More]

Virtual Router Project – Turn any Windows 7 PC into a Wireless Access Point / Hot Spot

Over the last week some of you may have heard about Connectify. It’s an app that unleashes the “Virtual WiFi” and Wireless Hosted Network features of Windows 7 to turn a PC into a Wireless Access Point or Hot Spot. Well, I looked into what it would take to build such an app, and it really wasn’t that difficult since Windows 7 has all the API’s built in to do it. After some time of looking things up and referencing the “Wireless Hosted Network” C++ sample... [More]

Resolve IP Address And Host Name From MAC Address using C# and Windows ARP Utility

While working on the Virtual Router project, I’ve come across a need to be able to retrieve the IP Address and Host Name of a given machine on the local network when only the machines MAC Address is known. This took a bit of research to figure out, and eventually I stumbled across the “arp.exe” utility within Windows. “arp.exe” uses the Address Resolution Protocol to provide functionality to add, delete or display the IP address for MAC (Media Access Control) addres... [More]

.NET Windows Service Installer and Auto Start After Installation

It’s not very often that I need to build a Windows Service for a specific task, and it’s even more rare that I need to create an Installer (.msi) to install/uninstall the service. Well, the project I’m currently working on (http://virtualrouter.codeplex.com) requires me to 1) Install a Windows Service using a Setup Project, and 2) Start the Windows Service immediately after installation. Create Setup Project for Windows Service Just create a Setup Project within your Solution i... [More]

A Couple VB.NET Language Tips for C# Developers

Originally, I started out as a Visual Basic developer, and have since moved mostly to C#. However, when doing consulting work, I do need to cross back and forth quite often. Here are a couple VB.NET tips that you probably aren't aware of if you're mostly a C# developer. Some VB.NET developers may not even know about then either. Null Coalesce Null Coalescing is really simple in C#: [code:c#] // If "someValue" is Null then set "i" to 0 (zero) // otherwise set it to the value of "someValue" in... [More]

C#: Generate WebPage Thumbmail Screenshot Image

There are a few services out there that serve up screenshots of any webpage for you to display on your website. One popular one is Kwiboo; this is the one that DotNetKicks uses. For some time now I've wondered what the easiest way to do this in .NET was, and today I stumbled upon the undocumented WebBrowser.DrawToBitmap method that makes this extremely easy to do. By the way, I stumbled upon the WebBrowser.DrawToBitmap while taking a look at the source code for the WebPreview tool over at SmallS... [More]

C#: Enhance Enums using Extension Methods

Extension Methods are one of the coolest features that have been added in .NET 3.5. I've heard arguments that there is no reason to use them, and the only reason Microsoft added them is to enable the ability to buid LINQ. Well, I do not entirely agree with that statement; in fact, I have found a cool way to use Extension Methods to enhance the System.Enum object since it cannot be inherited. Even though Enum can not be inherited, it can be extended using Extension Methods. [More]

.NET: Why aren't Private Abstract methods allowed?

Sometimes when creating base objects you want to create methods that are overridable by object that inherit the base object, but keep the methods from being public. However, the compiler throws an exceptions when you try to define a method as "Private Abstract". Why doesn't the compiler like "Private Abstract" methods? The reason for this is Private methods can not be overriden. So, essentially the Abstract is useless if the method is Private. How can I create a "Private" method that's a... [More]