System.IO.FileLoadException Could not load file or assembly ‘Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies

We’d just published a ASP.NET MVC 4 SPA to assist QA in generating test data.  This SPA uses the ASP.NET Web API framework to GET and PUSH data to a data repository. The SPA transmits all data as JSON. This requires JSON.NET. The version referenced is version 4.5.0.0.  Locally it worked fine but when published to a win svr 2008 R2 environment any calls to the api would result in this:

{"ExceptionType":"System.IO.FileLoadException","Message":"Could not load file or assembly 'Newtonsoft.Json, Version=4.0.8.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)","StackTrace":" at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)\r\n at System.Reflection.RuntimeAssembly.GetExportedTypes()\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n at System.Web.Http.WebHost.WebHostHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)\r\n at System.Web.Http.Dispatcher.HttpControllerTypeCache.InitializeCache()\r\n at System.Lazy`1.CreateValue()\r\n at System.Lazy`1.LazyInitValue()\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache()\r\n at System.Lazy`1.CreateValue()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.SelectController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)"}

To circumvent this error we modified the web.config file and included the following assembly binding:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.8.0" newVersion="4.5.0.0" />
        </dependentAssembly>
   </assemblyBinding>
</runtime>

HTTP Error 500.21 – Internal Server Error

You are seeing this in your browser:

HTTP Error 500.21 - Internal Server Error
Handler "PageHandlerFactory-Integrated" has a bad module "ManagedPipelineHandler" in its module list

It is realistic that your version of asp.net is corrupt or is incomplete. This may have come about from installing a framework after installing IIS.

To reinstall the asp.net framework (here I focus on .NET 4.0 x64) follow these instructions:

Navigate to your framework folder (%windir%\Microsoft.NET\Framework64\v4.0.30319) and run aspnet_regiis.exe -i

Only run post build events if build configuration is Release

We have recently moved over to SmartAssembly from Dotfuscator.  Happy face.  In order to simplify the build & obfuscation process we utilised the post build event.  A post build event will run a SA project.  This will occur for each project in a particular visual studio solution.  The downside of doing this is, regardless of what build configuration you were using at that time, it will always execute.  This considerably lengthened the compile time for debug configuration.  Sad Face

To circumnavigate this we implemented conditional logic in the post build event.  Happy face.  I have included an example of this conditional logic here:

if $(ConfigurationName) == Release (
"C:\Program Files\Red Gate\SmartAssembly 6\smartassembly.com"  "C:\Source\Aspire\SmartAssembly\Aspire.saproj"
)

The incoming message has an unexpected message format ‘Raw’. The expected message formats for the operation are ‘Xml’, ‘Json’.

Wanted to test the POST for a REST API call using fiddler but was getting this reported:

The incoming message has an unexpected message format ‘Raw’. The expected message formats for the operation are ‘Xml’, ‘Json’.

 My request body was xml:

<string xmlns=”http://schemas.microsoft.com/2003/10/Serialization/”>Something to commit</string>

Everything looked correct.  However on further inspection I realised I’d omitted the Content Type header directive.  I subsequently included this in the header:

Content-Type: text/xml;

 and it worked.

 

Invoke-Sqlcmd : Incorrect syntax near the keyword ‘user’.

The SqlServerCmdletSnapin100 Powershell snapin

I have recently become an advocate of PowerShell.  One of the first things I wanted to do was to dip into a database and produce a list of column values from a table.  This is where I hit my first  road block.  Everytime I ran the following command:

Invoke-Sqlcmd -Query “select Username from table” -database “database” -username “sqllogin” -password “password” -serverinstance “(local)\namedinstance

…I got this error:

Invoke-Sqlcmd : Incorrect syntax near the keyword ‘user’.
At line:1 char:14
+ Invoke-Sqlcmd <<<<  -Query “select Username from ********” -database “********” -username “********” -password “********” -serverinstance “(local)\********”
+ CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

It turns out that the table name must be encased in square brackets like so:

Invoke-Sqlcmd -Query “select Username from [table]” -database “database” -username “sqllogin” -password “password” -serverinstance “(local)\namedinstance

This producted a list of usernames.

Crucial M4 SSD Visual Studio Performance comparison

Setting the scene

In recent months I have become interested in the much talked about performance gain from an SSD.  To experience this for myself I acquired one from Crucial.

Leading up to this I had hoped to visually observe the performance difference via an online resource.  I saw a few clips on YouTube but they failed to answer my curiosity.  What I had wanted to see was a comparison of 2 identical environments; one with a SSD and one without.  I was particularly interested in seeing a product that I use regularly loaded ready for use.  I never found such a clip.  So what I’ve done is produce one myself.  You can see this at the end of this blog post.

Please note that this blog is as much about the experience of installing a SSD as it is of the performance gain with one.  And finally, it is important to note, if you’re doing this yourself, that you must get a caddy that fits your second bay if you wish to continue using your HDD.  I’m glad I did as otherwise I would have had to wait.  You’ll see why when you read one.  I sourced one from eBay.

 

Step 1 – Moving content over to SSD

