Build error CS1031

Aashishkebab

Member
Joined
Apr 10, 2023
Messages
12
Programming Experience
5-10
I have a very large ASP.NET application. Recently we decided to migrate from local hosting to Azure.

However, I am getting compiler errors that I'm not getting locally.

C#:
/// <summary>
/// Returns either <paramref name="potentiallyNullValue"/> or the minimum value allowed by its <paramref name="type"/> if it's null.
/// </summary>
/// <param name="potentiallyNullValue"></param>
/// <param name="type"></param>
/// <returns></returns>
[Pure]
public static object GetValueOrFloor(this object potentiallyNullValue, Type type)
{
    if(type is null)
    {
        throw new ArgumentNullException("type", "The supplied type must exist (ie cannot be null, which it is in this case).");
    }
    else if(potentiallyNullValue != null && !type.Equals(potentiallyNullValue.GetType()))
    {
        throw new ArgumentException("The supplied type must match the type of object this method was called upon.");
    }

    if(type.Equals(typeof(DateTime)) || type.Equals(typeof(DateTime?)))
    {
        return potentiallyNullValue ?? DateTime.MinValue;
    }
    else if(type.Equals(typeof(int)) || type.Equals(typeof(int?)))
    {
        return potentiallyNullValue ?? int.MinValue;
    }
    else if(type.Equals(typeof(uint)) || type.Equals(typeof(uint?)))
    {
        return potentiallyNullValue ?? uint.MinValue;
    }
    else if(type.Equals(typeof(short)) || type.Equals(typeof(short?)))
    {
        return potentiallyNullValue ?? short.MinValue;
    }
    else if(type.Equals(typeof(ushort)) || type.Equals(typeof(ushort?)))
    {
        return potentiallyNullValue ?? ushort.MinValue;
    }
    else if(type.Equals(typeof(long)) || type.Equals(typeof(long?)))
    {
        return potentiallyNullValue ?? ushort.MinValue;
    }
    else if(type.Equals(typeof(ulong)) || type.Equals(typeof(ulong?)))
    {
        return potentiallyNullValue ?? ulong.MinValue;
    }
    else if(type.Equals(typeof(sbyte)) || type.Equals(typeof(sbyte?)))
    {
        return potentiallyNullValue ?? sbyte.MinValue;
    }
    else if(type.Equals(typeof(byte)) || type.Equals(typeof(byte?)))
    {
        return potentiallyNullValue ?? byte.MinValue;
    }
    else if(type.Equals(typeof(float)) || type.Equals(typeof(float?)))
    {
        return potentiallyNullValue ?? float.MinValue;
    }
    else if(type.Equals(typeof(double)) || type.Equals(typeof(double?)))
    {
        return potentiallyNullValue ?? double.MinValue;
    }
    else if(type.Equals(typeof(decimal)) || type.Equals(typeof(decimal?)))
    {
        return potentiallyNullValue ?? decimal.MinValue;
    }
    else if(type.Equals(typeof(char)) || type.Equals(typeof(char?)))
    {
        return potentiallyNullValue ?? char.MinValue;
    }
    else if(type.Equals(typeof(string)))
    {
        return string.IsNullOrWhiteSpace((string)potentiallyNullValue) ? string.Empty : potentiallyNullValue;
    }
    else
    {
        throw new ArgumentException("Type is not one with a recongnized minimum value.");
    }
}

Specifically, the first if statement on line 10 gets error CS1031, "Type expected, A type parameter is expected."
This error does not occur locally, only when I try to deploy on Azure (which is set to ASP.NET 4.8). What is going on? What needs to change in the code to fix this? Is Azure using a different version of C#?
 
Shouldn't the condition be type == null rather than type is null ?
 
I don't know. The only Azure deployments I have done involve code that I pre-compile locally and push the binaries. No .CS files.
 
This error does not occur locally

And you really are building it with the exact same setup that azure does? Something must be different (is your azure instance linux?) because .net fiddle on 4.7.2 gives the same error:


But on .netcore+ it's fine


This code is using pattern matching of the form "if(x is 2 or 3)" but your azure compiler is treating it as the older type checking "if(x is String)"

Just change it to `== null` and move on with life - it's what the newer pattern match form is converted to anyway:

1681285254803.png


Top is what is written by the dev, bottom is result of a compile decompile cycle
 
Last edited:
And you really are building it with the exact same setup that azure does? Something must be different (is your azure instance linux?) because .net fiddle on 4.7.2 gives the same error:


