SQL Server CLR integration with 3rd party SDK: Error on missing assembly System.Drawing

旧街凉风 提交于 2020-01-04 07:35:13

问题


I am developing a solution that will interface with the SDK of a 3rd party application and ultimately create a dataset to migrate data into a destination database on our SQL Server 2014 instance. This interface is via a SQL Server CLR Stored procedure. the following references have been used in creating the CLR assembly on the C# side:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Server;
    using (3rd party SDK);
    using System.Dynamic;

The code builds correctly and all. However, when I attempt to create an assembly using the CREATE ASSEMBLY statement I get the following error:

Assembly 'MyAssembly' references assembly 'system.drawing, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a.', which is not present in the current database. SQL Server attempted to locate and automatically load the referenced assembly from the same location where referring assembly came from, but that operation has failed (reason: 2(The system cannot find the file specified.)). Please load the referenced assembly into the current database and retry your request.

Going further, if I attempt to create an unsafe assembly based on the apparently missing reference, system.drawing (keep in mind I do have trustworthy set to true in the target database), I get the following error:

CREATE ASSEMBLY for assembly 'System.Drawing' failed because assembly 'System.Drawing' failed verification. Check if the referenced assemblies are up-to-date and trusted (for external_access or unsafe) to execute in the database. CLR Verifier error messages if any will follow this message
[ : System.Drawing.Image::Finalize][mdToken=0x600000d][offset 0x00000000] Code size is zero. (etc, etc. same error repeating on different methods in system.drawing)

I already have the .NET frameworks matching between the solution and the target SQL Server database matching to v4.0. I honestly am at a loss here, and this seems soo much harder than it should be. I'm not even directly referencing System.Drawing, yet I need it for some odd reason.


回答1:


While it might not always behave as expected, you should be able to load System.Drawing into SQL Server. You will need to set the Database to TRUSTWORTHY ON (which it seems that you have), and you need to mark the Assembly as UNSAFE in the CREATE ASSEMBLY statement (i.e. use the WITH PERMISSION_SET = UNSAFE clause).

If you still get an error, make sure you are loading the actual System.Drawing DLL and not one of the reference Assemblies, and make sure you grab the correct 32-bit or 64-bit version.

For CLR version 4.0 (which is used by SQL Server 2012 and newer), the assembly path should be one of following two options, depending on whether you need the 32-bit or 64-bit version:

  • C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Drawing.dll

  • C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Drawing.dll


Also, you don't need to worry about the .NET Framework version as long as you are using one that is bound to the proper CLR version, which is 4.0. Meaning, you could just as well be using .NET Framework version 4.5.2 or 4.6, so long as the target server has been updated to that Framework version.



来源:https://stackoverflow.com/questions/34323388/sql-server-clr-integration-with-3rd-party-sdk-error-on-missing-assembly-system

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