'RestSharp error in Azure Pipeline: 'RestResponseBase.StatusCode' is not supported by the language
After upgrading to v107 I'm getting this error on the Azure Pipeline:
##[error]XXXXXXXXXXX.Shared.Integration\APIClients\XXXXXXXXXXRESTClientBase.cs(114,43): Error CS1545: Property, indexer, or event 'RestResponseBase.StatusCode' is not supported by the language; try directly calling accessor methods 'RestResponseBase.get_StatusCode()' or 'RestResponseBase.set_StatusCode(??)'
The code compiles on my laptop, but for some reason it fails on the pipeline.
Could it be related to this message in the upgrade guide? "The most important change is that RestSharp stop using the legacy HttpWebRequest class, and uses well-known 'HttpClient' instead. This move solves lots of issues, like hanging connections due to improper HttpClient instance cache, updated protocols support, and many other problems."
I'm using .NET core 3.1 which still has LTS.
An example of where compilation fails is in this example:
private bool RequestFailed(HttpStatusCode statusCode)
{
var result = statusCode == HttpStatusCode.InternalServerError ||
statusCode == HttpStatusCode.ServiceUnavailable ||
statusCode == HttpStatusCode.Unauthorized;
if (result) Logger.Verbose("API Call resulted in status code: " + statusCode.ToString());
return result;
}
private bool HandleResult(RestResponse response)
{
return RequestFailed(response.StatusCode);
}
Which errors on the line:
return RequestFailed(response.StatusCode);
Similar issue:
error CS1545: Property, indexer, or event 'Parameter.Name' is not supported by the language
public static void SetCorrelationId(RestRequest request, Guid? correlationId)
{
var existingHeader = request.Parameters.Where(x => x.Name == CorrelationIdHeaderKey).FirstOrDefault();
...
}
Solution 1:[1]
The issue is caused by .NET Core 3.1 SDK being unable to work with properties that have init instead of set, although RestSharp is built to target .NET Standard, and should work with .NET Core 3.1.
It works on your machine because you have a newer SDK (.NET 5 or 6). Newer SDKs can build for legacy targets, so it works just fine. In your CI pipeline you, most probably, use the .NET Core 3.1 SDK to build the solution, and it fails.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Alexey Zimarev |
