简体   繁体   中英

How can I add a value that is stored in web.config to a class attribute in c#(from web config file not the resource file)

public static class trial                 
{               
    public static string LUIS_MODEL_ID=ConfigurationManager.AppSettings["ID"];  
    public static string LUIS_SUBSCRIPTION_KEY =ConfigurationManager.AppSettings["KEY"];

    [LuisModel(LUIS_MODEL_ID, LUIS_SUBSCRIPTION_KEY)]  // An attribute argument must be a constant expression     
    [Serializable]            
    public class DialogLuis : LuisDialog<object>
    {                         

This is the error I'm getting:

"An attribute argument must be a constant expression"

Is there another way to pass a value from the Web.config to the LuisModel attribute?

You can create a base class that will get these values from web.config and instantiate the LuisDialog :

namespace ChatBot_LUIS.Dialogs
{
    [Serializable]
    public class BaseLuisDialog<T> : LuisDialog<T>
    {
        public BaseLuisDialog() : base(GetNewService())
        {

        }

        private static ILuisService[] GetNewService()
        {
            var modelId = ConfigurationManager.AppSettings.Get("LuisModelId");
            var subscriptionKey = ConfigurationManager.AppSettings.Get("LuisSubscriptionKey");
            var staging = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("LuisStaging") ?? "false");
            var luisModel = new LuisModelAttribute(modelId, subscriptionKey, staging: staging);
            return new ILuisService[] { new LuisService(luisModel) };
        }
    }
}

Then in your code you should use this base class for all your Luis Dialogs and remove the LuisModelAttribute altogether. Example:

public class RootLuisDialog : BaseLuisDialog<object>
    {
    ....
    }

You also can inherit from LuisModelAttribute:

[Serializable]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Interface, AllowMultiple = true)]
public class ConfiguredLuisModelAttribute : LuisModelAttribute, ILuisModel
{
    public ConfiguredLuisModelAttribute() : base(
        GetModelId(), 
        GetSubscriptionKey(), 
        LuisApiVersion.V2,
        staging: GetStaging()) { }

    private static string GetModelId()
    {
        return ConfigurationManager.AppSettings.Get("LuisModelId");
    }

    private static string GetSubscriptionKey()
    {
        return ConfigurationManager.AppSettings.Get("LuisSubscriptionKey");
    }

    private static bool GetStaging()
    {            
        return Convert.ToBoolean(ConfigurationManager.AppSettings.Get("LuisStaging") ?? bool.FalseString);
    }

You can try to add your parameters in web.config

<appSettings>
<add key="SitePath" value="http://example.com/" />

after you can get your parameter by

ConfigurationManager.AppSettings["SitePath"].ToString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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