如何检索程序集的目录
为了调用 Web 服务,我在 C# .NET 2.0 中创建了一个 DLL。
在 SQL Server 2008 R2 中注册 DLL 后,我尝试在该 DLL 中检索其目录以加载配置文件。
问题是我在Google上找到的所有方法都不起作用,这是一个问题,因为我无法加载文件。我不想在代码中添加一些“硬路径”,因为它可能会根据环境而变化。
这是我在 SQL Server 2008 中注册 dll 的方法:
DROP FUNCTION [Tools].[CallWriteDepot]
GO
DROP FUNCTION [Tools].[CallExtractDepot]
GO
drop assembly SqlAssembly
go
create assembly SqlAssembly
from 'C:\webservices\dll\SqlAssembly.dll'
with permission_set = unsafe
go
---------------------------------------------------------------------------------------------------
CREATE FUNCTION [Tools].[CallWriteDepot](@xmlInputFile [nvarchar](max))
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlAssembly].[SqlAssembly.Main].[writeToSafe]
GO
---------------------------------------------------------------------------------------------------
CREATE FUNCTION [Tools].[CallExtractDepot](@xmlInputFile [nvarchar](max))
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlAssembly].[SqlAssembly.Main].[readFromSafe]
GO
如您所见,DLL 位于 C:\webservices\dll\SqlAssembly.dll 中,并且不可能在 DLL 代码中找到此路径,如下所示。
经过几次测试,结果如下:
Assembly.GetAssembly(typeof(SqlAssembly.Main)).CodeBase
Result : empty
attrTest2.Value = Assembly.GetAssembly(typeof(SqlAssembly.Main)).Location
Result : empty
Assembly.GetCallingAssembly().Location
Result : C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn\SqlAccess.dll
Assembly.GetCallingAssembly().GetName().CodeBase
Result : file:///C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/Binn/SqlAccess.DLL
Assembly.GetExecutingAssembly().Location
Result : empty
Assembly.GetExecutingAssembly().GetName().CodeBase
Result : empty
Directory.GetCurrentDirectory();
Result : C:\Windows\system32
AppDomain.CurrentDomain.BaseDirectory;
Result : C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn\
Environment.CurrentDirectory;
Result : C:\Windows\system32
有谁知道SQL Server如何注册DLL函数吗?就像它将函数保存在内存区域中并且不再读取dll文件一样。
如果有人也有解决方案;)
In order to call a webservice, I've created a DLL in C# .NET 2.0 .
After registering the DLL in SQL Server 2008 R2, I tried, in this DLL, to retreive its directory in order to load a configuration file.
The problem is all methods I found on Google doesn't work, and it's a problem because I can't load the file. I don't want put some "hard paths" in my code, because it can change depending of the environment.
There is how I register my dll in SQL Server 2008 :
DROP FUNCTION [Tools].[CallWriteDepot]
GO
DROP FUNCTION [Tools].[CallExtractDepot]
GO
drop assembly SqlAssembly
go
create assembly SqlAssembly
from 'C:\webservices\dll\SqlAssembly.dll'
with permission_set = unsafe
go
---------------------------------------------------------------------------------------------------
CREATE FUNCTION [Tools].[CallWriteDepot](@xmlInputFile [nvarchar](max))
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlAssembly].[SqlAssembly.Main].[writeToSafe]
GO
---------------------------------------------------------------------------------------------------
CREATE FUNCTION [Tools].[CallExtractDepot](@xmlInputFile [nvarchar](max))
RETURNS [nvarchar](max) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [SqlAssembly].[SqlAssembly.Main].[readFromSafe]
GO
As you see, the DLL is in C:\webservices\dll\SqlAssembly.dll and it is impossible to find this path in the DLL code, as you can see below.
There is the result after few tests :
Assembly.GetAssembly(typeof(SqlAssembly.Main)).CodeBase
Result : empty
attrTest2.Value = Assembly.GetAssembly(typeof(SqlAssembly.Main)).Location
Result : empty
Assembly.GetCallingAssembly().Location
Result : C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn\SqlAccess.dll
Assembly.GetCallingAssembly().GetName().CodeBase
Result : file:///C:/Program Files/Microsoft SQL Server/MSSQL10_50.MSSQLSERVER/MSSQL/Binn/SqlAccess.DLL
Assembly.GetExecutingAssembly().Location
Result : empty
Assembly.GetExecutingAssembly().GetName().CodeBase
Result : empty
Directory.GetCurrentDirectory();
Result : C:\Windows\system32
AppDomain.CurrentDomain.BaseDirectory;
Result : C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Binn\
Environment.CurrentDirectory;
Result : C:\Windows\system32
If anybody knows how SQL Server register a DLL function ? It's like it save the function in a memory zone and don't read the dll file anymore.
If anybody have the solution too ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 SQL Server 中创建程序集时,DLL 的二进制代码将加载到 SQL Server 中。您可以在 sys. assembly_files 中找到它。当您在程序集中执行函数时,不会使用用于创建程序集的 dll。
解决您的问题的一个可能的解决方案可能是拥有一个包含配置文件路径的
.ini
文件。只需确保将.ini
文件放在可以通过path
环境变量找到的位置即可。When you create an assembly in SQL Server the binary code of the dll is loaded into SQL Server. You can find it in
sys.assembly_files
. The dll used to create the assembly is not used when you execute functions in the assembly.A possible solution to your problem could be to have an
.ini
file that contains the path to the configuration file. Just make sure you put your.ini
file in a place you can find it through thepath
environment variable.