Friday, December 01, 2006

Adding Some Armor to Methods Using Passed Enums

In my previous post I demonstrated the fallibility of methods that take enumerations as parameters. As a defense you can use the Enum.IsDefined() method to check the validity of passed enum values:

static void SafeColorToString(EnumLegalColor color) {

if(!Enum.IsDefined(typeof(EnumLegalColor), color))
throw new Exception(string.Format("{0} is not a valid EnumLegalColor", (int) color));

Console.WriteLine(color.ToString());
}

Enumerations Aren't As Bullet Proof As I Thought

Pass an enumeration as a method parameter and its guaranteed to be valid right?
Wrong!

This code compiles and executes with no runtime error:
using System;

namespace Enumerating_Fun {

enum EnumLegalColor{red=1, white=2, blue=3};

class Program {

static void Main(string[] args) {

PrintColor(EnumLegalColor.red);
PrintColor(EnumLegalColor.white);
PrintColor(EnumLegalColor.blue);

PrintColor((EnumLegalColor) 1234);

while(!Console.KeyAvailable)
;
}


static void PrintColor(EnumLegalColor color) {

switch(color) {
case EnumLegalColor.red:
Console.WriteLine("Le Rouge");
break;

case EnumLegalColor.white:
Console.WriteLine("Le Blank");
break;

case EnumLegalColor.blue:
Console.WriteLine("Le Blu");
break;

default:
Console.WriteLine("WTF!");
break;
}
}
}
}

Wednesday, September 20, 2006

Disabling the ability to 'Set Web Reference' to a Web Service

You may wish to expose a 'private' web service such that in order for a new client to access it, the client must be furnished with a pre-built proxy class.

It seems there are two ways to accomplish this:

1. Implement the equivalent of the following in the WEB.CONFIG:


<webservices>
<wsdhelpgenerator href="DefaultWSDLHelpGenerator.aspx">
<!-- disable the web service test page -->
<wsdlhelpgenerator href="">
</wsdlhelpgenerator>
</webservices>


2. Implement the equivalent of the following in the WEB.CONFIG:


<webservices>
<protocols>
<remove name="Documentation">
</remove>
</protocols>
</webservices>


After implementing option #1, two things are very different:

1. Instead of help, you get a blank page in your browser.

Pointing your browser at the web service no longer yields the web service's Home /Help page. You know, the one with the big deep blue title banner with a 'invokable' (hyper-linked) list of all of the web methods below a line of text that reads "The following operations are supported. For a formal definition, please review the Service Description."



2. You cannot set a Web Reference to the service.

Trying to set a Web Reference to the Web Service from Visual Studio will ultimately result in an error. VS will be able to find and list the Web Service and it looks promising but selecting it and saying 'go' is an exercise in futility.

After implementing option #2, things are the same as implementing option #1 except that instead of getting a blank web page you get an error page saying:

Request format is unrecognized.


Presumably this is because a request for the main asmx page is interpereted as a documentation request and that protocol has been removed from the http handlers vocabulary. ... or something like that.

Wednesday, August 24, 2005

Friendly casting using 'as'

Running this code will NOT cause a runtime error if 'b' is not of type 'myclass'. 'a' will simply be assigned null :

myclass a;

a = b as myclass;

if(a == null)
Console.WriteLine("Oh well. b is not a myclass.");
else
Console.WriteLine("Hey, guess what! b is a myclass! Wow!");


This code will throw an invalid cast exception:


myclass a;
someotherclass b;

a = (myclass) b;


One might say "What's so cool about using 'as'? I can do this with roughly the same amount of code. Like this...":

myclass a;
someotherclass b;

try{
a = (myclass) b;
Console.WriteLine("Hey, guess what! b is a myclass! Wow!");
}
catch{
Console.WriteLine("Oh well. b is not a myclass.");
}


That's neat, but guess what? That's using the .Net framework's exception handling mechanism for flow control. In all the examples above the implication is that there is not just the possibility that 'b' is not a myclass but rather there is some likelihood that 'b' is not a myclass. Using the exception handling for flow control is very bad form and a bad habit to get into. Its very slow and can make the debugger stop for no good reason. Don't do it!

Friday, August 19, 2005

Using C#/.net Reflection to call Private and Protected methods

Today I learned that it is possible to call private and protected methods by using reflection.
This is a great technique for Unit Testing such critters for proper behavior when 'code that shuold never be reached' gets reached.

Example:


internal string GetAString(int stringId)
{
foreach(DataRow dr in _theDataSet.Tables["mySTringTable"].Rows)
{
if((int)dr["strID"] == stringId)
return((string)dr["theStringValue"]);
}

//Impossible or very difficult to create a test situation
// where this code would ever get executed. The methods
// that DO have access to this method ALWAYS pass a valid stringId.
throw new NotFoundException("String not found!");
}


Check out this link:

http://www.codeproject.com/csharp/TestNonPublicMembers.asp

Or google:

"reflection unit test private method"

Tuesday, August 09, 2005

Saving View State to a Place OTHER THAN a Hidden Field on the Page

If you want to keep the view state server-side or in some other place on the rendered page.
You might, for instance, determine that the wire cost exceeds the server-side memory cost and therefore want to keep View State on the server.

Override these two methods :


