-1
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?

Gp_1993
  • 89
  • 11
  • values in web.config are not constants, hence the error... – Zohar Peled Jan 19 '17 at 10:16
  • 2
    possible duplicate of [http://stackoverflow.com/questions/14999117/asp-net-mvc4-an-attribute-argument-must-be-a-constant-expression-typeof-expre](http://stackoverflow.com/questions/14999117/asp-net-mvc4-an-attribute-argument-must-be-a-constant-expression-typeof-expre) is this link helps you? – kgzdev Jan 19 '17 at 10:19
  • Possible duplicate of [ASP.NET MVC4: An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type](http://stackoverflow.com/questions/14999117/asp-net-mvc4-an-attribute-argument-must-be-a-constant-expression-typeof-expre) – Oscar Jan 19 '17 at 10:20
  • Is there a way to get it from web.config file instead of resx file which is what's said in that link you gave. Thanks :) – Gp_1993 Jan 19 '17 at 10:29
  • @IkramTurgunbaev Is there a way to get the value from web.config itself? – Gp_1993 Jan 19 '17 at 10:44
  • @Gp_1993 as Zohar said web.config values are not constant, so you will need resource file – kgzdev Jan 19 '17 at 10:47
  • Oh.....there is no other way to do this?? Are you sure? – Gp_1993 Jan 19 '17 at 10:49
  • Of course there may be couple of ways, but one of them is resource file – kgzdev Jan 19 '17 at 10:51
  • Once I build, I should be able to edit the values in that attribute LuisModel...I cannot edit the value of the resource file, once build..but I can edit the web.config values.... – Gp_1993 Jan 19 '17 at 11:13
  • Possible duplicate of [Passing model and subscription key to a LuisDialog other than via an attribute](http://stackoverflow.com/questions/39244030/passing-model-and-subscription-key-to-a-luisdialog-other-than-via-an-attribute) – Ezequiel Jadib Jan 19 '17 at 13:01

3 Answers3

2

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>
    {
    ....
    }
SmartDev
  • 2,802
  • 1
  • 17
  • 22
0

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);
    }
martinoss
  • 5,268
  • 2
  • 45
  • 53
-2

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()
ahmed
  • 7
  • 5
  • yeah...This is what I did....But you cant put this in the attribute....thats when you get the error: An attribute argument must be a constant expression..... – Gp_1993 Jan 19 '17 at 10:31
  • What i know when you need to get value of attribute from web.config you can get it by his key not his id – ahmed Jan 19 '17 at 10:44
  • ok...that I know :) ..that is how I have done there as well...in the appsettings tab the key is given as "ID" and "KEY"...but when you put this inside an attribute , this error is shown – Gp_1993 Jan 19 '17 at 10:48
  • You can create a new classe Called ConstanteManager and you can use it namespace myProject.ConstanteManager { public class ConstanteManager { public const string MyConstante = "Value"; – ahmed Jan 19 '17 at 11:05
  • That is nor possible...because again...it is not constant, since its in web.config – Gp_1993 Jan 19 '17 at 11:12