But on .netcore+ it's fine


This code is using pattern matching of the form "if(x is 2 or 3)" but your azure compiler is treating it as the older type checking "if(x is String)"

Just change it to `== null` and move on with life - it's what the newer pattern match form is converted to anyway:

View attachment 2727

Top is what is written by the dev, bottom is result of a compile decompile cycle

Hello,

I appreciate your response. Since that method wasn't being used anyway, I simply deleted it.

However, now I'm having an issue with this one, which makes even less sense:

C#:
/// <summary>
/// Parses a <see cref="string"/> as a <see cref="Nullable{T}"/> <see cref="bool"/>.
/// If conversion fails, returns <c>null</c>.
/// </summary>
/// <param name="string"></param>
/// <returns></returns>
[Pure]
public static bool? ToNullableBoolean(this string @string)
{
    return bool.TryParse(@string, out bool @bool) ? @bool : (bool?)null;
}

For that I get the following error: error CS1525: Invalid expression term 'bool'

This expression we use a lot, and I don't understand why it is giving a compiler error. More importantly, our solution has thousands of files, and I don't want to fix thousands of errors.

I do believe Azure is running on Linux.
 
That error reproduces again with .NET Fiddle using 4.7.2 . So the first thing I would confirm is if you truly are setting up your Azure environment for 4.8, or 4.7.2.
 
That error reproduces again with .NET Fiddle using 4.7.2 . So the first thing I would confirm is if you truly are setting up your Azure environment for 4.8, or 4.7.2.

Both 4.7.2 and 4.8 use C# 7.

As I figured out, Azure is using an old version of MSBuild and attempting to compile for C# 6, and there is no way to change it. Microsoft being Microshit as usual.
 
I suspect just a lack of attention to detail about which server family you chose. If picked family 5, then you would get 4.6.2, but if you had selected family 6 or 7 you would have gotten 4.7.2 or 4.8 respectively.


Additionally, if you had gotten a bare machine and did your own installation, you could have upgraded and installed 4.8.
 
I suspect just a lack of attention to detail about which server family you chose. If picked family 5, then you would get 4.6.2, but if you had selected family 6 or 7 you would have gotten 4.7.2 or 4.8 respectively.


Additionally, if you had gotten a bare machine and did your own installation, you could have upgraded and installed 4.8.

This is incorrect. It's also not how it works. It also has nothing to do with what I'm talking about. If you look at my latest comment, I figured out the cause of the build errors.

ASP.NET framework 4.8 works with previous versions because they all use C# 7.
 
I was trying to point out that there are multiple versions of Azure hosts. If you pick a host that is older, then you get a version of .NET Framework that is older, which would then have the older version of the .NET Framework toolchain, including an older MSBuild and older CSC.exe (C# compiler).
 
I did also wonder if there is a stray old LangVersion node in a project file somewhere.. though that (I think) elicits a different error message about "feature x is not available in c# y, use z or later"..
C#:
  public static bool? ToNullableBoolean(this string @string) {
return bool.TryParse(@string, out bool @bool) ? @bool : (bool?)null; }

Calling variable names the same as reserved words, meaning they need escaping, is poor practice in my book..

this string s
this string toConvert
out var b

etc (if you don't want to be descriptive, it's ok to use b, s etc because the usage is over and done with within a few lines of the declare site and isn't externally visible)

Dare say a whole word case matches find "@string" replace with "s" would sort the code out (and you should get errors if other things are called s)
 
Last edited:
I suspect our OP is incorrectly applying Hungarian notation in a language that as part of its naming convention tells people not to use Hungarian.
 
Also, the more I think about it, the more I don't really understand the point of the method

C# has a "parses a string as a bool and returns an indicator of success or not" method, so I'm not sure what this adds other than generating a `bool?` which, if nullable context is turned on, causes the compiler to moan at you more

C#:
//usage of standard c#
if(bool.TryParse(s, out var b)){
  UseBool(b);
}

//usage of this extension
if(s.ToNullableBoolean() is bool? b && b != null){
  UseBool(b.Value);
}
 
In my experience, the most common place I have seen the use of nullables (of a value type) is for tabular UI with the intent for showing that no values are available for some of the columns of that row. This happens often for data that comes in as XML (or JSON) where fields are optional in the "schema". For nulls of reference types, this more common for graph like data structures like trees and parse graphs.
 

Latest posts

Back
Top Bottom