dotnet / dotnet/fsharp

Make FSharp.Core safe for SQL CLR with IL initonly (somehow) on static field 'empty' in Map

Open
#3,535 13 comments 1 reaction 0 assignees View on GitHub
Feature Request
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

I want to use F# to create CLR functions for SQL Server but I cannot publish them automatically because FSharp.Core is not "safe" as far as SQL Server is concerned.

Specifically, I get the error message:

> CREATE ASSEMBLY failed because type 'Microsoft.FSharp.Collections.FSharpMap`2' in safe assembly 'fsCLR' has a static field 'empty'. Attributes of static fields in safe assemblies must be marked readonly in Visual C#, ReadOnly in Visual Basic, or initonly in Visual C++ and intermediate language.

Of course, it may be that some other resolution to 'empty' is needed other than the one suggested above, but it's the first message I've seen stating _why_ FSharp.Core isn't safe, but I don't have the chops to address it without the worry of mucking up something else.

#### Details

A Visual Studio (2017) solution with two projects: 1) an F# library, 2) a SQL Server Database Project.

A simple library could be:
```
module fsCLR
open System.Data.SqlTypes
[]
let FsFunc (ss: SqlString) : SqlString =
if ss.IsNull then ss else "from F#: " + ss.Value |> SqlString
```

The database project can be empty, just add a reference the library. Building/Publishing the DB project will do the work of creating the deploy SQL for an assembly binary and making the function(s) available in T-SQL.

Try to publish the DB project to a SQL Server; fails with the above message.

Contrariwise, the same DB project with a C# library auto-published works fine.

#### Note
Strictly speaking, the 'direct translation' of a working C# library would look like this:
```
namespace UserDefinedFunctions
open System.Data.SqlTypes
type fsCLR () =
[]
static member FsFunc (ss: SqlString) : SqlString =
if ss.IsNull then ss else "from F#: " + ss.Value |> SqlString
```
but doesn't make a difference.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.