在ASP.NET用户范围设置 [英] User-scoped settings in ASP.NET

查看:122
本文介绍了在ASP.NET用户范围设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗口(WPF)和Web(ASP.NET)上下文中使用一个类库。库具有设置是相当用户范围,如果用在窗户上下文,并且必须应用程序作用域,如果用在Web上下文。我想声明为用户范围,并使用默认值伪应用程序范围。我的问题是,Web应用程序禁止用户范围设置通过抛出配置错误。

I have a class library used in windows (WPF) and web (ASP.NET) context. The library has settings that are rather user-scoped, if used in windows context and that have to be application-scoped, if used in web context. I would like to declare them as user-scoped and use the default values as "pseudo-application-scoped". My problem is, that web applications ban user-scoped settings by throwing a configuration error.

什么是框架结构体系内pferably处理这个问题,$ P $的最好方法是什么?

What is the best way to handle this, preferably within the frameworks configuration system?

推荐答案

好吧,面对不可避免的。有ASP.NET中没有用户范围设置。在混合客户端/服务器环境中使用的类库不应该使用它们!

Ok, facing the unavoidable. There are no user-scoped settings in ASP.NET. Class libraries used in mixed client / server environments shouldn’t use them!

下面是解决方案,我用结束:

Here is the solution I ended with:

有关在服务器环境中的使用(ASP.NET)类库的所有设置都必须应用程序范围。

For the usage in a server environment (ASP.NET) all settings of class libraries have to be application-scoped.

我介绍的第二组设置在客户环境中的使用(WPF)。这些设置有属性名称和类型相同的类库属性名和类型,但是它们可以在不同的范围和它们存在于我的主组件上。

I introduce a second set of settings for the usage in client environments (WPF). These settings have property names and types identical to the class libraries property names and types, but they can differ in scope and they are located in my main assembly.

通过一小块code口扩展库设置类注入的用户范围设置。

With a small piece of code I extend the libraries setting class to inject the user-scoped settings.

namespace ClassLibrary.Properties 
{
    using System.Configuration;

    public sealed partial class Settings {
        private ApplicationSettingsBase injectedSettings;

        public void InjectSettings(ApplicationSettingsBase settings)
        {
            injectedSettings = settings;
        }

        public override object  this[string propertyName]
        {
            get 
            { 
                if (injectedSettings != null)
                    return injectedSettings[propertyName];

                 return base[propertyName];
            }
            set 
            { 
                base[propertyName] = value;
            }
        }
    }
}

通过这个扩展我可以在第二设置类注入到库设置:

With this extension I can inject the second settings class into the library settings:

ClassLibrary.Properties.Default.InjectSettings(Application.Properties.Settings.Default);

这篇关于在ASP.NET用户范围设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