I’m getting an error in VS trying to check the Return Code of a method that builds and post data via an API.
The line that is generating the error is:
if (BuildApi(MyGlobals.data5, MyGlobals.data1, FQAN, MyGlobals.data4) == MyGlobals.ReturnCode)
The error is:
Operator '==' cannot be applied to operands of type 'Task<string'>' and 'string'
My goal is to pass those parameters (data1, data5, etc) to the BuildApi()
method and then post that data via an API call.
When the data is successfully posted, I should get a Return Code of 200 or a Return Code of 400 if an error occurred (according to the API developer).
The BuildApi()
method should return either a 200 or 400 back to the condition statement.
Is the BuildApi()
method formatted correctly to return the Return Code and if so, what’s wrong with that “if” statement?
Full Code:
static class MyGlobals
{
public static XmlDocument XmlAccounts = new XmlDocument();
public static XmlNode XmlRoot;
public static string data1 { get; set; }
public static string data2 { get; set; }
public static string data3 { get; set; }
public static string data4 { get; set; }
public static string data5 { get; set; }
public static string ReturnCode { get; set; }
}
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
SqlConnection ObjConn = new SqlConnection();
string ConnectMe = @"
Data Source =SERVER;
Database =DATABASE1;
User ID =USER;
Pwd =PASS;
Connection Timeout =700
";
// Open Connection
ObjConn = new SqlConnection(ConnectMe);
ObjConn.Open();
// Call methods based on the required tool
SR_Provisioning(ObjConn);
}
static public void SR_Provisioning(SqlConnection ObjConn)
{
Get = @"
SELECT
data1,
data2,
data3,
data4,
data5
FROM
table
";
ObjAdp = new SqlDataAdapter(Get, ObjConn);
ObjAdp.Fill(OutputTable);
foreach (DataRow OutputRow in OutputTable.Rows)
{
//Initalize FQAN
string FQAN = "";
// Convert query output to variables
MyGlobals.data1 = OutputRow[0].ToString();
MyGlobals.data2 = OutputRow[1].ToString();
MyGlobals.data3 = OutputRow[2].ToString();
MyGlobals.data4 = OutputRow[3].ToString();
MyGlobals.data5 = OutputRow[4].ToString();
// Instantiate new objects
strFunctions MyStr = new strFunctions();
wshWin32API win32api = new wshWin32API();
// Convert server to FQDN for accessibility ease
string FQDN = getFQDN(MyGlobals.data1, ObjConn);
// Perform action based on Tranaction_Type
switch (MyGlobals.data5)
{
case "Add":
if (MyGlobals.data2 == "LOCAL")
{
// Create local ID first
try
{
FQAN = MyGlobals.data1 + "\\" + MyGlobals.data3;
// Check the return code to determine how to log the results
if (BuildApi(MyGlobals.data5, MyGlobals.data1, FQAN, MyGlobals.data4) == MyGlobals.ReturnCode)
{
switch (MyGlobals.ReturnCode)
/*
Return Codes
200 (Created)
400(Expectation Failed)
*/
{
case "200":
// Do something
AllIsGood();
break;
case "400":
// Do something else
AllIsBad();
break;
}
}
}
catch (Exception err)
{
// Handle error and update transaction record
Update_Row();
}
}
}
static async Task<string> BuildApi(string data5, string data1, string FQAN, string data4)
{
try
{
UriBuilder baseUri = new UriBuilder("https://pwmfunction001.azurewebsites.net/api/VMGroupMemberModify01?code=T753ljF4jwXZXzmotCnnrBdV7Mrbqvcd3ibazRb92ZoBfJADuCpq5w==-Headers@{Metadata=true}-Body@{");
// Create the query string
string queryToAppend = "DATA5=" + data5 + ";DATA1=" + data1 + ";FQAN=" + FQAN + ";data4=" + data4 + "}";
if (baseUri.Query != null && baseUri.Query.Length > 1)
{
baseUri.Query = baseUri.Query.Substring(1) + ";" + queryToAppend;
}
else
{
// Check this
baseUri.Query = queryToAppend;
}
string httpResponseBody = "";
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(client.ToString());
HttpResponseMessage response = await client.PostAsync(baseUri.ToString(), content);
if (response.IsSuccessStatusCode)
{
httpResponseBody = "200";
return httpResponseBody;
}
else
{
httpResponseBody = "400";
return httpResponseBody;
}
}
catch(HttpRequestException err)
{
throw err;
}
}
}
}
Your BuildApi function is async so you need to await it in your code:
UPDATE:
If you can't run it async then you need the result:
However, I would try in the first instance to make your calling method async