Posts Tagged ‘dotnet’

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.

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()

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

CompulsoryCat version 1.2

June 30th, 2011

Updated CompulsoryCat to version 1.2

The first big news is the Assemblyname functionality for retrieving a tree of assembly references for an application.

The other big news is that I genereated help files and uploaded too.

http://code.google.com/p/compulsorycat/

Assert.Inconclusive and my present almost-TDD compromise

April 1st, 2011

I am trying out Visual studio unit testing framework, the Microsoft alternative to, for instance, nUnit which was my old preferred testing framework (and possibly still is).

At the same time I am not in a project devoted to Test driven development so I can’t use its religion to write tests first and refuse to show any UI progress to the customer.  Right or wrong – that is how I perceive the reality around me right now.  But I do want to have tests and waiting for them I make sure I have place holders.

My compromise is to set the tests to Inconclusive  (Ignore in nUnit talk).  I guess it will come back to bite me…

What I also do is to make sure I have a test to go with every method.

        [TestMethod]
        public void AddOrUpdateTest()
        {
            Assert.Inconclusive();
            Elvis.Common.Meta.GetMethodName<BLF.Sample>(x => x.AddOrUpdate(null, null));
        }

I used the code from

http://stackoverflow.com/questions/1524290/linq-is-it-possible-to-get-a-method-name-without-a-return-type-via-linq-expressio

but I could probably use CompulsoryCat just as well.

I belive that even adhering TDD this technique is good.  There are so many times I don’t know what the classes and methods will look like before having done a simple implementation.  To write exhaustive tests for a method that will be deleted is a waste.  Better then to make sure the simple code runs and not forget to write the tests by having a yellow Inconclusive flag.

Include other files in config files in dotnet

November 19th, 2010

A seldom used feature of dotnet config files is the possibility to link to another file.  Use configSource=”theotherfile” or file=”theotherfile” as attribute.  An example and comments are found here.

This is old news indeed but every time I use it I tend to google for a while since I cannot remember the keywords.

Example of use:

I have an ordinary app.config file.  It links to connectionstrings.config which keeps my <drumroll/> development connection string.  For creating an installation file I have a normal setup/installation project.  This project takes the file connectionstrings.prod.config and incorporates it in the MSI while renaming it to connectionstrings.config.

Threading in C# by Albahari gratis

November 7th, 2010

Advise: before attempting parallel programming – read up on the subject!

Normally we program by intellisense and the assumption that the name of the method does what we suppose it does.  It can be thought of as lazy, that we don’t grok the framework before we start using it.  But is does seem to work.

This is not the case with parallel programming.  Do it without understanding it and you’re down the slippery slope.

Download the Threading chapter of C#4 in a nutshell by Albahari.  It is gratis.
Read up.  The text is easy to read and well written.

I have read one other part (looking for interface and inheritance and virtual) of the first release and it was also good.  If the rest of the book is as well written I suggest everyone to get a copy.

Exception: Only parameterless constructors and initializers are supported in LINQ to Entities.

October 30th, 2010

I am not sure of the *exact* meaning and reason for this but I found something that can be good to know.  Or not.

return context.NuggetSet.Where(n => n.Name == name).Select(x => new MyClass(x)).ToList();

threw a

Only parameterless constructors and initializers are supported in LINQ to Entities.

When inserting a ToList it worked as suspected/wanted.

return context.NuggetSet.Where(n => n.Name == name).ToList().Select(x => new MyClass(x)).ToList();

My guess is that by calling ToList we losen EF’s grip of the entities and can do what we want with it.