Blog

Advanced Newtonsoft.Json: Dynamically rename or ignore properties without changing the serialized class

November 2, 2017, Software Development

This article describes how to implement a custom contract resolver, so that you can ignore or rename serialized JSON properties where the serialization happens and without changing the serialized classes. I use this resolver in my project NSwag, a Swagger/Open API toolchain: A Swagger specification uses JSON Schemas to describe types, but it is not exactly the same model – some properties of a schema are not allowed and some must be renamed. Because I cannot change the JSON Schema implementation, I had to ignore or rename properties in the Swagger serializer. Another use case of this contract resolver is, […] Read more...

Tags: , , ,

NSwag Tutorial: Implement a custom operation processor to define ReDoc code samples

November 1, 2017, (updated on November 2, 2017), Software Development

With NSwag you can implement custom operation processors and apply them to ASP.NET Core MVC or Web API controller operations. These processors then get picked up by NSwag and are applied to the given operation in the Swagger specification. This article shows how to implement a custom operation processor using the example of adding code samples to the ReDoc UI. The ReDoc UI processes the x-code-samples property on operations. Here is a sample Swagger specification: { ... "paths": { "/person": { "post": { "summary": "Adds a new person.", "operationId": "addPerson", "x-code-samples": { "lang": "CSharp", "source": "console.log('Hello World');" } ... } […] Read more...

Tags: , , , ,

How to instantiate a generic type in TypeScript

August 1, 2017, Software Development

In C# we can define a generic type argument and instantiate this type at runtime: public class Factory { public T Create<T>() where T: new() { return new T(); } } var factory = new Factory(); var person = factory.Create<Person>(); But how would you write such a method in TypeScript? The main problem is, that the generic type argument T is not available at runtime because TypeScript generics are only used at compile time for type checking and removed in the transpiled JavaScript code. The best solution I came up with, is to pass the generic type as method argument […] Read more...

Tags:

Use T4 TextTemplatingFilePreprocessor in .NET Standard or PCL libraries

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: , , , ,

Aurelia JS: Detect dirty checked properties during development

March 29, 2017, (updated on October 6, 2017), 2 comments, Software Development

I am currently working on new single-page web application based on the Aurelia framework. To avoid performance problems, we want to completely avoid dirty checked properties and method calls. Bindings are dirty checked whenever the framework cannot use another mechanismn than regularly polling for changes. It is important to detect dirty checked properties as early as possible so that they can direcly be avoided and not too many depenencies have to be changed. I found a simple solution: Just intercept the dirty check subscription method and print the name of the dirty checked property name to the console. In your […] 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: , , ,

Aurelia JS: Implement a dialog service which renders Bootstrap UI dialogs

December 19, 2016, Software Development

I’m currently working on an SPA (single-page application) which uses the Aurelia JavaScript client framework and Bootstrap UI CSS framework. In the application I need to show various custom dialogs. This is why I implemented a reusable dialog service class which simplifies the creation of Bootstrap based dialogs. In this article I’d like to share and explain the code of this dialog service and the required helper classes. The sample code is written in TypeScript but it can easily be converted to ES6 JavaScript. Implement the dialog service and base classes First we need to implement the dialog service – […] Read more...

Tags: , , ,

.NET Core: Missing mscorlib after installing a Portable Class Library (PCL) or .NET Standard library

October 3, 2016, (updated on October 4, 2016), Software Development

.NET Core projects can reference Portable Class Libraries if one of the .NET Standard compliant profiles is used (e.g. the profile 259). However, after installing a PCL NuGet package in a .NET Core projects, you may end up with the following compiler error: The type '???' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. To fix this, just install the following PCL compatibility NuGet in your project: Microsoft.NETCore.Portable.Compatibility The package provides bindings for the missing “mscorlib” so that the project can be compiled without problems. Read more...

Tags: , , ,

NSwag Tutorial: Generate an Angular TypeScript client from an existing ASP.NET Web API web assembly

October 1, 2016, (updated on July 6, 2017), 11 comments, Software Development

This tutorial shows how to generate an Angular TypeScript client with the NSwag Swagger toolchain. You can use this client in your Angular 2 SPA (single-page application) to consume the web services of an existing Web API project. The metadata for the client generator will be loaded from an existing .NET assembly which contains your Web API controller classes. The NSwag project provides many other TypeScript templates (for targeting other JS frameworks) and also a C# client generator. Introduction video: For ASP.NET Core projects, I recommend to use the middleware to generate the Swagger spec (there are some problems loading […] 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: , , , , , , ,


  Page 1 of 6 Next Page »