UNSAFE vs EXTERNAL_ACCESS assembly

  • A developer wanted an UNSAFE assembly deployed to production, and, after fiddling with the C# code a bit, I found that a string declared as static in the code could be declared as a constant instead. This was necessary for deploying the assembly with EXTERNAL_ACCESS permission set, instead of UNSAFE.

    I got the following error when I attempted to create the assembly with EXTERNAL_ACCESS perm set, while static variable was in the code:

    Msg 6212, Level 16, State 1, Line 3

    CREATE ASSEMBLY failed because method 'xxxx' on type 'StoredProcedures' in external_access assembly 'xxxx' is storing to a static field. Storing to a static field is not allowed in external_access assemblies.

    Is there a CLR performance cost from changing a string from static to a constant?

    Am I doing the right thing by not allowing the assembly to be deployed with UNSAFE perm set?

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • Hi, Marios. Did you ever get additional information on this? Do you have a sample of the code before and after in c#?

  • Steve-524674 (5/5/2011)


    Hi, Marios. Did you ever get additional information on this? Do you have a sample of the code before and after in c#?

    Hmm, that was a while ago, I'd have a hard time digging up that code.

    Suffice it to say, it is OK to store to a readonly static field if you want to generate an external-access assembly.

    It's when the field is static and variable (ie. able to change values inside the code) that the assembly has to be generated as UNSAFE.

    I will try to get you some more info, links and some code samples.

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • The use of static readonly fields for SAFE/EXTERNAL_ACCESS assemblies is discussed here:

    http://www.devx.com/codemag/Article/34918/1954

    __________________________________________________________________________________
    SQL Server 2016 Columnstore Index Enhancements - System Views for Disk-Based Tables[/url]
    Persisting SQL Server Index-Usage Statistics with MERGE[/url]
    Turbocharge Your Database Maintenance With Service Broker: Part 2[/url]

  • Thanks, Mario. After talking about it with some colleagues, it seems the best solution for deploying CLR would be some way to enable pass-through authentication, though I have not yet found adequate documentation on the topic.

    I will check on the links. Thank you very much for the follow-up.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply