Can I store information in a static variable in a SQL Server CLR assembly without using UNSAFE?

I have a regular text / sentence process that was developed as a sqlserver clr assembly written in C #. A subroutine requires a model file (designed with training data) of about 20 MB for calculation, and I try to load a model file into a static field when the function is called for the first time. However, it seems that PERMISSION_SET = EXTERNAL_ACCESS does not allow writing a static field. Of course, building UNSAFE will solve this problem, but my database administrator doesn't like this idea at all. And it makes no sense for me to upload a 20 MB model file every time a function is called. In any case, to save some information in the assembly without using UNSAFE.

+1
c # sql-server sqlclr
source share
1 answer

Assuming that the model file (i.e. the value of the static variable) does not change after loading, you should set the static variable as readonly , which can be set in the constructor of the same class or after the declaration.

Here is the MSDN page for readonly .

Another option is to modify your code to put this data inside the collection, and then mark this collection as readonly . Once you are marked as readonly , the assembly cannot be reassigned, but you can still use the Add and Remove methods to add elements to it and remove elements from it. This will allow you to mark Assembly as SAFE . Please see my answer to another S.O. Question for more details:

Is it possible to change readonly static variables in C # SQLCLR?

0
source share

All Articles