BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News ASP.NET vNext: Custom Project Loaders and Language Support

ASP.NET vNext: Custom Project Loaders and Language Support

Leia em Português

This item in japanese

Bookmarks

The ASP.NET vNext runtime uses the Roslyn compiler to compile and load the C# sources before running them. David Fowler shows how you can leverage the DI-by-design approach of KRuntime to inject support for your own language.

The KRuntime contains all the bits needed to buld and run an ASP.NET vNext application. As a matter of principle, the runtime does not recognise a "design-time". Compilation and loading happen simultaneously. For this, KRuntime has different loaders. These walk through the dependencies listed in the project.json file and build them, along with the sources themselves.

The project reference loader is interesting. By default, it uses the RoslynProjectReferenceProvider which in-turn returns the RoslynProjectReference. As the name suggests, this uses the Roslyn C# compiler to compile the C# sources and load the assemblies.

However, this behavior can be overridden, and David Fowler shows how.

F# Support For ASP.NET vNext

Refer David's vNextLanguageSupport github project. He defines an implementation of IProjectReferenceProvider (FSharpProjectReferenceProvider). This in-turn returns a custom implementation of the IMetadataProjectReference (FSharpProjectReference).

The FSharpProjectReference does the bulk of the work. It's Emit method -

  • goes through the project references
  • copies them to a temp-path
  • compiles the source files along with the references using the fsc compiler, and then
  • deletes the temp files.

Both these classes are just housed in the FSharpSupport project, so that they can be compiled and then referenced as an assembly. Note that even though the implementations are for providing F# support, they are written in C# themselves.

He then uses this in a sample F# project. For this he defines the projectReferenceProviderType in his project.json file -

(snippet)

    "language": {
        "name": "F#",
        "assembly": "FSharpSupport",
        "projectReferenceProviderType": "FSharpSupport.FSharpProjectReferenceProvider"
    },

And just like that, F# is now supported for your ASP.NET vNext app. The startup class is now an F# type instead of a C# class.

This example uses F#, but you can use the same technique to build support for any CLR language. All you have to do is provide your implementations of IMetadataProjectReference and IProjectReferenceProvider.

Aleksander Heintz has a few detailed articles explaining David's sample further -

The example is not trivial. But it demonstrates the power of the inbuilt-DI approach taken by KRuntime, which allows a lot of flexibility for adding new features. As explained in the KRuntime structure, every layer of the runtime, including the Native Process, Host, Managed Entry Point and the Application Host, is resolved via Dependency Injection. (for e.g. KRuntime works on Linux and OSX using Mono).

ASP.NET vNext represents a fundamental change to how Microsoft constructs and deploys web frameworks.

Rate this Article

Adoption
Style

BT