简体   繁体   English

ASP.NET和VB.NET配置文件

[英]ASP.NET and VB.NET config files

I am trying to share a DLL between an ASP.NET application and a VB.NET (Forms) application. 我试图在ASP.NET应用程序和VB.NET(Forms)应用程序之间共享DLL。 The client application uses a configuration file to store settings eg connection strings and so does the ASP.NET application. 客户端应用程序使用配置文件来存储设置,例如连接字符串,ASP.NET应用程序也是如此。

The ASP.NET application uses the System.Web.Configuration namespace whilst the VB.NET application uses the System.Configuration namespace. ASP.NET应用程序使用System.Web.Configuration命名空间,而VB.NET应用程序使用System.Configuration命名空间。 I notice that they inherit from the Object class. 我注意到它们继承自Object类。 I was expecting them both to inherit from a common class so that I could perhaps use Polymorphism to create an instance of the appropriate type in the DLL. 我期望他们都从公共类继承,以便我可以使用Polymorphism在DLL中创建适当类型的实例。

What is the best practice for using config files for DLLs that are shared across ASP.NET and VB.NET applications. 对于在ASP.NET和VB.NET应用程序中共享的DLL使用配置文件的最佳实践是什么? I have researched this and I read somewhere that you should use System.Configuration in the ASP.NET application and I read somewhere else that you should not do this because you cannot write to the web.config if you use System.Configuration in the ASP.NET application. 我已经研究了这个,我在某个地方读过你应该在ASP.NET应用程序中使用System.Configuration而我在其他地方读到你不应该这样做,因为如果你在ASP中使用System.Configuration你就不能写入web.config .NET应用程序。 I do not need to write to the web.config; 我不需要写入web.config; I need to read it. 我需要阅读它。 What is the best practice? 什么是最佳做法?

Update 13/07/2012 The DLL contains one class. 更新13/07/2012 DLL包含一个类。 The class currently has one constructor, which looks like this: 该类目前有一个构造函数,如下所示:

private connectionString As Sub
Public Sub New()
connectionString = WebConfigurationManager.ConnectionStrings("TestConnection").ConnectionString
End Sub

I am now thinking about creating another constructor in the class for the none web clients to call ie : 我现在正考虑在类中为无Web客户端调用创建另一个构造函数,即:

private connectionString As Sub
Public Sub New()
connectionString = connectionString2
End Sub

我建议从宿主应用程序传递你的DLL函数的参数,并从dll中完全删除配置文件依赖项。

What has worked well for us in this case is we put entries in using: 在这种情况下,对我们有用的是我们使用以下条目:

<configuration>
  <appSettings>
    <add key="AccessServer" value="\\apollo\all_access_anon\northwind.mdb" />
    ...

And then we access it using: 然后我们使用以下方式访问它:

NameValueCollection appSettings = ConfigurationManager.AppSettings;
string str = appSettings["AccessServer"];

That is common to both config files. 这对于两个配置文件都很常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM