惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 聂微东
MyScale Blog
MyScale Blog
H
Help Net Security
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
M
MIT News - Artificial intelligence
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
P
Proofpoint News Feed
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏

博客园 - 飞天舞者

微服务学习之旅 - 构建一个helloworld的Micro service. Windows Azure上的大数据服务: HDInsight的介绍 介绍Windows Azure HDInsight服务的Hadoop Storm的视频 Windows Azure Service Bus Topics实现系统松散耦合 Windows Azure Service Bus Notification Hub推送通知 使用Microsoft Azure云平台中的Service Bus 中继 Intanet环境下的WCF服务。 SharePoint 2013的REST编程基础 什么是SharePoint? Windows的SEH机理简要介绍 如何调试由于heap corruption导致的程序崩溃的简单示例 利用定制行为扩展WCF之-利用MessageInsepctor behaviourExtension扩展WCF行为(自定义消息头) (转载)如何在WCF实现impersonnate客户端的功能 关于COM的Reg-Free(免注册)技术简介及实例讲解。 COM中的error handling机制及示例(ISupportEfforInfo,ICreateErrorInfo,IErrorInfo) 如何实现DCOM或者COM+的远程调用 转载推荐:COM中不同字符类型相互转换,例如char*, BSTR, CString等等 [软件调试学习笔记]防止栈缓冲区溢出的基于Cookie的安全检查机制 [软件调试学习笔记]WinDbg演示IA-32 CPU下的Windows 分页机制下的地址转换过程 Debug入门之旅-StackoverFlow exception的调试
一种强行指定dll assembly读取其相应*.dll.config配置文件的...
飞天舞者 · 2009-04-24 · via 博客园 - 飞天舞者

      一般来说,.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;
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
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文件):

<?xml version="1.0" encoding="utf-8" ?>
<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被争取读取了:)