On receipt of the SSD I set about copying all content from HDD over to SSD.  I got the SSD pack that included the cloning software and cable.  I did that first.  It took slightly under 2 hours to complete.

 

Step 2 – Installing SSD

Next I took the back off of the laptop, to uncover the HDD and caddy.  All bar one of the screws that attaches the HDD to the caddy could be unscrewed.  This left me scratching my head for a short while; is there way to specify that the SSD is now the new master?  Google didn’t throw up anything and neither did the documentation.  So this is what I did to get it working…

 

Step 3 – Trial by error

I installed the SSD.  There were no Bios settings to make the SSD in bay 2 as master.  I wasn’t sure what to do so I did I unplugged HDD, booted up, then powered down, plugged HDD back in and powered back up.  Luckily the SSD still remained the master.  This was confirmed by the Disk Management console

 

Conclusion / Recommendation

The hype is justified, get one!

 

And now for the bit you’ve all been waiting for I’m sure…

To view the evidenece plese click here -> Before and After installing a SSD

Detect MS SQL Instances

We use InstallAware (was version 9 R2 but now 10) to package up our solutions for easy installation on customer sites.  It’s a good product but access to their support is poor.  As far as I can see there’s no channel to report an error.  Sufficed to say you are left out in the cold and you have to stumble around in the dark for a work around.  This leads nicely into a work around for a sporadic error we’ve been receiving reports of for some time now.  However, I now have a VM with XP installed and I can get it to fail / error consistently.  So if they were to contact me to progress this issue then I could replicate it on demand.  Any takers?  Worth a try…

 

Ok, enough dangling of the carrot.  The error is :

'' is not a valid integer value

'' is not a valid integer value

Runtime error in setup script
'' is not a valid integer value

This isn’t the greatest of error messages.  Anyhow, it did point me towards where the error was being raised.

It was this line:

Detect MS SQL Instances : get result into variable AVAILABLESERVERS

 

This so happens to be a plugin so there’s not a great deal of help anywhere on the web for this.  So how to get beyond this?

 

As there’s no syntax to handle errors I decided to pass in a command line value to allow for this plugin to be ignored.  So if it was causing an error (remember I said it was sporadic) for our customers I have a workaround to get beyond this issue.  This is the code I used to wrap the plugin in:

 

IA Detect MS SQL Instances workaround

IA Detect MS SQL Instances workaround

 

What this allows for is that if run from the command line with the word ‘IGNORE’ not case sensitive then this plugin will not run.  What you will next be presented with is the IA dialog where you can enter in the servername[+instancename] as opposed to selecting the instance from a dropdown list.   The next button will then lead into executing the T-SQL as designed.

I hope this goes some way towards helping you if / when you come against this little annoyance.

Installing SQL Server on fresh install of an O/S

You will run into many issues when installing a database on a fresh install of sql server on a fresh install of an o/s.  Things to be aware of are (1) the ’sa’ account is disabled by default and (2) the logged in o/s user needs to be a trusted user of SQL Server.  To circumnavigate all these issues I recommend you following the steps listed below.

1. Install sql server and the appropriate SPs and management studio (Management Studio Express 2005 download) .  Here you can set the Named Instance to be in mixed authentication mode.  Also, you’ll have to allow remote access and TCP/IP and named pipes.  Restart the SQL Server services.

2. Add o/s user to SQL Server account groups

3. Run Management Studio as Administrator

4. Enable the ‘sa’ account

Create new query session within Management Studio and execute:

ALTER LOGIN sa ENABLE
GO

5. Set the ‘sa’ password
6. Restart sql service(s)
7. Log into Management Studio as ‘sa’
8. Install DBs
You will not have any problems installing a DB when you have followed the steps above.

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid

I had just built a FakeRepository for a V4 EF Model I was constructing.  The Fake worked but I wanted to make sure I had access to the underlying data source.  When I ran a test against the data source

[TestMethod]
public void Products_Add_Count1()
{
    // arrange
    var prod = MockRepository.GenerateStub();

    //FakeDBEntities fake = new FakeDBEntities();
    AIEntities ctx = new AIEntities();
    AIRepository rep = new AIRepository(ctx);

    // act
    rep.AddProduct(prod);

    // assert
    Assert.IsTrue(fake.Products.Count() == 1);
}

I got this error:

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

As the data model was in a different project to the unit test project the unit test project couldn’t find the connection string.  I copied the App.Config across and I was good to go.

Invalid column name ‘fieldname’ – SQL Server

If you start getting an “Invalid column name ‘fieldname’.” from a database make sure you check the Collation of the database.

You may find that the database collation is Case-Sensitive.

When I was made aware of a problem with an install I found that a function was not created.  This was the result of a fieldname that didn’t match the case at definition.

To demonstrate this…

If I have a table named ‘Users’ with a field named ‘UserID’ the following syntax will fail with a database collation of Turkish_CI_AS:-

select userid from Users

where as this will not:-

select UserID from Users

To change the collation after database creation you can use this syntax:-

ALTER DATABASE database COLLATE Latin1_General_CI_AS