Welcome to WindowsClient.net | Sign in | Join

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)

June 2008 - Posts

Managing Multiple Configuration File Environments with Pre-Build Events

Almost every project I write has a configuration file and its content varies from one environment to another.
In this case, managing multiple configuration files can make life a lot easier.

There are different approaches for doing this, I'll link to a demonstration made by Scott Hanselman a while back - Click Here.

In short:
1 - Create configuration builds.
2 - Add a batch file at the root of the project as written below.
3 - Add a Pre-Build action to execute it.

The batch file 'copyifnewer.bat' content:
@echo off
echo Comparing two files: %1 with %2

if not exist %1 goto File1NotFound
if not exist %2 goto File2NotFound

fc %1 %2
if %ERRORLEVEL%==0 GOTO NoCopy

echo Files are not the same.  Copying %1 over %2
copy %1 %2 /y & goto END

:NoCopy
echo Files are the same.  Did nothing
goto END

:File1NotFound
echo %1 not found.
goto END

:File2NotFound
copy %1 %2 /y
goto END

:END
echo Done.

The pre-build action to be inserted:
"$(ProjectDir)copyifnewer.bat" "$(ProjectDir)web.config.$(ConfigurationName)" "$(ProjectDir)web.config"

 

WCF - Creating a certificate

I implemented a Security Token Service in WCF as part of our WS-Trust solution here in our project.

I needed a certificate to apply the message security and all that WS-Federation requires.
I needed to set it up quickly for a temporary purpose, thus I created a certificate using 'makecert.exe' (accessible through VS2008 Command Prompt).

One of the valid set of arguments to create a certificate for such use is:
makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=WCFCert2 -sky exchange -pe

You can change the store name / location and certificate name as you like.

I'll end by saying that using such certificates in production is not recomended.
The best thing to do is to get a chain-trusted certificate for your STS and services.

One additional thing - in order to add a certificate to the trusted people store execute the following:
certmgr.exe -add -r LocalMachine -s My -c -n localhost -r CurrentUser -s TrustedPeople

Posted: Jun 29 2008, 07:38 AM by Amir Zuker | with 1 comment(s)
Filed under: ,
PInvoke Interop Assistant

Via Nicholas Allen's post.

"new tool on CodePlex the other day called the PInvoke Interop Assistant that automatically converts between managed and unmanaged type signatures. In addition to converting API functions, it also pulls together all of the structures and types used by the API. Here's a sample showing how it handles CoCreateInstance."

This is a common action I find myself in need for here and then, this looks like a handy tool for doing that.

Posted: Jun 25 2008, 07:36 PM by Amir Zuker | with no comments
Filed under:
Enum - Working with [Flags]

A nice feature of the language is the ability to tag an enum with the [Flags] attribute.

Such tagging makes the use of the enum in a bitwise manner, you can choose multiple values to an enum field.

Working with such enum tagged with flags introduces a different approach than working against a normal enum.

Let's assume we define the following enum:

[Flags]
public enum Day
{
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}

Tip: An "All" item could be added to the list of items in the enumeration as follows: All = Sunday | Monday | Tuesday |.. So On

Imagine the purpose would be writing an alerter service which invokes a certain operation on a pre-defined day.
There can be multiple days chosen so this enum answers this requirement.

The first value should be 1, then just double the value for each consecutive item.

Here's how you work against such an enum:

Declaration: (Same as normal enum)

Day day = Day.Sunday;
Day day = Day.Sunday | Day.Monday (sets the day to contain both Sunday and Monday)

Adding a Day::

day |= Day.Tuesday;

Removing a Day:

day ^= Day.Tuesday:

Checking if the field contains a day value:

bool contains = (day & Day.Tuesday == Day.Tuesday); (checks if day contains Tuesday)

A little bonus - checking if the field contains a 'DayOfWeek' (.Net enum type, non flags):

bool contains = (day & (Day)Math.Pow(2, (double)DayOfWeek.Tuesday) == Day.Tuesday); (checks if day contains DayOfWeek.Tuesday)

Posted: Jun 19 2008, 09:58 PM by Amir Zuker | with 1 comment(s)
Filed under:
Threading Thoughts

Multi-threading is an important concept, we can all agree to that.

We could find ourselves using threads even if we're not in an environment which is multi-threaded by nature. (services and such)

But how do we write atomic code? (Thread-Safe)
Well, there isn't a simple answer to that, obviously.

The key thing is: Be aware of any instance used concurrently.

You can read the following post:
Implementing The Singleton Pattern in C#
It describes various ways to implement the singleton pattern specifically, but you can take a lot from it to other scenarios as well.

Plus, there's the MSDN article:
Managed Threading Best Practices

Some key points I pulled out myself:

Singleton Pattern -

  • Use Lazy initialization / Double-check lock mechanism (consider defining the field as volatile)

If you do use the double-check, you can apply what is described at the bottom.

Instance in concurrent environment -

  • Use Interlocked where possible to ensure thread-safety with performance optimization
  • Perform Locks & Monitoring on reference-type objects (not value-types, not types and such)

Use of Interlocked -

Statement
lock(lockObject)
{
    myField++;
}

Change to
System.Threading.Interlocked.Increment(myField);

Statement
if (x == null)
{
    lock (lockObject)
    {
        if (x == null)
        {
            x = y;
        }
    }
}

Change to:
System.Threading.Interlocked.CompareExchange(ref x, y, null);

A quick example for implementing locking with a handle object using Interlocked:
public class SyncUsingSyncHandle
{
   private object synchHandle;
   private object GetSynchHandle()
   {
      System.Threading.
Interlocked.CompareExchange(ref synchHandle,
                  new object(), null);
 
      return synchHandle;
   }
 
  
private void DoSomethingSynchronized()
   {
      lock (GetSynchHandle())
      {
         //perform logic
      }
   }
}
 

Note: You can simply perform lazy instantiation with the handle and lock its instance, but this way you're using Interlocked and its optimizations.

Writing atomic code is certainly a crucial concept when working in concurrent environments, we should "Think Runtime".

Posted: Jun 12 2008, 11:29 AM by Amir Zuker | with no comments
Filed under:
WCF Visualizers

A very cool set of visualizers for WCF classes.

Find the it in CodePlex - Here.

The set of available visualizers are currently for:

1. Message Visualizer
2. ChannelDispatcher Visualizer
3. ServiceDescription Visualizer
4. ServiceHost Visualizer
5. ServiceEndpoint for the client side.
6. ClientRuntime Visualizer
7. Binding Visualizer
8. SecurityContext Visualizer
9. Security Visualizer

Posted: Jun 10 2008, 08:12 AM by Amir Zuker | with 1 comment(s)
Filed under:
Silverlight 2 Beta 2

Yeap, it is here.

Silverlight 2 beta 2 was release last week.

It had been posted over 999,999,999 blogs so I won't say much about it.

It does have some cool new things, check Scott's post.

Posted: Jun 10 2008, 08:09 AM by Amir Zuker | with no comments
Filed under:
Page view counter