Exception has occurred: CLR/System.TypeInitializationException

175 Views Asked by At

I have a QNA Service on Azure portal and I'm trying to get answers using C# language and used a sample (the code below):

class Program
{
    private const string endpointVar = "";
    private const string endpointKeyVar = "";
    private const string kbIdVar = "";

    private static readonly string endpoint = Environment.GetEnvironmentVariable(endpointVar);

    private static readonly string endpointKey = Environment.GetEnvironmentVariable(endpointKeyVar);
    private static readonly string kbId = Environment.GetEnvironmentVariable(kbIdVar);

    static Program()
    {
        if (null == endpointKey)
        {
            throw new Exception("Please set/export the environment variable: " + endpointKeyVar);
        }
        if (null == endpoint)
        {
            throw new Exception("Please set/export the environment variable: " + endpointVar);
        }
        if (null == kbId)
        {
            throw new Exception("Please set/export the environment variable: " + kbIdVar);
        }
    }

    static void Main(string[] args)
    {
        var uri = endpoint + "/qnamaker/v4.0/knowledgebases/" + kbId + "/generateAnswer";
        string question = @"{'question': 'Is the QnA Maker Service free?','top': 3}";

        using (var client = new HttpClient())
        using (var request = new HttpRequestMessage())
        {
            request.Method = HttpMethod.Post;
            request.RequestUri = new Uri(uri);
            request.Content = new StringContent(question, Encoding.UTF8, "application/json");
            request.Headers.Add("Authorization", "EndpointKey " + endpointKey);

            var response = client.SendAsync(request).Result;
            var jsonResponse = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(jsonResponse);

            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }
    }
}

but I'm getting an error message :

Exception has occurred: CLR/System.TypeInitializationException
An unhandled exception of type 'System.TypeInitializationException' occurred in QNA.dll: 'The type initializer for 'QnAMakerAnswerQuestion.Program' threw an exception.'
 Inner exceptions found, see $exception in variables window for more details.
 Innermost exception     System.Exception : Please set/export the environment variable: 03947ac53fc346adaff6a52e5a4adf25
   at QnAMakerAnswerQuestion.Program..cctor() in D:\Sahara watira\Deploy test\QNA\QNA\Program.cs:line 33

the error message in VS code

0

There are 0 best solutions below