Class project configuration file app. config information after packaging and Installation Problems

Source: Internet
Author: User

A word plug-in is implemented in a class library project. The configuration information is stored in the configuration file app of the class library project. in config, use the following method to obtain the doctype node information in the configuration file after packaging. the value obtained by deleetpipeline ["doctype"] is always unavailable. The value found during tracing debugging is null. After the class library is applied to the Internet, configurationmanager is displayed. appsettings accesses the application configuration file instead of the configuration file used by the class library. Therefore, you only need to change the policy and set the app. the config file is also packaged into the installation file. After installation, find the file in the installation path and read the relevant information. The specific implementation method is as follows:
(C # code in. NET environment)

// Obtain the File Installation path
Assembly sampleassembly = assembly. getexecutingassembly ();
String filepath = path. getdirectoryname (sampleassembly. codebase. substring (8) + "// app. config ";
// Parse the configuration file to obtain the corresponding value
Xmldocument xdoc = new xmldocument ();
Xdoc. Load (filepath );
String doctype = xdoc. selectsinglenode (@ "/configuration/etettings/Add [@ key = 'doctype']"). attributes ["value"]. value;

Doctype is the value to be obtained.

 

A Method for forcibly specifying DLL assembly to read the corresponding *. dll. config configuration file (also known as how to create. Net DCOM)

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.