I'm trying to create a workitem in Azure DevOps using client libraries and using OAuth as authentication mechanism.
It works fine on my local machine (when I debug it locally), but throws exceptions whenever deployed on cloud(in my case Azure App service).
public string CreateWorkItemDemo(string accesstoken)
{
try
{
Uri _uri = new Uri("https://xyz.visualstudio.com");
JsonPatchDocument patchDocument = new JsonPatchDocument();
string project = "abcproject";
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.Title",
Value = "Test item created through token two"
});
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.AreaPath",
Value = string.Format("{0}", project)
});
try
{
VssConnection connection = new VssConnection(_uri, new VssOAuthAccessTokenCredential(accesstoken));
WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();
try
{
var result = workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, project, "Bug").Result; //This line of code throws exception
return result.Id.Value.ToString();
}
catch (Exception ex)
{
//The exceptions is logged from here.
}
}
catch (Exception ex)
{
//Exception messages here
}
}
catch (Exception exception)
{
//Exception messages here
}
The exception that it is throwing is:
One or more errors occurred. ex.stacktrace at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task
1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task
1.get_Result()
This method is called directly from a link and the accesstoken is passed along with it. Earlier I was calling this method through ajax call as I thought maybe ajax calls don't wait for async. But it throwed the same exception.
Then I changed my method to async/await and called it through a link. Here is the code:
public async Task<string> CreateItem(string accesstoken)
{
string _uri = "https://xyz.visualstudio.com";
Uri uri = new Uri(_uri);
string project = "abcproject";
JsonPatchDocument patchDocument = new JsonPatchDocument();
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.Title",
Value = "Test item created through code seven"
});
patchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.AreaPath",
Value = string.Format("{0}", project)
});
try
{
VssConnection connection = new VssConnection(uri, new VssOAuthAccessTokenCredential(accesstoken));
WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient<WorkItemTrackingHttpClient>();
try
{
var response = await workItemTrackingHttpClient.CreateWorkItemAsync(patchDocument, project, "Bug");
return "Successfully created bug with Id" + response.Id.Value.ToString();
}
catch (Exception ex)
{
//Exceptions are logging here
return ex.Message + " ," + ex.StackTrace + " One here";
}
}
catch (Exception ex)
{
return ex.Message + " ," + ex.StackTrace + " 2 here";
}
}
Here I'm getting this kind of exception and with the last line:
d__52.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__50.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__47
1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.d__28
1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)You must pass a valid patch document in the body of the request. , at Microsoft.VisualStudio.Services.WebApi.VssHttpClientBase.
Since I'm using OAuth mechanism, the accesstokens are generated only in my cloud app. So to run it locally, I've created a separate Asp.net web application and running it locally by passing the access token generated from my cloud app.
And I can't emphasize it enough that both the methods are running perfectly fine when I ran it locally multiple times.
I'm strucked badly with this, and since I'm a little bit newbie to this programming world, any help with a little bit detail will be much appreciated