Can I store information in a Static Variable in a SQL Server CLR Assembly without using UNSAFE?

ぃ、小莉子 提交于 2019-12-24 11:37:35

问题


I have a natural language processing/sentence tagging routine that has been developed as a sqlserver clr assembly written in c#. The routine requires a model file (developed with training data) about 20MB for the computation and I try to load the model file to a static field when the function is called for the first time. However, it seems PERMISSION_SET=EXTERNAL_ACCESS does not allow write to a static field. Of course having an UNSAFE assembly would address the problem but my DBA doesn't like the idea at all. And it makes no sense to me to load the 20MB model file every time the function is called. Is there anyway to store some information in assembly without using UNSAFE.


回答1:


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

Here is the MSDN page for readonly.

Another option is to modify your code to place that data inside of a Collection, and then mark that Collection as readonly. Once marked as readonly, the Collection 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 the Assembly as SAFE. Please see my answer to another S.O. question for more details:

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



来源:https://stackoverflow.com/questions/28179038/can-i-store-information-in-a-static-variable-in-a-sql-server-clr-assembly-withou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!