ADO .NET Entity Framework Vote of No Confidence

by bill 6/23/2008 2:26:00 PM

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Defensive Event Publishing

by bill 5/26/2008 3:48:00 PM

How does one go about publishing event in a safe way so that one could minimize the chance of exceptions?  Let's say you have a class such as this:

public class MyClass
{
      public event EventHandler MyEvent;
}

The first thing that comes to mind when you want to publish MyEvent would be to just invoke the delegate.

MyEvent(this, EventArgs.Empty);

The first issue that you would find is that if no one has subscribed to the event then just invoking the delegate would cause a null reference exception.  So we need to check for null.

if ( MyEvent != null )
    MyEvent(this, EventArgs.Empty); 

This leads to another issue.  Someone could unsubscribe from the event handler after we check for null which would lead to another null reference exception when we invoke the delegate.  Since delegates are immutable we can copy the delegate to a variable and it will keep its state even if someone unsubscribes from the event.  So we assign it to a variable, check the variable for null then invoke.

EventHandler handler = MyEvent;
if (handler != null)
    handler(this, EventArgs.Empty); 

That much code for invoking a delegate smells bad and needs to be moved to its own method.  So we refactor using ExtractMethod and create an OnMyEvent method.

protected void OnMyEvent()
{
    EventHandler handler = MyEvent;
    if (handler != null)
        handler(this, EventArgs.Empty);

Now that we have the method there is one hidden issue that most people don't realize could cause us problems which is compiler optimization.  The JIT compiler might decide to optimize our method, removing our variable and just make the call directly to the event delegate leaving us open for another null reference exception.  We can stop that from occurring by adding an attribute which will tell the compiler to not optimize the method.

[MethodImpl(MethodImplOptions.NoInlining)]
protected void OnMyEvent()
{
    EventHandler handler = MyEvent;
    if (handler != null)
        handler(this, EventArgs.Empty);

This gives us a self contained safe and easy way to raise the event.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Vista Sidebar CC.Net Monitor

by bill 6/23/2007 8:38:00 PM

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Windows 2003, IIS 6, FileStream, UnauthorizedAccessException

by bill 4/2/2007 8:22:00 PM

Hopefully, if you are getting an UnauthorizedAccessException in IIS 6 while trying to write out a file this will help you.

Today a client was frustrated after rolling out an application from staging to live.  The web application created thumbnails on the fly and caches those thumbnails to disk using a FileStream.  The code worked fine in the dev environment and in the staging environment, but as soon as it went to the live server it started throwing an UnauthorizedAccessException whenever the file was being written.

After searching Google for the terms in this posts subject I finally came across what I thought might fix the problem, sure enough it did. The IIS_WPG group must have the correct rights in the folder you are trying to write files to.

---------------------- Snip of the error -------------------
UnauthorizedAccessException: Access to the path '[THE_PATH_WAS_HERE]' is denied.]
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +2013027
System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +998
System.IO.FileStream..ctor(String path, FileMode mode) +65

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Simulating Persisted StateMachine Workflows

by bill 3/30/2007 11:18:00 AM
I wanted the unit tests for a StateMachineWorkflow to act as if they are persisted so I created the InMemoryPersistenceService.  Basically the service serializes the activities to memory then stores them as byte arrays in dictionaries using the guid as the key.  Fairly simple stuff, but usefull.

InMemoryPersistenceService .cs (5.64 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Multiple Generic Types + Multiple Constraints

by bill 1/7/2007 11:02:00 AM

I could not remember how to do this (I kept trying to use a comma).

 

public T CreateSomething<U, V>()
    where T : SomeObject
    where U : SomeObject<V>
    where V : SomeThingElse, new()
{
    return .....
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Nesting Project Items

by bill 1/3/2007 1:56:00 PM

I don't really understand why this isn't built in to vs.net.  Delarou's macro makes life alot easier if you want to do any item nesting.

---

"This macro enables you to nest project items inside Visual Studio .NET. Until now, there is no easy way to nest project items through the Visual Studio IDE, you can only do it by manipulating the project (.csproj or .vbproj) file and adding the DependentUpon element."

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

GridView Combined ImageField and HyperLinkField

by bill 12/8/2006 9:50:00 PM

Is it so hard to imagine that someone someday would want to link an image to something from within the GridView?  You can't just set some link properties on the ImageField or set some image properties on the HyperLinkField. So I broke out Reflector and made an ImageLinkField. 

 

ImageLinkField .cs (12.75 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

C# Exception Class Template

by bill 11/21/2006 8:26:00 AM

Download the ExceptionClass.zip (1.53 kb) and place it in [Drive]:\Documents and Settings\[User]\My Documents\Visual Studio 2005\Templates\ItemTemplates\Visual C#\.

The next time you go to add a file, ExceptionClass should be in your "My Templates" section at the bottom.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Simple ClickOnce Utils

by bill 11/8/2006 7:59:00 PM

Two simple methods for ClickOnce deployed applications.

 

public static class ClickOnce
{
    /// <summary>
    /// Determines whether this instance is deployed.
    /// </summary>
    /// <returns>
    ///     <c>true</c> if this instance is deployed; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsDeployed()
    {
        return (AppDomain.CurrentDomain.ActivationContext != null && ApplicationDeployment.IsNetworkDeployed);
    }

    /// <summary>
    /// Parses the query string.
    /// </summary>
    /// <returns><see cref="NameValueCollection"/> containing the key/value pairs from the ActivationUri if they are found; otheriwise an empty <see cref="NameValueCollection"/>.</returns>
    public static NameValueCollection ParseQueryString()
    {
        if (!IsDeployed())
            return new NameValueCollection();

        Uri uri = ApplicationDeployment.CurrentDeployment.ActivationUri;
        if (uri != null)
        {
            NameValueCollection parms = HttpUtility.ParseQueryString(uri.Query);
            if (parms.Count != 0)
                return parms;
        }

        return new NameValueCollection();
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Name of author Bill Rodenbaugh
I bang on the keyboard and somehow the end result turns into some kind of thing that sometimes does something.

E-mail me Send mail
View Bill Rodenbaugh's profile on LinkedIn

Calendar

<<  July 2008  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Recent comments

Authors

Categories

None


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in