Coding adventures with Happy results

Creating Conditional Directives for .Net Standard Projects

Thursday, February 20, 2020 | 6:04:52 PM | C#

When we creating a multi-targeted framework project, it is important to separate framework dependent code using the Conditional Directives.

From the menu select File > New Project, and create a new Class Library .Net Core

You can specify target frameworks by Right-click the class library and press edit.

Enabling multi frameworks

We need to specify the version of .NET that you want our project will target. In our case is Asp .Net Core and Framework 4.6. Right click the class library and press edit.

Once the editor is open, locate the line


And replace with


Creating Conditional Directives

We can define Conditional Directives by adding a <ItemGroup> for the target pulling. This will allow us to conditional compile and conditional reference libraries by setting the target framework. In our case will define two, one for asp .net core and one for full .Net framework. Again we can define directives by Right-clicking the class library and pressing edit.

  <!-- .NET 4.6 references, compilation flags and build options -->
  <PropertyGroup Condition=" '$(TargetFramework)' == 'net461'">

  <!-- .NET Standard 2.0 references, compilation flags and build options -->
  <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">

  <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="Microsoft.AspNetCore.Owin" Version="2.2.0" />

  <ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">


You can now create Conditional Directives on your code to include framework target namespaces


using Microsoft.AspNetCore.Builder;


Or you can create methods and functions with Conditional compile

public static class RequestAuditBuilderExtensions


        public static IApplicationBuilder UseRequestAuditLogger(this IApplicationBuilder coreApp)


            return coreApp.UseOwin(setup =>

                setup(next =>


                    AppBuilder owinBuilder = new AppBuilder();

                    IApplicationLifetime coreLifeTime =



                    AppProperties owinProp = new AppProperties(owinBuilder.Properties);

                    owinProp.OnAppDisposing = coreLifeTime?.ApplicationStopping ??


                    owinProp.DefaultApp = next;

                    IServiceProvider serviceProvider = owinBuilder.InitializeServices();
                    owinBuilder.Use(async (context, _next) =>

                        await serviceProvider.GetRequiredService<RequestAuditMiddleware>().Invoke(context, _next);

                    return owinBuilder.Build<AppFunc>();





        public static IAppBuilder UseRequestAuditLogger(this IAppBuilder app)
            var serviceCollection = InitializeServices(app);
            app.Use(async (context, next) =>


                await serviceCollection.GetRequiredService<RequestAuditMiddleware>().Invoke(context, next);


            return app;





Until next time,

Happy Coding!