Enum in switch statement always returns first case

68 views Asked by At

I have simple enumeration and switch statement like below:

    public enum AuthType
    {
        Auth,
        Token
    }

    public T GetRecord<T>(string queryString, AuthType authType){
        switch (authType)
        {
            case AuthType.Auth:
                var response = doSomethingAuth(queryString)
                return response 
            case AuthType.Token:
                var response = doSomethingToken(queryString)
                return response;
            default:
                throw new InvalidOperationException("AuthType does not exist");
        }
    
    }

var querystring = "&id=12345"
var addressList = _repository.GetRecord<List<Address>>(querystring, AuthType.Token)

When I run my code locally and the authType value is AuthType.Token, then it falls to the correct case.

When I deploy the code to my Azure App Service the switch statement always falls to the first case.

The deployed code is on a very old S1 instance of an Azure App Service plan with runtime set to .Net Framework 4.8.

My local machine is running up to date Windows 10. The application runtime has been updated a couple of times is currently targeted to .Net 4.8.

This is a classic "works on my machine" moment and I am at a loss to explain.

1

There are 1 answers

1
Aslesha Kantamsetti On BEST ANSWER

I created web forms in the .NET Framework 4.8 with simple enumeration and switch statements, and they were successfully deployed to Azure App Services.

I used the following code to create this project.

Repository.cs:

using System;
public class Repository
{
    public T GetRecord<T>(string queryString, AuthType authType)
    {
        switch (authType)
        {
            case AuthType.Auth:
                var authResponse = DoSomethingAuth(queryString);
                return (T)Convert.ChangeType(authResponse, typeof(T));
            case AuthType.Token:
                var tokenResponse = DoSomethingToken(queryString);
                return (T)Convert.ChangeType(tokenResponse, typeof(T));
            default:
                throw new InvalidOperationException("Invalid AuthType provided.");
        }
    }
    private string DoSomethingAuth(string queryString)
    {
        return "Auth Response";
    }

    private string DoSomethingToken(string queryString)
    {
        return "Token Response";
    }
}

Default.aspx.cs:

using System;
namespace WebApplication44
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InputPanel.Visible = true;
                ResultPanel.Visible = false;
            }
        }
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            var queryString = "&id=12345";
            var repository = new Repository();
              AuthType authType;
            if (Enum.TryParse(UserInputDropDownList.SelectedValue, out authType))
            {
                try
                {
                    var result = repository.GetRecord<string>(queryString, authType);
                    ResultLabel.Text = $"Result for {authType}: {result}";                
                    InputPanel.Visible = false;
                    ResultPanel.Visible = true;
                }
                catch (InvalidOperationException ex)
                {
                    ResultLabel.Text = ex.Message;
                }
            }
            else
            {
                ResultLabel.Text = "Invalid AuthType provided.";
            }
        }
    }
}

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication44.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Panel ID="InputPanel" runat="server" Visible="true">
                <asp:Label ID="InputLabel" runat="server" Text="Select AuthType:"></asp:Label>
                <asp:DropDownList ID="UserInputDropDownList" runat="server">
                    <asp:ListItem Text="Auth" Value="Auth"></asp:ListItem>
                    <asp:ListItem Text="Token" Value="Token"></asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" />
            </asp:Panel>
            <asp:Panel ID="ResultPanel" runat="server" Visible="false">
                <asp:Label ID="ResultLabel" runat="server" Text=""></asp:Label>
            </asp:Panel>
        </div>
    </form>
</body>
</html>

Local Output:

enter image description here

enter image description here

I deployed the web app to Azure using Visual Studio.

Right-click on the project name and select Publish as shown below.

enter image description here

enter image description here

enter image description here

If you have already created a web app in Azure, select the existing one or create a new one.

enter image description here

I successfully published the app to Azure, as shown below.

enter image description here

Azure App Service Output:

enter image description here

enter image description here