种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)(转自:http://blog.csdn.net/shaily/article)
来源:互联网 发布:数据库的日志已满 编辑:程序博客网 时间:2024/06/05 01:04
一般来说,.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:
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Configuration;
using System.IO;
using System.Reflection;
namespace AnotherDCOM
{
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITestClass
{
string HelloWorld();
}
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class SimpleClass:ITestClass
{
public string HelloWorld()
{
Assembly assembly;
System.Configuration.Configuration configuration;
ExeConfigurationFileMap map;
Uri uri;
map = new ExeConfigurationFileMap();
assembly = Assembly.GetCallingAssembly();
uri = new Uri(Path.GetDirectoryName(assembly.CodeBase));
map.ExeConfigFilename = Path.Combine(uri.LocalPath, assembly.GetName().Name + ".dll.config");
string str=ConfigurationManager.OpenMappedExeConfiguration(map, 0).AppSettings.Settings["MyString"].Value;
/*
说明:如果采取如下的传统方法,读取config key
string str = ConfigurationManager.AppSettings["MyString"];
如果采取这种方式的话, 则assembly不会读取相应*.dll.config文件;by default,只有exe assembly的配置文件才会被load
*/
if (str != null)
{
return str;
}
else
{
return "Hello World .net Another DCOM, Can't read config";
}
}
}
}
VB.net version
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Configuration
Imports System.IO
Namespace NetDcom
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> Public Interface ITestClass
Function HelloWorld() As String
End Interface
<ClassInterface(ClassInterfaceType.AutoDispatch), ProgId("NetDcom.SimpleClass"), ComVisible(True)> Public Class SimpleClass
Implements ITestClass
Public Function HelloWorld() As String Implements ITestClass.HelloWorld
Dim assembly As Assembly
Dim configuration As Configuration
Dim map As ExeConfigurationFileMap
Dim str As String
Dim uri As Uri
map = New ExeConfigurationFileMap
[assembly] = assembly.GetCallingAssembly
uri = New Uri(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
If String.IsNullOrEmpty(str) <> True Then
HelloWorld = str
Else
HelloWorld = "Hello World, .net DCOM(vb.net)"
End If
End Function
End Class
End Namespace
app.config配置文件如下(编译后会自动生成test.dll.config文件):
<configuration>
<appSettings>
<add key="MyString" value="Hello World"/>
</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如下:
set obj=CreateObject("AnotherDCOM.SimpleClass")
Response.write obj.HelloWorld()
%>
通过procmon我们可以看到,该dll.config被争取读取了:)
- 种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)(转自:http://blog.csdn.net/shaily/article)
- 种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
- 一种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)
- ASP.net读取Web.xml的配置信息(转自:http://blog.csdn.net/donhao/article/details/6678755)
- (转)动态链接到MFC的DLL 转自http://blog.csdn.net/rivershan/
- 程序运行提示msvcp100d.dll和msvcp100r.dll缺少的解决方案 http://blog.csdn.net/ztz0223/article/details/8886247
- 如何用c#读写配置文件(转自:http://blog.csdn.net/lanman/article/details/5287717)
- 转自:http://blog.csdn.net/limingchuan123456789/article/details/11499665 jquery判断checked的三种方法: .att
- fastjson的使用(转自:http://blog.csdn.net/wx_962464/article/details/37612861)
- 查看混淆后的日志(转自http://blog.csdn.net/fwt336/article/details/51896009)
- Java的XML创建、解析文档(转载自http://blog.csdn.net/psyuhen/article/details/7539228)
- libxml读取xml文件的其他方法 http://blog.csdn.net/hdutigerkin/article/details/7547014
- ubuntu如何修改默认的文件关联打开程序 转自:http://blog.csdn.net/blueboy82006/article/details/5305497
- 开发人员一定要加入收藏夹的网站(转自http://blog.csdn.net/songkexin/article/details/4828055)
- selenium使用中遇到的问题,转自:http://blog.csdn.net/funi16/article/details/9036753
- Android Canvas 和Paint的用法 转自http://blog.csdn.net/u010947098/article/details/44574171
- dependencies与dependencyManagement的区(转自:http://blog.csdn.net/liutengteng130/article/details/46991829)
- C++的内存管理(转载自:http://blog.csdn.net/bizhu12/article/details/6668834)
- oracle commit的时候发生了什么
- Quartz的cron表达式
- 软件外包相关职业规划
- 程序员杂记:找工作的经历
- 中小软件企业人力资源困境破局
- 种强行指定dll assembly读取其相应*.dll.config配置文件的方法(又名:如何创建.net 的DCOM)(转自:http://blog.csdn.net/shaily/article)
- Linux下面如何的proc文件--------编写Linux资源管理器
- 通过AJAX与ASP.NET结合实现的仿GridView增删改查功能!
- IPhone开源代码 绝对经典!
- 室内GPS定位初露峥嵘
- ASP.NET通过IHttpModule实现伪静态
- 《XNA游戏开发》简介
- C/C++中static解析
- 我的第一个博客