views:

1098

answers:

3

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.

A: 

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

Rob Cowell
A: 

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.

Brad Bruce
+2  A: 

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
veggerby
When you write "add the DownloadChunkSize setting to Project 2", do you mean inside an app.config or a web.config file (or something else?)?
John Galt
Using the Project Properties > Settings tab/form
veggerby
Very nice. I see now what you mean. I've eliminated the app.config from this class library project and now I see there is a Settings.settings file under the Properties folder. Thank you very much.
John Galt