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

<TargetFramework>netstandard2.0</TargetFramework>

And replace with

<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>

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'">
    <DefineConstants>NETFULL</DefineConstants>
  </PropertyGroup>

  <!-- .NET Standard 2.0 references, compilation flags and build options -->
  <PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
    <DefineConstants>TRACE;DEBUG;NETCORE;NETSTANDARD;NETSTANDARD2_0;</DefineConstants>
  </PropertyGroup>

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

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

 

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

#if NETCORE

using Microsoft.AspNetCore.Builder;

#endif

Or you can create methods and functions with Conditional compile

public static class RequestAuditBuilderExtensions

    {

#if NETCORE
        public static IApplicationBuilder UseRequestAuditLogger(this IApplicationBuilder coreApp)

        {

            return coreApp.UseOwin(setup =>

                setup(next =>

                {

                    AppBuilder owinBuilder = new AppBuilder();

                    IApplicationLifetime coreLifeTime =

                          (IApplicationLifetime)coreApp.ApplicationServices

                              .GetService(typeof(IApplicationLifetime));

                    AppProperties owinProp = new AppProperties(owinBuilder.Properties);

                    owinProp.OnAppDisposing = coreLifeTime?.ApplicationStopping ??

                                                        CancellationToken.None;

                    owinProp.DefaultApp = next;

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

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

                    });
                    return owinBuilder.Build<AppFunc>();
                })
            );

        }

 

#endif

#if NETFULL

        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;

        }

#endif

}

 

Until next time,

Happy Coding!