C#/.NET 0

Code Contracts in Interfaces are evaluated after Contracts in Class Implementations

I recently noticed an — at least for me — unexpected behaviour when working with Code Contracts in interfaces. When you define a contract for an interface (via ContractClassFor) and implement a class from that interface that contract is certainly enforced. However when you define additional requirements on that implementation class these contracts are evaluated BEFORE the interface contracts. Here […]

C#/.NET 0

C# Code Contracts and Interface Properties

I recently ran into a problem when trying to use Code Contracts on Auto-Implemented Properties (i.e. Automatic Properties) combined with a C# interface. I started with a normal interface IScriptInvokerImpl where I wanted to define a method for running a PowerShell script and a public property for the PowerShell instance being used: A first implementation So a first implementation of […]

C#/.NET 0

ODataQueryOptions do not correctly apply $filter and $orderby Options to IQueryable when using EntityFramework 6 with SQL Server

This blog post is a bit more lengthly than my usual posts and the idea came from an issue I faced when working in one of our project where we use an OData v3 REST interface that talks to an MS SQL Server (2012 R2) via EntityFramework v6.1.3. In short: we wanted to implement ‘entity framework row level security’ (in […]

PowerShell 0

[NoBrainer] ‘The Should command may only be used inside a Describe block’ when run in an interactive PowerShell session

Here is a small helper method that you can use when creating Pester tests and trying to run them in an interactive PowerShell session. When executing statements that include the Pester Should Cmdlet interactively in a PowerShell session you receive the following error message: With the Should helper method below you can still execute the original Pester statement without having […]

C#/.NET 0

[NoBrainer] C# UnitTesting with CodeContracts and Telerik JustMock

When using Microsoft CodeContracts your Contract.Requires and Contract.Ensures statements will throw a ContractException which you cannot catch (per design). This means that you cannot use the [ExceptedException] attribute to assert them. Furthermore when mocking with Telerik Justock you cannot use Contract.Ensures and Contract.Requires at all in your stubs, as the stub is expected to be a Func in the form […]

Various Products 1

Unit Testing with Code Contracts

If your are not already using CodeContracts I really recommend to start using them now. And if you are already using them, you will have noticed that their exceptions cannot easily be caught. This is due to a design decision from the design team as stated in their Code Contracts User Manual: 7.6 ContractException The ContractException type is not a […]

PowerShell 0

[NoBrainer] Performance Considerations regarding using Guid.ToString vs Guid.Guid in PowerShell

While doing some performance optimisations on one our loader scripts I came across an issue of converting a System.Guid or [[Guid]] in PowerShell to a string representation. In C# I was always (and still am) using ToString() and never saw anything like a Guid property. However, for some reason in PowerShell I tended to use the Guid property (instead of […]

PowerShell 0

Transient Fault Handling with PowerShell

We are using PowerShell to automate various backend systems quite heavily. Sometimes these interfaces (which may be REST APIs, message queues or something completely different) are shortly not available because of intermittent failures or simply because of network issues. In any case, most of the time a script will crash, though quite a bunch of work in this script might […]

PowerShell 0

Assert and Exception Logging made easy in PowerShell

In this post I would like to present you a way of how to easily perform assertions like error checking and result validation with PowerShell based on an idea of the Microsoft Code Contracts library. We at d-fens spend quite some time writing PowerShell scripts that automate and integrate various backend systems where unforeseen events not only happen but are […]

Various Products 0

ODataController throws ‘No NavigationLink factory was found’ when returning a different type in IHttpActionResult

Recently I ran into a strange error when working on an ODataController. On several occasions (especially on CREATE or UPDATE operations) we do not want to return the actual underlying entity of the controller, but instead would like to return an entity that can be used for synchronisation such as a Job. So instead of doing something like this and […]

C#/.NET 0

Using CodeContracts with OData Controllers and Web.Api Exception Filters

When writing ODATA controllers for our projects we spend quite some time on writing boilerplate code and input parameter validation. In addition, for our error handling we would like detailed information for internal troubleshooting but not so detailed information sent back to the client (i.e. no stack traces). In order to achieve this without extra work we started looking at […]

LightSwitch 15

Consuming ODATA Services from LightSwitch with Impersonation

In case you are still using LightSwitch and are not entirely relying on its internal features, you might have started using ODATA services as LightSwitch data sources. This comes in especially handy when combined with the Microsoft Managed Extensibility Framework (MEF) which lets you create decoupled components that can be easily plugged into your core application. However, as soon your […]

PowerShell 0

[NoBrainer] Invoke-Pester from your Visual Studio IDE as an External Tool

You might have heard of the recently added support for PowerShell development inside Visual Studio. This extension provides several features, with support for the Pester testing framework being one of them. However, it seems that sometimes the Visual Studio 2013 just does not recognise your test scripts (even when they are named like SomeScript.Tests.ps1). As a quick workaround you can […]

C#/.NET 0

Unit Testing OData Controllers with the Katana TestServer and Service References

Today I would like to quickly present you how to unit test Web.API based OData Controllers the easy way. On biz.dfch.CS.Examples.Odata you find a complete code example and some accompanying information. The idea is based on the following information (and slightly enhanced): * Unit Testing Web API OData Services – Part 1 * Unit Testing Web API OData Services – […]