To forcibly specify the DLL assembly to read its corresponding *. dll. config configuration file (also known as: How to Create. Net DCOM) (from: http://blog.csdn.net/shaily/article)

Source: Internet
Author: User

In general,. Net EXE assemly will have a corresponding *. EXE. config configuration file. When you need to read configuration information, you can directly read the key values in *. EXE. config through configurationmanager. etettings [Index], but there are few situations where DLL Assembly requires config file. If the current DLL Assembly name is test. dll, if you call configurationmanager in test. DLL to read test. dll. config, it will fail!

Of course, it is rare to read *. dll. config from DLL assembly. But if you really want to read it, you can use the following method to forcibly define the path of its DLL assembly.

The following code demonstrates how to compile a. Net DLL and publish it as a regasm test. dll/codebase/TLB, publish it as a DCOM, and then call it on the ASP page. The published method helloworld () in test. DLL needs to read the key value in test. dll. config.

The DLL assembly code is as follows:

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). deleettings. settings ["mystring"]. value;
/*
Note: If you use the following traditional method, read the Config Key
String STR = configurationmanager. receivettings ["mystring"];
If this method is used, assembly will not read the *. dll. config file. By default, only the configuration file of EXE assembly will be loaded
*/
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). receivettings. 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

 

The app. config configuration file is as follows (the test. dll. config file is automatically generated after compilation ):

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "mystring" value = "Hello World"/>
</Appsettings>
</Configuration>

After compilation and release, run the following command to register it as DCOM

Regasm test. dll/codebase/TLB

NOTE: If. Net DLL is to be registered as COM, several necessary conditions are required:

1. Set the corresponding attribute of system. runtime. interopservices for DLL, as shown in the following code.

2. In assemblyinfo. CS (in C #), set comvisible to true.

3. When generating DLL assembly, check the register for cominterop check box as follows:

Then, use ASP to call the DCOM as follows:

<%
Set OBJ = Createobject ("anotherdcom. simpleclass ")
Response. Write obj. helloworld ()
%>

Through procmon, we can see that the DLL. config has been read :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.