种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)(转自:http://blog.youkuaiyun.com/shaily/article)...

本文介绍如何在.NET DLL组件中正确读取配置文件(*.dll.config),并通过示例演示了使用C#和VB.NET实现的过程。文章还展示了如何将.NET DLL注册为DCOM组件,并通过ASP页面进行调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一般来说,.net 的exe assemly会存在一个对应的*.exe.config配置文件。当需要读取配置信息的时候,可以直接通过ConfigurationManager.AppSettings[index]来读取*.exe.config中的键值,但很少存在dll assembly需要config file的情况。假如当前dll assembly名为test.dll,如果在test.dll中调用ConfigurationManager来读取test.dll.config,那么是无法成功的!

当然,在dll assembly中要读取其*.dll.config这种需求非常少见。但是确要读取的话,可以采取以下方式,即强行制定其dll assembly的路径。

以下代码演示的是:编写一个.net dll并将其发布为一个regasm test.dll /codebase /tlb将其发布为一个DCOM,然后通过asp页面来调用。其中在test.dll中的公布的方法HelloWorld()需要读取其test.dll.config中的key value.

dll assembly代码如下:

C# version:

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Runtime.InteropServices;
usingSystem.Configuration;
usingSystem.IO;
usingSystem.Reflection;

namespaceAnotherDCOM
{
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
publicinterfaceITestClass
{
stringHelloWorld();
}

[ClassInterface(ClassInterfaceType.AutoDispatch)]
publicclassSimpleClass:ITestClass
{

publicstringHelloWorld()
{
Assemblyassembly;
System.Configuration.Configurationconfiguration;
ExeConfigurationFileMapmap;

Uriuri;
map
=newExeConfigurationFileMap();
assembly
=Assembly.GetCallingAssembly();
uri
=newUri(Path.GetDirectoryName(assembly.CodeBase));
map.ExeConfigFilename
=Path.Combine(uri.LocalPath,assembly.GetName().Name+".dll.config");
stringstr=ConfigurationManager.OpenMappedExeConfiguration(map,0).AppSettings.Settings["MyString"].Value;
/*
说明:如果采取如下的传统方法,读取configkey
stringstr=ConfigurationManager.AppSettings["MyString"];
如果采取这种方式的话,则assembly不会读取相应*.dll.config文件;bydefault,只有exeassembly的配置文件才会被load
*/
if(str!=null)
{
returnstr;
}
else
{
return"HelloWorld.netAnotherDCOM,Can'treadconfig";
}

}
}
}

VB.net version

ImportsSystem
ImportsSystem.Runtime.InteropServices
ImportsSystem.Reflection
ImportsSystem.Configuration
ImportsSystem.IO


NamespaceNetDcom
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)>PublicInterfaceITestClass
FunctionHelloWorld()AsString
EndInterface

<ClassInterface(ClassInterfaceType.AutoDispatch),ProgId("NetDcom.SimpleClass"),ComVisible(True)>PublicClassSimpleClass
ImplementsITestClass
PublicFunctionHelloWorld()AsStringImplementsITestClass.HelloWorld
DimassemblyAsAssembly
DimconfigurationAsConfiguration
DimmapAsExeConfigurationFileMap
DimstrAsString
DimuriAsUri
map
=NewExeConfigurationFileMap
[assembly]
=assembly.GetCallingAssembly
uri
=NewUri(Path.GetDirectoryName([assembly].CodeBase))
map.ExeConfigFilename
=Path.Combine(uri.LocalPath,([assembly].GetName.Name&".dll.config"))
str
=ConfigurationManager.OpenMappedExeConfiguration(map,0).AppSettings.Settings.Item("MyString").Value

IfString.IsNullOrEmpty(str)
<>TrueThen
HelloWorld
=str
Else
HelloWorld
="HelloWorld,.netDCOM(vb.net)"
EndIf


EndFunction

EndClass
EndNamespace


app.config配置文件如下(编译后会自动生成test.dll.config文件):

<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<appSettings>
<addkey="MyString"value="HelloWorld"/>
</appSettings>
</configuration>

编译发布之后,运行以下command将其注册为DCOM

regasm test.dll /codebase /tlb

说明:如果.net dll要注册成为COM的话,需要几个必备的条件:

1. dll要设置相应的System.runtime.InteropServices的相应attribute,如山代码所示。

2. (C#中)assemblyinfo.cs中, comvisible要设置为true。

3. 生成dll assembly的时候,一定要选中 Register for ComInterop复选框,如下所示

然后,通过asp调用该DCOM如下:

<%
setobj=CreateObject("AnotherDCOM.SimpleClass")
Response.writeobj.HelloWorld()
%>

通过procmon我们可以看到,该dll.config被争取读取了:)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值