Blog: .NET

Use T4 TextTemplatingFilePreprocessor in .NET Standard or PCL libraries

April 25, 2017, Software Development

For my projects NJsonSchema and NSwag I wanted to use T4 templates to programmatically generate code generating code (TextTemplatingFilePreprocessor). Adding the .tt file with its generated C# class works, even if the files are added to a PCL (Portable Class Library) or .NET Standard 1.0 project. The problem is, that the produced code does not compile because the used PCL Profile 259 (same as .NET Standard 1.0) is missing some required classes and reflection methods. To fix the compiler errors, you have to provide the missing types and methods in your assembly. To avoid name collisions with potential client assemblies […] Read more...

Tags: , , , ,

Implement custom MSBuild tasks and distribute them via NuGet

January 31, 2017, (updated on February 2, 2017), Software Development

In this article describes how to implement MSBuild tasks with inline C# or in an external .NET assembly. After doing so, we will bundle these tasks in a NuGet package, so that they can easily be distributed and updated via NuGet and executed as part of the MSBuild compilation process. This is a powerfull technique to share and manage MSBuild tasks (e.g. build scripts, code generators, etc.) in bigger environments (enterprises, open-source libraries, common/shared tasks). Write simple task with inline C# First we implement an MSBuild task with inline C# code. For the purpose of this article, a simple “Hello […] Read more...

Tags: , , ,

NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project

September 30, 2016, (updated on October 1, 2016), Software Development

This tutorial shows how to integrate NSwag (Swagger toolchain for .NET) so that you can access your Web API actions with the Swagger UI and a generated C# client library. The toolchain can be integrated into the following three types of Web API projects: Global.asax-based Web projects (full .NET Framework) OWIN-based Web projects (full .NET Framework) ASP.NET Core based projects (full .NET Framework or .NET Core) For each project, the integration is a little bit different, but this tutorial will provide links to all supported ways. Just follow the steps. 1. Create and setup a new Web API project First […] Read more...

Tags: , , , , , , ,

Inside Async/Await: Synchronize an async method or code block

November 5, 2015, (updated on November 10, 2015), Software Development

Have you ever tried to await a task inside a lock() block? It is not possible, because you can only synchronize synchronous code with the lock keyword. However, in today’s .NET development, async/await is used everywhere and the need to synchronize asynchronous code blocks is coming up quite often. This is why I wrote a simple class which can be used to synchronize asynchronous code blocks. As an example for this article, we have a simple synchronous method which is synchronized using a lock() block: private object _lock = new object(); public void Run(int i) { lock (_lock) { Console.WriteLine("Before: […] Read more...

Tags: , , , , ,

Avoid wrongly scoped injections with a custom Ninject kernel

October 22, 2015, (updated on February 19, 2016), Software Development

I’m currently working on a large project where different teams implement assemblies which are consumed by other teams. We use Ninject for dependency injection: Each assembly has a Ninject module class which registers the available interface-to-implemenation bindings. At the moment, the consumers of these assemblies just know what module classes to load and what interfaces to inject into own objects like ASP.NET controllers or own service objects, but nothing about the scope of the injected objects. Recently we had some nasty runtime errors, because a consumer of one of these assemblies injected a per-request scoped object into a singleton-scoped object. […] Read more...

Tags: , , , ,

Getting started with Entity Framework Code First Migrations

October 12, 2015, (updated on October 17, 2015), Software Development

The following post describes the steps to setup and use Entity Framework Code First Migrations. For more information read this Microsoft page. The sample application shown in this article can be found on GitHub. Initial setup Listed below are the steps to setup a new project with an Entity Framework context, an entity class and an initial migration: Create a new data access project (C# library project) in your solution Open the Package Manager Console in the Visual Studio menu Tools > NuGet Package Manager > Package Manager Console. In the console window, select the previously created data access project […] Read more...

Tags: , , , , , ,

Avoiding DLL file locks when using .NET reflection in external assemblies

September 6, 2015, (updated on January 7, 2016), Software Development

For one of my projects, I had to load external assemblies and query the exported types (i.e. the public classes) using reflection. Everything worked fine, except that the .DLL files were being locked until the application was terminated. The problem is, that once you load an assembly into the main AppDomain using Assembly.LoadFrom(), it stays loaded and you cannot unload the assembly. Also, loading an assembly into the AppDomain where your other code is running might have unexpected side effects. To avoid all these problems, we need to do the work in a secondary, isolated AppDomain. An AppDomain is like […] Read more...

Tags: , , , , ,

Precompile ASP.NET MVC Razor views in Release mode to find errors at compile time

A problem we often face when developing ASP.NET MVC applications is that errors in Razor views are only detected at runtime while browsing the site. This is because the Razor views are not compiled until they are requested by the browser. To avoid this problem, you can prebuild the views by adding the MvcBuildViews tag to your .csproj file: <?xml version="1.0" encoding="utf-8"?> <Project ...> ... <PropertyGroup> ... <MvcBuildViews>true</MvcBuildViews> ... There are two problems with this approach: The build process takes much longer You cannot change the views when debugging the running application To avoid these problems and still be able […] Read more...

Tags: , , , ,

Post-process the HTML output of an action in ASP.NET MVC

May 5, 2015, (updated on December 16, 2015), 2 comments, Software Development

This article shows how to implement a post-processor for an ASP.NET MVC application, which transforms the output of an controller action before it is transmitted to the client. The obvious way to do this is by implementing a custom action filter. First, we implement the base filter class: public abstract class OutputProcessorActionFilterAttribute : ActionFilterAttribute { protected abstract string Process(string data); public override void OnResultExecuted(ResultExecutedContext filterContext) { var response = filterContext.HttpContext.Response; response.Filter = new OutputProcessorStream(response.Filter, Process); } } As you can see in the OnResultExecuted method, the response’s current Filter property is wrapped by a new stream implementation. Using this interceptor […] Read more...

Tags: , , , ,

My most used Visual Studio and ReSharper extensions

March 12, 2015, (updated on August 25, 2015), 5 comments, Software Development

This article describes some of the Visual Studio and ReSharper extensions and development tools I’m using to enhance my daily C#/.NET development productivity. ReSharper extensions In my opinion, ReSharper is a must have extension for Visual Studio which provides lots of improvements, for example better refactorings, an improved code snippet and template engine, additional static code analysis and much more. My personal configuration: Use camel humps instead of whole word when navigating or deleting: ReSharper > Options… > Environment > Editor > Editor behavior > Use CamelHumps Copy XML documentation from interface when generating interface methods: ReSharper > Options… > […] Read more...

Tags: , , , , , ,


  Page 1 of 3 Next Page »