This is work in progress
I read about the .NET Generic Host for the first time in a Tweet of David Fowler. And I was hooked by the idea, it was just what I was looking for.
I like modules making things small but fit together like pieces of a puzzle, if possible being able to reuse them. I started refactoring Greenshot over a year ago, and was making modules out of the spaghetti code that it was. Trying to extract the modules into their own nuget packages, making them more generic and testable. I glued them together with some code I wrote, which is availble in Dapplo.Addons, but I knew that this was just a journey, until I found something that is a better with more potential.
I found the following description: The purpose of Generic Host is to enable a wider array of host scenarios. Messaging, background tasks, and other non-HTTP workloads based on Generic Host benefit from cross-cutting capabilities, such as configuration, dependency injection (DI), and logging.
I wasn't directly sure about the scenarios one would use it for, but than after seeing a couple of examples I changed my view. When thinking the implementation details away, and trying just to see the functionality, I could see similarities with Spring Boot, which is one of my favorite Java projects. The site for Spring-Boot says: Spring Boot makes it easy to create stand-alone, production-grade Applications that you can "just run".
Spring-Boot, in my words, makes it possible to write an Enterprise grade Java application which uses plugable services & functionality for which normally one would use a J2EE application server like JBoss, Tomcat etc.
Don't get me wrong, the implementation is VERY different, but it should be possible to produce similar products. This article is about how we can use the generic host to make a modular application, similar to using Spring-Boot.
I will try to describe the similarities and some of the the gaps, between both solutions and hopefully come up with some solutions for those gaps.
Let's see how the two implementations compare on a functional level, only covering a subset of the possibilities:
Spring projects | Description | .NET implementation |
---|---|---|
Spring Boot | Easy to create stand-alone, production-grade Applications that you can "just run". | The HostBuilder for the "generic host" |
Spring Framework | Provides a comprehensive programming and configuration model for modern Java-based enterprise applications. Core technologies: dependency injection, events, resources, i18n, validation, data binding, type conversion, SpEL, AOP. Testing: mock objects, TestContext framework, Spring MVC Test, WebTestClient. Data Access: transactions, DAO support, JDBC, ORM, Marshalling XML. Spring MVC and Spring WebFlux web frameworks. Integration: remoting, JMS, JCA, JMX, email, tasks, scheduling, cache. Languages: Kotlin, Groovy, dynamic languages. | Build in IoC, Loggin etc of the "generic host" |
Spring Data | Provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. The list of Spring-Data projects is substantial: Spring Data JDBC Spring Data JDBC Extensions Spring Data JPA Spring Data LDAP Spring Data MongoDB Spring Data Redis Spring Data R2DBC Spring Data REST Spring Data for Apache Cassandra Spring Data for Apache Geode Spring Data for Apache Solr Spring Data for Pivotal GemFire Spring Data Couchbase Spring Data Elasticsearch Spring Data Envers Spring Data Neo4J Spring for Apache Hadoop |
Entity Framework Core |
Spring Cloud | Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). The sub-projects for Spring-Cloud are Spring Cloud Stream Spring Cloud Azure Spring Cloud for Amazon Web Services Spring Cloud Bus Spring Cloud CLI Spring Cloud for Cloud Foundry Spring Cloud - Cloud Foundry Service Broker Spring Cloud Cluster Spring Cloud Commons Spring Cloud Config Spring Cloud Connectors Spring Cloud Consul Spring Cloud Contract Spring Cloud Function Spring Cloud Gateway Spring Cloud GCP Spring Cloud Netflix Spring Cloud Open Service Broker Spring Cloud Pipelines Spring Cloud Security Spring Cloud Sleuth Spring Cloud Stream App Starters Spring Cloud Task Spring Cloud Task App Starters Spring Cloud Vault Spring Cloud Zookeeper Spring Cloud App Broker Spring Cloud Kubernetes Spring Cloud OpenFeign Spring Cloud Data Flow |
Spring Cloud Netflix contains Eureka, which is covered by [Steeltoe's Service Discover](https://steeltoe.io/docs/steeltoe-discovery/ and Circuit Breaker (Hystrix) which is similar to Polly) |
Spring Cloud Data Flow | Microservice based Streaming and Batch data processing for Cloud Foundry and Kubernetes. | |
Spring-Security | is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications. | ASP.NET core security |
Spring Session | Provides an API and implementations for managing a user’s session information. | |
Spring Integration | Extends the Spring programming model to support the well-known Enterprise Integration Patterns. | |
Spring HATEOAS | Spring HATEOAS provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC. | |
Spring REST Docs | Helps you to document RESTful services. | HTTP Repl |
Spring Batch | A lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily operations of enterprise systems. | |
Spring AMQP | The Spring AMQP project applies core Spring concepts to the development of AMQP-based messaging solutions. | RabbitMQ dotnet |
Spring for Android | is an extension of the Spring Framework that aims to simplify the development of native Android applications. | Xamarin |
Spring Cloud Skipper | Skipper is a tool that allows you to discover Spring Boot applications and manage their lifecycle on multiple Cloud Platforms. | |
Spring CredHub | Spring CredHub provides client-side support for storing, retrieving, and deleting credentials from a CredHub server running in a Cloud Foundry platform. | Steeltoe |
Spring Flo | Is a JavaScript library that offers a basic embeddable HTML5 visual builder for pipelines and simple graphs. This library is used as the basis of the stream builder in Spring Cloud Data Flow. | |
Spring for Apache Kafka | applies core Spring concepts to the development of Kafka-based messaging solutions. | Confluent Kafka dotnet |
Spring LDAP | Is a library to simplify LDAP programming in Java, built on the same principles as Spring Jdbc. | |
Spring Mobile | is an extension to Spring MVC that aims to simplify the development of mobile web applications. | Xamarin |
Spring Roo | A next-generation rapid application development tool for Java developers. With Roo you can easily build full Java applications in minutes. | |
Spring Shell | provides an interactive shell that allows you to plugin your own custom commands using a Spring based programming model. | |
Spring Statemachine | is a framework for application developers to use state machine concepts with Spring applications. | Stateless |
Spring Test HtmlUnit | Provides integration between Spring MVC Test Framework and HtmlUnit. | |
Spring Vault | provides familiar Spring abstractions and client-side support for accessing, storing and revoking secrets. | |
Spring Web Flow | allows implementing the "flows" of a web application. | |
Spring Web Services | is a product of the Spring community focused on creating document-driven Web services. |
I've extracted some of the Spring-Framework core functionality, to get into more details:
Functionality | Spring (-Boot) | Generic Host |
---|---|---|
Dependency Injection | Spring-Framework | Microsoft.Extensions.DependencyInjection Docs: DI |
web framework | Spring-MVC | ASP.NET Core in the Microsoft.AspNetCore namespace, e.g. MVC |
Configuration | Spring-Cloud-Config | configuration ( Microsoft.Extensions.Configuration and Microsoft.Extensions.Options |
Security | Spring-Security | ASP.NET core security |
Composition / Add-on | (Component scanning](https://springframework.guru/spring-component-scan/) | ??? (see further) |
Here are the Microsoft.Extensions I needed, and I created:
- Plugin or composition support, in Spring-Boot this is called component scan.
- WPF / WindowsForms lifetime support
- Caliburn.Micro bootstrapper
I'm first writing a prototype: With the Spring-Framework it's very common to have component scanning, this detects additional components and will wire these into your application. For the generic host I have not yet found such a thing, this is a functionality I really need for Greenshot! I will try to write a prototype for this in my GenericHostPlayground (Current status - Prototype is working, look at the repo issues for currently know issues)
A list of currently available packages on nuget can be found here which already were 465 at the time of writing this. Unfortunately these are currently mainly logging and dependency injection adaptors.
Here are a few blog posts I found:
- Generic Host Builder in ASP .NET Core
- The Background Tasks Based On Generic Host In .NET Core
- Microservice-based Application with ASP.NET Core Generic Host
- The ASP.NET Core Generic Host: namespace clashes and extension methods
- Running a .NET Core Generic Host App as a Windows Service
- Using HostBuilder and the Generic Host in .NET Core Microservices