Posts

Showing posts from January, 2010

Using Resources / Resx objects on a Web Site

In an earlier post I showed how you can take any Resources object(especially PinnedBufferMemoryStream and UnmanagedMemoryStreamWrapper) and obtain it’s byte[]. Unfortunately on the web this is not sufficient because when the byte[] is sent to a client. The browser expects a mime type header to tell it how to handle the byte[]. A partial solution is to simply map each mnemonic to a specific known mime type . This is a fragile solution.   If you are doing localization then a mnemonic such as SiteLogo may be different mime types in different languages, for example: en-US: image is a  .bmp es-Mx: image is a  .png fr-CA image is a  .gif (animated) dk-DK image is a  .tiff Of course, one could simply specify to human-ware that they must all be the same . In reality, if the web-site is a commercial product, this will usually break down.   The practical solution is to use Magic Numbers . Use the magic number to determine the file type, then lookup the mime type bas

NEVER use NT Groups to control SQL Server Permissions

About a year I go I looked at the use of Windows Group as a mechanism for controlling access to SQL Server for a PCI-DSS project. PCI requires that all access be done by accounts that uniquely identify the user. Best practices mandate Integrated Security so we must use Windows Users. There were two apparent solutions: Associate each Windows user to appropriate SQL Login Associate each Windows user to a Windows Group that is associated to a SQL Login Since the expected administrators of the PCI application are not DBA’s and may not be SQL Server knowledgeable, the second approach looked ideal. Their role is to determine who may or may not access the application. This simple idea had a nasty gotcha: Grant SQL permissions to anyone in the Windows Group. This work perfectly for granting permissions I expected that if a user was removed from the Windows Group, permissions would be immediately removed. UNFORTUNATELY , if a Windows user was removed from

An Improved Path.Combine to Handle Relative Paths and ~’s

Recently I was working with parsing a .Resx file which stored file locations as relative locations (i.e. ..\..\..\Images\Logo.jpg ) and found that path combine does not work with such. I have often wished Path.Combine( DirectoryInfo , string) was valid but because Path is static I could not extend it. I ended up writing a short utility below which handles more variations than Path.Combine.   /// <summary> /// A utility to resolve ..\ in the relative path /// </summary>/// <param name="directory">Directory Information on the base </param> /// <param name="relativePath">The dotted relative path</param> /// <returns></returns> public static string CombinePath(DirectoryInfo directory, string relativePath) { while (relativePath.StartsWith( @"..\" )) { relativePath = relativePath.Substring(3); directory = directory.Parent; } if (relativePath.StartsWith( @"~"

Using Datasets in WCF and Webservices with Legacy Applications

Recently I came across the issue of XML DataTable to Clean XML , specifically the problem of “ Web Service has to deliver neutral/agnostic XML and NOT .NET related information. ”  The solution code used XmlDocument which is not the best performing, as well as not being to “monkey-see, monkey-do” quality that is often needed.   The solution is simple if you add an extension to the dataset as shown below: /// <summary> /// Returns a dataset as a Xml String /// </summary> /// <param name="ds">A DataSet</param> /// <param name="isDotNet">If true, optimize for .Net client, if false for Legacy(Java)</param> /// <param name="includeSchema">If true,include the Schema in the Xml</param> /// <returns>The dataset serialized as Xml</returns> public static string GetXml( this DataSet ds, bool isDotNet, bool includeSchema) { var swriter = new StringWriter(); using (var xwriter = ne

Exporting .Resources to Sql Server (byte [] or string)

One of my current tasks is writing a utility to import the contents of an arbitrary random .resources file (compiles .Resx file) into SqlServer. The problem is that there some of the resources returned as objects are INTERNAL classes, namely: System.IO.PinnedBufferMemoryStream System.IO.UnmanagedMemoryStreamWrapper (1 page of Google results only… the unknown .Net Class) Two solutions to this": Add your own equivalent classes using the source at: http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System.IO&type= UnmanagedMemoryStreamWrapper http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System.IO&type= PinnedBufferMemoryStream Read through the code and realize the the following code will deal with these obstructive classes.                                      byte[] bytes=null;                // The following two classes are not exposed :

Getting detail System Information without a DLLImport in dotNet

The two functions below can make getting information trivial because   it writes the information out into clean XML which may be examined by XPath or LINQ. The first function calls the second using a built in list of the WMI objects (this can take a long time to run and will produce a huge file: BE WARNED -- run it once to see what is available.) The second function writes a short list specified in a string array (for example: use a text file with one item per line). This is likely the most common use. If you are planning to write to a file, you may find encoding issues. using System; using System.Collections.Generic; using System.Management; using System.Text; using System.IO; using System.Xml; Everything in the Wmi The following obtains the list of available objects (some 1200+ on my Windows 7 box) and then gets information about each   /// <summary> /// Enumerates all of the WMI classes on the local machine /// </summary> /// <

Purple Software

This is a follow-up to my post: What Is A Software Company?   A software company’s job is to distribute the cost of making the software over many customers.  If this is true then acquiring as many customers as possible is important to the company.  The more customers you acquire the less you can sell your software for.  If your software costs a million dollars to develop and you have ten customers you need to sell it for $100,000 per customer.  However, if you have 100,000 customers, you can sell it for $10 per customer (somewhere you need profit).    Being able to sell it for less means you make it harder for your competition.  If both companies require a million dollars to make the software and you have 100,000 customers and they have 10 customers, you will have very different price points, making it hard for them to reach profitability or  acquire the next customer.  It also makes it very hard for another company to enter the market.  Because of the market dominance effect o

What Is A Software Company?

As a programmer running a software company I didn’t realize this at first:   “A software companies job is to distribute the cost of making the software over many customers”   It is a disservice to your customers to not acquire enough customers to cover the cost of making the software.  If after you have spent your investors money to make the software, and you get too few customers to continue operations and repay the investors eventually you will go out of business.  Going out of business is not what your customers want from your company, they purchased your software (over choosing open source or shareware) so that it would have company support.  They have an expectancy of continual upgrades, and some form of product support.   In other words, if you can’t acquire enough customers to distribute the cost of the software you don’t have a software company, you have an open source project.  Though open source is many things, it can also be thought of as a software p

Lazy exception handling...

The typical C# code seen does: try{ } catch(Exception exc){ }  Best practices are to handle each type of exception that could occur separately, tools like Telerik JustBeans can/will soon autogenerate the exception possibilities. This brings me to the second aspect, the failure to use:   /// <exception cref= "System.Exception" > Thrown when... .</exception> see documentation . What is sad, is that tools like StyleFx do not support checking for this (when it is possible). I often seen magic-phrase strings being returned in code that I review, instead of the exception being allowed to bubble up the stack.

New Years Predication - Photography

My parents are scanning their slide collection into digital images and for the years between 1971 and 1981 they have 780 pictures, for the first ten years of my life there are 78 pictures a year. I remember getting my first camera around the age of 13 the cost of film was roughly $5.00 per roll and $10.00 to develop. 24 pictures were worth 60 cents apiece. Photography was too expensive in the late 80s to experiment with – every picture had to be a good one and we would wait weeks to see if the pictures we took would turn out.   Today, 110 years after George Eastman introduced the Brownie to the world, I will take 100 pictures in a day. There are no costs associated with the picture taking, just the price of the camera. Because it doesn’t cost me anything, my 4 year old daughter has her own Fisher Price camera and has taken more photos then her grandparents took those first ten years.   Just like Tiger Woods started golfing at age 2 to become a world champion golfer, children