how to share settings between app <-> class lib?

R

Rainer Queck

Hello NG,

Is it possible to share the settings of an application with a class
libreary?
In my case I have a application and a set of different reports (home made)
put into a class library.
The plan is to delivere different report.dlls with the main app.

But it is essentially importent, that the reports and the app use the same
settings.
How can I achieve this?

Thanks for help and hints.

Rainer Queck
 
R

Rainer Queck

Hi Sloan,

thanks for the hint, but can't I just "pass on" my applications
Properties.Settings somehow?

Regards
Rainer
 
W

Walter Wang [MSFT]

Hi Rainer,

You will have two options:

Option A: Use SettingsGroupNameAttribute to specify a section name in your
class library, so that it could use the same settings section in your
application config (which is also used by your application):

1) In your class library, create the settings as usual; you could then use
strong-typed Settings.Default.<Key> to access those settings.

2) Create another separate class:

namespace ClassLibrary1.Properties
{
[SettingsGroupName("ConsoleApplication1.Properties.Settings")]
internal sealed partial class Settings :
global::System.Configuration.ApplicationSettingsBase
{
}
}

(You need to add reference to System.Configuration.dll)

Please note this "ConsoleApplication1.Properties.Settings" can be modified,
just remember to use the settings section name in the application config
file (see below).

Also, the namespace should be same to the designer generated
Settings.Design.cs.

3) In your application, also use the settings designer to create those
settings (or simply copy over the content from app.config). Open the
app.config, you will see the

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ConsoleApplication1.Properties.Settings>
<setting name="Key1" serializeAs="String">
<value>ConsoleApplication1</value>
</setting>
</ConsoleApplication1.Properties.Settings>
</applicationSettings>

If you changed the section name "ConsoleApplication1.Properties.Settings",
make sure you also modify them here.

4) Now reference the class library from the application, the class library
now will use the application's settings as well.


Option B: use "configSource" attribute of config section to move both the
class library and the application's settings into a separate settings file:

1) Create the settings in the class library as usual, add some settings;
then open app.config, copy the settings section
"ClassLibrary1.Properties.Settings" and save it into a separate file, for
example "SharedSettings.xml":

<ClassLibrary1.Properties.Settings>
<setting name="Key1" serializeAs="String">
<value>ClassLibrary1</value>
</setting>
</ClassLibrary1.Properties.Settings>

Modify the app.config to remove the content of the settings section and add
"configSource" attribute to point it to the external file
"SharedSettings.xml":

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ClassLibrary1.Properties.Settings
configSource="SharedSettings.xml">
</ClassLibrary1.Properties.Settings>
</applicationSettings>

2) In your application's config file, also point its settings section to
use this file:

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication2.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ConsoleApplication2.Properties.Settings
configSource="SharedSettings.xml">
</ConsoleApplication2.Properties.Settings>
</applicationSettings>

3) Copy the SharedSettings.xml from the class library to your application
directory, make sure you set its "Copy to output directory" property to
"Copy always" in its properties window.

4) Now both your class library and your application are using the separate
settings xml file "SharedSettings.xml".



Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rainer Queck

HI Walter,

thank you very much for the detailed answer.
It enlarged my horizont on the subject.
But since I am a very lazzy person I decided to choose option C:

I defined a interface for all needed settings and implemented it into the
classes evolved plus a little class reading my settings in the application.

Interface :
public interface IReportSettingsInterface
{
string PdfReader { get; set; }
string PdfReaderArgs { get; set; }
string ReportPath { get; set; }
string SamplePath { get; set; }
string TemplatePathe { get; set; }
}

Class constructor in library:
public GbReport(IReportSettingsInterface settings)
{
reportSettings = settings;
...
}

Settings reader class in application:
public class ReportSettings : IReportSettingsInterface
{
private string pdfReader;
private string pdfReaderArgs;
private string reportPath;
private string samplePath;
private string templatePathe;

public ReportSettings()
{
pdfReader = Properties.Settings.Default.PdfReader;
pdfReaderArgs = Properties.Settings.Default.PdfReaderArgs;
reportPath = Properties.Settings.Default.ReportPath;
samplePath = Properties.Settings.Default.SamplePath;
templatePathe = Properties.Settings.Default.TemplatePathe;
}
....
<properties set get>
}

Class factory in my app, instantiating the classes in my library:
....
GbReport report = Activator.CreateInstance(reportClassType, new Object[] {
new ReportSettings()}) as GbReport;
....

Works nice in my app ;-)

Regards
Rainer


Walter Wang said:
Hi Rainer,

You will have two options:

Option A: Use SettingsGroupNameAttribute to specify a section name in your
class library, so that it could use the same settings section in your
application config (which is also used by your application):

1) In your class library, create the settings as usual; you could then use
strong-typed Settings.Default.<Key> to access those settings.

2) Create another separate class:

namespace ClassLibrary1.Properties
{
[SettingsGroupName("ConsoleApplication1.Properties.Settings")]
internal sealed partial class Settings :
global::System.Configuration.ApplicationSettingsBase
{
}
}

(You need to add reference to System.Configuration.dll)

Please note this "ConsoleApplication1.Properties.Settings" can be
modified,
just remember to use the settings section name in the application config
file (see below).

Also, the namespace should be same to the designer generated
Settings.Design.cs.

3) In your application, also use the settings designer to create those
settings (or simply copy over the content from app.config). Open the
app.config, you will see the

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ConsoleApplication1.Properties.Settings>
<setting name="Key1" serializeAs="String">
<value>ConsoleApplication1</value>
</setting>
</ConsoleApplication1.Properties.Settings>
</applicationSettings>

If you changed the section name "ConsoleApplication1.Properties.Settings",
make sure you also modify them here.

4) Now reference the class library from the application, the class library
now will use the application's settings as well.


Option B: use "configSource" attribute of config section to move both the
class library and the application's settings into a separate settings
file:

1) Create the settings in the class library as usual, add some settings;
then open app.config, copy the settings section
"ClassLibrary1.Properties.Settings" and save it into a separate file, for
example "SharedSettings.xml":

<ClassLibrary1.Properties.Settings>
<setting name="Key1" serializeAs="String">
<value>ClassLibrary1</value>
</setting>
</ClassLibrary1.Properties.Settings>

Modify the app.config to remove the content of the settings section and
add
"configSource" attribute to point it to the external file
"SharedSettings.xml":

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary1.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ClassLibrary1.Properties.Settings
configSource="SharedSettings.xml">
</ClassLibrary1.Properties.Settings>
</applicationSettings>

2) In your application's config file, also point its settings section to
use this file:

<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ConsoleApplication2.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<ConsoleApplication2.Properties.Settings
configSource="SharedSettings.xml">
</ConsoleApplication2.Properties.Settings>
</applicationSettings>

3) Copy the SharedSettings.xml from the class library to your application
directory, make sure you set its "Copy to output directory" property to
"Copy always" in its properties window.

4) Now both your class library and your application are using the separate
settings xml file "SharedSettings.xml".



Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
W

Walter Wang [MSFT]

Hi Rainer,

Thank you for sharing your experience here. I'm glad you've solved the
issue.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top