Archive for the ‘Code and Development’ Category

System.Messaging.MessageQueueException was unhandled : A workgroup installation computer does not support the operation.

January 29th, 2012

If you play with MSMQ and get an exception like

System.Messaging.MessageQueueException was unhandled
Message=A workgroup installation computer does not support the operation.
Source=System.Messaging
ErrorCode=-2147467259
StackTrace:
at System.Messaging.MessageQueue.ResolveFormatNameFromQueuePath(String queuePath, Boolean throwException)
at System.Messaging.MessageQueue.get_FormatName()
at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 action, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
at System.Messaging.MessageQueue.Receive()

 

It might be due to a reason like mine.

With

 

var queues = MessageQueue.GetPrivateQueuesByMachine(“.”);
var queue = queues.Where(q => q.QueueName == “private$\\mynewqueue”).Single();
var message = queue.Receive();

it does work
but with

 

var queue = new MessageQueue(“private$\\mynewqueue”);
var message = queue.Receive();

it doesn’t.

Change to

 

var queue = new MessageQueue(“.\\private$\\mynewqueue”);
var message = queue.Receive();

and you might be good to go again.

I haven’t bothered to figure out exactly why but my workaround might help you.

Tilt your extra monitor to portrait

January 27th, 2012

I, of course, have two monitors for my development computer.  (sometimes three but that is subject for another post)  The extra monitor I have is tilted to portrait so it is higher than wide.  I strongly recommend it.

We write code with the flow going from the top to the bottom.  Having a high monitor makes me see more code and keep less in the RAM part of the brain.

One could argue that methods should be short and there hence is no need for a screen that is so high that two methods are visible at once.  One could also argue that cars should be fragile so as to feel the need for avoiding collisions. Or calculators to calculate wrongly so we learn to do it manually instead.

Try tilting your extra monitor to portrait today!  You might not like it – hacking queries is better done wide, spreadsheets is often wide, presentations are wide – but then you have your other landscape monitor for that.

You might have to rearrange some tools in your favourite development environment but that is easy done.

I dare you to try it and leave a comment.

 

TortoiseSVN and code.google

January 27th, 2012

The quick and dirty documentation found for instance here hints at one should write the project name and user name together with the https source path.  So is not the case if one uses TortoiseSVN.

So the string

https://compulsorycat.googlecode.com/svn/trunk/ compulsorycat --username myusername@gmail.com

should really be

https://compulsorycat.googlecode.com/svn/trunk/

Not that hard to figure out really.  But it took me a while.  I even missed it at this helping side.

Just for information.  Nothing fancy.

Convert dotnet DateTime to Sqlserver SqlDateTime

December 9th, 2011

The SqlDateTime has a narrower span than the Dotnet ditto; dotnet starts from year 0 but sqlserver from 1753.  This means that uninitialised DateTime fields in dotnet will give runtime execution errors when trying to persist them in a sqlserver database.

I write together a small and simple method for making sure the DateTime interval is inside the valid SqlDateTime interval.

public static class DateTimeExtensions
    {
        /// <summary>This method returns the datetime fitting into min/max of Sqlserver
        /// as per http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DateTime ToValidSqlserverDatetime(this DateTime dt)
        {
            //  Must cast System.Data.SqlTypes.SqlDateTime.MinValue: http://codebetter.com/petervanooijen/2008/08/21/valid-date-time-values-in-sql-server-sqldatetime-vs-datetime/
            if (dt < (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue)
            {
                return (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
            }
            else if (dt > (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue)
            {
                return (DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue;
            }
            else
            {
                return dt;
            }
        }
    }

The code is easier to read at pastebin.

Honour those who should.

 

 

 

 

 

 

Scope_Identity doesn’t return an Int in sqlserver but a Decimal

December 5th, 2011

This is an old item.  Scope_Identity in Sqlserver doesn’t return an Int but a Decimal.  So in Dapper the call is

                var id = conn.Query<Decimal>(...

Honour those who should and here.

VB.Net and lambda

November 30th, 2011

Using the terse lambda syntax used in C# also in VB.Net is also (almost) possible.

Here is a short example:

var nameList =
 myFacade.GetData().
 where( x => x.ID = 42 ).
 select( x => x.Name ).
 ToList();

which in VBNet becomes:

Dim nameList =
 myFacade.GetData().
 Where( Function(x) x.ID = 42 ).
 Select( Function(x) x.Name ).
 ToList()

Note that the periods have to be at the end of the line, otherwise the multi line capability of Vbnet doesn’t work.  This can be remedied by the old trailing underscore of VB.

If you need to set the type of x it is also doable:

Dim nameList =
 myFacade.GetData().
 Where( Function(x As MyClass) x.ID = 42 ).
 Select( Function(x As MyClass) x.Name ).
 ToList()

Unit tests, TDD, BDD

November 28th, 2011

Maybe we shouldn’t think of unit tests, TDD or BDD but more of go no go tests. Whatever you automatically test it has a Go/NoGo, Yes/No, Success/Fail result.

C#: string.ToUpper() and char.ToUpper()

November 16th, 2011

In C# there is a

"mystring".ToUpper()

but nothing in the char class.  Instead use

char.ToUpper('x')

… or write an extension method.

Singleton in dotnet

November 15th, 2011

Singleton is often considered the simplest pattern but I digress. Except for the simplest cases writing a singleton requires thorough knowledge about the language at hand; one really has to know the locks and ifs and constructors to make a zyzygy.

Below is linked a good article. I don’t consider it as wordy since it really explains the subject in depth in a simple way.

There are 6 examples where 4 can be used. At the bottom of the article is a short explanation if you don’t want to grok it.

- http://csharpindepth.com/Articles/General/Singleton.aspx

Aspnetmvc 4 and standard dotnet membership and roles

October 26th, 2011

I recently added the aspnet way of handling membership and roles to an experimental site.

One should not write one’s own usr/pwd handling since there are too many places to go wrong.

The MS tutorials all take for granted we want our users in a sqlexpress database situated as app_data in the web project.  For most real solutions we want to have the users in a database of our own pick.  Pointing aspnet to this one is not hard but has some caveats.

Update your database either through aspnet_regsql.exe found in the dotnet framework folder.  This application creates a bunch of stored procedures in the database you chose.  If you don’t want everything – say you only want roles and membership but not personalization or schema you have to run aspnet_regsql.exe with parameters.  You can also run some scripts to do the same, the same scripts that aspnet_regsql.exe uses.

Then you have to point your site (web.config) to use the database.  There is already a default membership provider in machine.config and we want to override it.

Create your ususal connection string.

<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString=...;MultipleActiveResultSets=True" />
  </connectionStrings>
...

Then to point at it scroll down to (aspnetmvc template has already created it) membership and update.  Check the bold for clues.
“Clear” is used for overriding the machine.config value.  And since you override it you have to provide a new; that is where ”defaultProvider” comes in so set it to the same value as “name”.  Finally  ”connectionStringName” must be equal to your connectionstring’s name as above.

    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <clear />
        <add connectionStringName="DefaultConnection"              ...
             name="DefaultMembershipProvider"               ...
         />
      </providers>
     </membership>

Then you probably want Roles to also be handled in your own database so do the same for “roleManager”.

All this can be found on the web but I spent an hour on it so I thought I’d jot it down for future googling.

Sources:
http://computerscribe.com/blogs/BlogPost?id=11
http://www.asp.net/security/tutorials/creating-the-membership-schema-in-sql-server-cs
http://forums.asp.net/t/978442.aspx/1