Saves any view-state information for the page.
Override this method if you want to save the Page view state in anything other than a hidden field. If you choose to do so, you must also override the Page.LoadPageStateFromPersistenceMedium method.

protected virtual void SavePageStateToPersistenceMedium(object viewState);



Loads any saved view-state information to the Page object. Override this method if you want to load the Page view-state information in anything other than a hidden field.

If you want to specify something other than hidden fields to save view state when using this method, you must also override the SaveStateToPersistenceMedium method.

protected virtual object LoadPageStateFromPersistenceMedium();

Thursday, December 02, 2004

Joining on objectSID or using objectSID in a WHERE Clause

This one drove me crazy for the better part of a day.
When SQL Server shows you an objectSID it looks something like this:

0x010500000000000515000000317B347F75771D5BB254B44007080000

If you take that string and type something like:

SELECT * FROM viewADEmployees WHERE objectSID = 0x010500000000000515000000317B347F75771D5BB254B44007080000

It works just fine.

But if you take that string and stuff it in a VARCHAR (or any other type you think might be appropriate) you get an empty result set.

So I thought, duh, I need to cast it to the same type as objectSID. It only took a little while to figure out that objectSID is of type VARBINARY. It turns out, however, that simply casting the string to VARBINARY does NOT work! In fact I could not find any CAST or CONVERT that did the trick. I performed more casts than the entire fishing population on opening day of trout season.

Google was of no help either. I was on my own on this one.

I finally stepped back reasoned it out:

1. objectSID is VARBINARY
2. I need to convert my comparison value to VARBINARY
3. I should create a user function that takes a string representation of a series of hex byte values
4. each two-character pair of hex digits reprsents a single binary byte
5. I need to build up a VARBINARY value by successively converting the pairs to integers and then adding them to my VARBINARY return value

This is my resulting function which took a suprisingly short period of time to conceive, write and debug :

CREATE FUNCTION dbo.fncFormatSID (@vchFromSID VARCHAR(300))
RETURNS VARBINARY(85)
AS
BEGIN
DECLARE @binOut VARBINARY(85);
DECLARE @intLen INT;
DECLARE @intIndex INT;
DECLARE @intLow INT;
DECLARE @intHigh INT;
DECLARE @vchLow VARCHAR(1);
DECLARE @vchHigh VARCHAR(1);

SELECT @intLen = LEN(@vchFromSID)
SELECT @intIndex = 2

WHILE (@intIndex < @intLen)
BEGIN

SELECT @vchHigh = SUBSTRING(@vchFromSID, @intIndex, 1)
SELECT @vchLow = SUBSTRING(@vchFromSID, @intIndex + 1, 1)

IF(@vchLow >= '0' AND @vchLow <= '9')
BEGIN
SELECT @intLow = ASCII(@vchLow) - ASCII("0");
END

IF(@vchHigh >= '0' AND @vchHigh <= '9')
BEGIN
SELECT @intHigh = ASCII(@vchHigh) - ASCII("0");
END

IF(@vchLow >= 'A' AND @vchLow <= 'F')
BEGIN
SELECT @intLow = ASCII(@vchLow) - ASCII("A") + 10;
END

IF(@vchHigh >= 'A' AND @vchHigh <= 'F')
BEGIN
SELECT @intHigh = ASCII(@vchHigh) - ASCII("A") + 10;
END

IF @binOut IS NULL
BEGIN
SELECT @binOut = CONVERT(VARBINARY(1), (@intHigh * 16) + @intLow) ;
END
ELSE BEGIN
SELECT @binOut = @binOut + CONVERT(VARBINARY(1), (@intHigh * 16) + @intLow) ;
END

SELECT @intIndex = @intIndex + 3

END /* While */


RETURN(@binOut);

END

Accessing Active Directory From SQL Server

Figured out how to query active directory from SQL Server. Its pretty well documented but the first step is to create what is know as a 'Linked Server'. This involves executing one line of code in Query Analyzer:

sp_addlinkedserver 'ADSI', 'Active Directory Service Interfaces', 'ADSDSOObject', 'adsdatasource'

Then you can execute stuff like this:

SELECT * from openquery(ADSI, 'SELECT cn,objectsid FROM ''LDAP://atlas/CN=Users,DC=doublestarinc,DC=com''')

I'm not sure if its possible to perform a join directly with this. To save time I created a view using something similar to the above SELELCT statement. Then using that view we can all join to our heart's content.

Friday, November 05, 2004

Passed 70-316 Today !

Scored a 920 !

Didn't think I did better than the 70-315 test but somehow I did.

Now on to 70-320.

Tuesday, September 28, 2004

Whacky, Nutty Keyboard Antics

My keyboard layout got whacked out for Office products only.
a would print q, comma would print : stuff like that.

Solution:

All Programs -> Microsoft Office -> Microsoft Office Tools -> Microsoft Office 2003 Language Tools.

All Programs -> Microsoft Office Tools -> Microsoft Office XP Language Tools.

In both locations found the French to be the Enabled and Active Language.

Deleted it from the the Active Lists.

Viola!

Wednesday, September 15, 2004

Tools I Used Today

Made my first professional forray into VBA / Access Front end development.

Yuck!

Did a good job though.

C# Sucks!

JK!! Seriously, though, somewhere around C#-3 we should have inculcated ourselves with the question: "Does 'CAN' == 'SHOULD...