当 web.config 不可用时如何解决对 applicationSettings 的引用

发布于 2024-07-30 09:04:35 字数 1153 浏览 2 评论 0原文

我正在尝试获取 .ASMX Web 服务中的代码并将其更改为类库项目。 Web 服务项目有一个 web.config 文件,其中包含:

<applicationSettings>
    <myService.Properties.Settings>
        <setting name="DownloadChunkSize" serializeAs="String">
            <value>100000</value>
        </setting>

..FileService.asmx.cs 文件中的代码使用该文件来检索值:

int chunkSize = (int)Properties.Settings.Default.DownloadChunkSize;

当我尝试重新构建此代码以消除 .asmx Web 服务时,我在我的类库项目中出现编译时错误,因为不再有可用的 web.config。 让我尝试更多地解释一下我到目前为止所做的事情(以及原因):

这样做的动机是为了简化我的项目。 也就是说,我有一个 Vstudio 解决方案,其中包含一个可以运行的 .asmx 项目。 它有一些方法,并封装了与供应商提供的另一个 .asmx Web 服务的通信。

我尝试的新设计如下:

项目 1 是一个名为 ProxyASMX 的类库项目,其中包含对供应商 Web 服务的 Web 引用。 我在这里没有提供任何代码; 它只是有一个指向供应商 Web 服务的小 app.config。

项目 2 是一个类库项目,引用了 ProxyASMX.dll。 此 FileService.cs 文件中的代码与我原来的 Web 服务相同,但 [webmethod] 属性已被删除。 这是我在编译时遇到问题的项目。

项目 3 是一个 Web 应用程序项目,具有 2 个 HTTPHandler - Upload.ashx 和 Download.ashx。 该项目将引用项目 2 类库。 我尝试用原始解决方案中更全面的 web.config 内容替换小的默认 web.config 内容,但当这不起作用时,我想我最好咨询有关此项目的专家。

我希望上面的草图是清楚的。 我认为这应该是非常可行的,但现在我不太确定。

I am trying to take the code in an .ASMX web service and change it into a class library project. The web service project has a web.config file with:

<applicationSettings>
    <myService.Properties.Settings>
        <setting name="DownloadChunkSize" serializeAs="String">
            <value>100000</value>
        </setting>

..and the code in the FileService.asmx.cs file uses this to retrieve the value:

int chunkSize = (int)Properties.Settings.Default.DownloadChunkSize;

When I try to re-architect this code to eliminate the .asmx web service, I get a compile time error in my class library project because there is no longer a web.config available. Let me try to explain a bit more about what I've done so far (and why):

The motivation for this is to simplify my projects. That is, I have a Vstudio solution with an .asmx project that works. It has a few methods and encapsulates the communication with another .asmx web service supplied by a vendor.

The new design I am attempting is as follows:

Project 1 is a class library project called ProxyASMX with a web reference to the vendor web service. I've provided no code here; it simply has a small app.config pointing at the vendor web service.

Project 2 is a class library project with a reference to the ProxyASMX.dll. The code in this FileService.cs file is the same as my original web service but the [webmethod] attributes have been removed. This is the project I'm having trouble compiling.

Project 3 is a web application project with 2 HTTPHandlers - Upload.ashx and Download.ashx. This project will have a reference to the Project 2 class library. I tried replacing the small default web.config content with the more comprehensive web.config content from the original solution but when that did not work I thought I better consult the experts about this venture.

I hope the above sketch is clear. I THINK this should be very do-able but now I am not so sure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有你我更好 2024-08-06 09:04:35

您可以将应用程序设置添加到类库。 因此,您在“使用它们”的项目上添加应用程序设置。 然后,您只需将这些配置部分复制到“项目堆栈上”即可。

因此,如果我理解正确,您需要将 DownloadChunkSize 设置添加到项目 2 中,并将此部分复制到项目 3 中的 web.config 中。这将类似于:

<applicationSettings>
    <Project2.Properties.Settings>
        <setting name="DownloadChunkSize" serializeAs="String">
            <value>100000</value>
        </setting>

您可以使用以下方式在项目 3 中访问此设置:

Project2.Properties.Settings.Default.DownloadChunckSize

在项目 2 中:

Properties.Settings.Default.DownloadChunckSize

You can add Application Settings to a class library. So you add the Application Settings on the project that "consumes them". Then you simply copy these config sections "up the project stack".

So if I understand you correct, you need to add the DownloadChunkSize setting to Project 2 and copy this section to your web.config in Project 3. This will be something like:

<applicationSettings>
    <Project2.Properties.Settings>
        <setting name="DownloadChunkSize" serializeAs="String">
            <value>100000</value>
        </setting>

You can access this setting inside Project 3 using:

Project2.Properties.Settings.Default.DownloadChunckSize

And in Project 2:

Properties.Settings.Default.DownloadChunckSize
jJeQQOZ5 2024-08-06 09:04:35

我认为您需要创建一个 ExeConfigurationFileMap 并使用它可以从项目 1 的 App.config 中检索值。

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

        fileMap.ExeConfigFilename = @"ConfigTest.exe.config";  // relative path names possible

        // Open another config file 

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);



        // read/write from it as usual

        ConfigurationSection mySection = config.GetSection("mySection");

        config.SectionGroups.Clear(); // make changes to it 

        config.Save(ConfigurationSaveMode.Full);  // Save changes

对于此类内容,企业库中有一个很好的帮助器 - Tom Hollander 已经做了很好的 文章

I think you need to create an ExeConfigurationFileMap and use it to retrieve values from your App.config from Project 1.

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

        fileMap.ExeConfigFilename = @"ConfigTest.exe.config";  // relative path names possible

        // Open another config file 

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);



        // read/write from it as usual

        ConfigurationSection mySection = config.GetSection("mySection");

        config.SectionGroups.Clear(); // make changes to it 

        config.Save(ConfigurationSaveMode.Full);  // Save changes

There's a nice helper from the Enterprise Library for this kind of stuff - Tom Hollander has done a good writeup of it

泼猴你往哪里跑 2024-08-06 09:04:35

如果可能的话,我倾向于避免使用库的配置文件。 我会将其作为您从库中公开的类的属性,并让应用程序确定设置的存储方式和位置。

如果有多个应用程序重复使用库,我更有可能考虑库级配置文件(例如 NLog)。 但在大多数情况下,来自应用程序的设置更有意义。

I tend to avoid config files for libraries if possible. I would make this a property of the class you are exposing from the library, and let the application determine how and where the setting is stored.

If there are multiple applications re-using a library I am more likely consider a library level config file (NLog for example). In most cases though, the setting makes more sense coming from the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文