7

According to this thread, F# Core must be SQLCLR-approved for assemblies to be marked SAFE. Is this planned? Can it even be done?

Daniel
  • 47,404
  • 11
  • 101
  • 179

2 Answers2

1

I believe it can be done. However, F# core library is the sole property of Microsoft. This means you can't modify its code and recompile it to match and comply with SQLCLR SAFE. I suggest you add suggestion to Microsoft using Microsoft's connect website.

Microsoft connect is at: http://connect.microsoft.com (you have to register and have email account on live.com or hotmail.com before register).

To manually add your .NET DLL and integrate it to SQL Server, you can do this:

In this example, the DLL from your F# code has to be compiled first. I take this step from MSDN Library link: http://msdn.microsoft.com/en-us/library/ms254956(v=vs.80).aspx

Just don't forget to add PERMISSION_SET = SAFE to the CREATE ASSEMBLY command.

Here are the steps I quote from above link:

Loading and Running the "Hello World" Stored Procedure in SQL Server

Once the sample procedure has successfully compiled, you can test it in SQL Server. To do this, open SQL Server Management Studio and create a new query, connecting to a suitable test database (for example, the AdventureWorks sample database). We will need to create the assembly so we can access the stored procedure. For this example, we will assume that you have created the helloworld.dll assembly in the C:\ directory. Add the following Transact-SQL statement to your query.

CREATE ASSEMBLY helloworld from 'c:\helloworld.dll' WITH PERMISSION_SET = SAFE

Once the assembly has been created, we can now access our HelloWorld method by using the create procedure statement. We will call our stored procedure "hello":

CREATE PROCEDURE hello
AS
EXTERNAL NAME helloworld.HelloWorldProc.HelloWorld

Once the procedure has been created, it can be run just like a normal stored procedure written in Transact-SQL. Execute the following command:

EXEC hello

This should result in the following output in the SQL Server Management Studio messages window.

Hello world!

Edit: based on the commenter below, he's right about F# is now open source! You can modify and recompile it to suit your needs.

Edit: adding more detail guide on how to integrate the DLL to SQL Server CLR integration.

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
  • 3
    F# core library, like all the compiler, is released with the open-source license Apache 2.0. You can certainly modify and recompile it, you can also distribute it, even in commercial purposes. – Laurent Apr 15 '11 at 09:20
  • 1
    Done - https://connect.microsoft.com/VisualStudio/feedback/details/670494/sqclr-approve-f-core-libraries – Daniel May 25 '11 at 15:53
  • Hi Daniel.. If you think my answer helps you, please mark it as answer to your question. :) – Eriawan Kusumawardhono May 26 '11 at 01:27
  • 1
    @eriawan: The question was "is it planned" and "can it be done," which you didn't quite answer. I was hoping maybe one of the MS employees lurking around SO would chime in. However, your suggestion to post it on MS Connect is helpful, so I up-voted it. – Daniel May 26 '11 at 16:31
  • @Daniel could you read my answer again? I said, I believe it can be done, especially now F# is open source. You can simply build it again from the source. I have done it myself. But as noted in my first paragraph of my answer, it's better to leave the SQLCLR compliance of F# to Microsoft, because they still own it, even though it's open source. – Eriawan Kusumawardhono May 27 '11 at 01:27
  • @eriawan: It doesn't help much that it's open-source since I have no idea what changes are necessary to make it CLR-safe. – Daniel May 27 '11 at 02:19
  • I will add the necessary guid to make it safe, in my updated answer above :) – Eriawan Kusumawardhono May 27 '11 at 03:39
  • @eriawan: Have you tried loading an F# assembly into SQL Server with `PERMISSION_SET=SAFE`? It can't be done. That is the crux of the problem, and the reason for the question. The F# compiler generates "unsafe" constructs, such as static fields, that prevent this. – Daniel May 30 '11 at 04:24
  • No, not loading the F# assembly, but loading the DLL generated by the F# compiler. If you code your F# using the imperative way of F# and compile it, you can mark it as **PERMISSION_SET=SAFE** and load it using the steps above. – Eriawan Kusumawardhono May 30 '11 at 06:39
  • @eriawan - Nay, tis not true. I don't mean the F# core libs, I mean _any_ assembly _written_ in F#. You can't load them into SQL Server as `SAFE`...doesn't matter how you write it (imperatively, or whatever). Besides, how are you going to load an F# assembly into SQL Server without loading the F# core libs also? – Daniel Jun 09 '11 at 21:34
1

A trick, as nasty as it is, would be using the --standalone flag of fsc (aka the F# compiler). This can even be added in the project settings and embeds in the output artifact the F# runtime library (along with all other dependencies deemed embeddable). I believe that at that point you should be able to do exactly what you want just by marking your assembly as SQLCLR safe.

em70
  • 6,088
  • 6
  • 48
  • 80
  • This still doesn't allow the assembly to be marked `SAFE`. All it does is move the unsafe code into your custom assembly. – Daniel Dec 12 '11 at 17:35