Can a class library be shared between .NET Core 2.0 and SQL Server CLR?

Viewed 2154

I would like to share libraries between .NET Core 2.0 applications and SQL Server CLR Procedures. I've tried putting the shared logic in the following project types:

.NET Standard Library

A .NET Standard Library can be used from a .NET Core Project, and I can add a reference from the SQL Server Project, however, the Classes cannot be referenced in the CLR Procedure: enter image description here

Portable Class Library

A Portable Class Library can be referenced from the CLR Procedure. There is an option for a PCL to target ASP.NET Core 1.0, but I'm not able to find a way to target .NET Core 2.0. When I reference this library from a .NET Core 2.0 project, I get the following compilation error:

Project PortableLibrary1 is not compatible with netcoreapp2.0 (.NETCoreApp,Version=v2.0). Project PortableLibrary1 supports: dotnet5.0 (.NETPlatform,Version=v5.0)

Are there any other options to share a library between .NET Core 2.0 and SQL Server CLR?

1 Answers

I came to a similar issue.

Context

One SQL project (with CLR C# code), I wanted to migrate some logic inside of a shared .NET standard project to be able to have one logic for my CLR and for my .NET Core API.

Solution

I created one .NET standard project (with two targets: NET452 and CORE1.6 (for your case 2.0):

<TargetFrameworks>netstandard1.6;net452</TargetFrameworks>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<OutputType>Library</OutputType>

I wasn't able to use System.Data.SqlTypes from this project, so I created a new project NET452 to put my specific implementation that was used by my CLR.

I can reference from my SQL project the new project NET452.

At this point, I had problem to reference my .NET standard project into my SQL project.

Right click on .NET standard project, in package your are able to produce a nuget package from your .NET standard library.

This nuget package will produce a specific dll NET452, this one can be referenced inside of the SQL project.

On the deployment on the database, you should add the assembly manually.

DevOps

What I'm doing in my DevOps process:

  1. on Build from a release branch, patch the assemblies to match the release number x.x.x

  2. publish nuget generated from the build to an internal nuget server

  3. on Release process, deploy nuget package in a folder on the SQL server

  4. Run script to drop and create assembly:

     CREATE ASSEMBLY [Library] 
     FROM 'D:\Library\Nuget.Library.1.0.0\lib\net452\Library.dll'  
     WITH PERMISSION_SET = SAFE; 
    

And everything was working fine for me.

Shared logic between my .NET Core library and my CLR

The nuget step is annoying, I'm waiting Microsoft makes the SQL project supporting Nuget package https://github.com/NuGet/Home/issues/545

Related