Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Azure函数中调用Azure DevOps REST API_C#_Azure_Azure Devops_Azure Functions_Azure Devops Rest Api - Fatal编程技术网

C# 在Azure函数中调用Azure DevOps REST API

C# 在Azure函数中调用Azure DevOps REST API,c#,azure,azure-devops,azure-functions,azure-devops-rest-api,C#,Azure,Azure Devops,Azure Functions,Azure Devops Rest Api,我正在尝试为Azure DevOps团队创建一个POST请求,并希望通过API方法创建一个新团队。我已经阅读了这方面的文档,但是我对这方面还不熟悉,不知道如何在我自己的项目中正确地实现这一点 我运行它时出错。错误是由返回值引起的。由于我使用RESTAPI的经验很少,所以如果有人能引导我走向正确的方向,我将不胜感激 我正在使用Visual Studio和.NET Core 3.0 以下是迄今为止的代码: using System.IO; using System; using Microsoft.

我正在尝试为Azure DevOps团队创建一个POST请求,并希望通过API方法创建一个新团队。我已经阅读了这方面的文档,但是我对这方面还不熟悉,不知道如何在我自己的项目中正确地实现这一点

我运行它时出错。错误是由返回值引起的。由于我使用RESTAPI的经验很少,所以如果有人能引导我走向正确的方向,我将不胜感激

我正在使用Visual Studio和.NET Core 3.0

以下是迄今为止的代码:

using System.IO;
using System;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;

namespace TeamsAdd
{
    public static class TeamsAdd
    {
        [FunctionName("TeamsAdd")]
        public static HttpResponseMessage Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequestMessage req)
        {
            var PAT = "xxxx";
            var body = new
            {
                name = "sampleTeamName",
                id = "xxxx",
                projectId = "xxxx",                 
            };

            using (HttpClient client = new HttpClient())
            {

                client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        System.Text.ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "", PAT))));

                //Connecting to the DevOps REST API
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://dev.azure.com/{organization}/_apis/projects/{projectId}/teams?api-version=6.0");
                requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

                //Reading Server Response
                using (HttpResponseMessage response = client.SendAsync(requestMessage).Result)
                {
                    response.EnsureSuccessStatusCode();
                }
            }
        }       
    }
}

关于此问题,您可以参考以下代码

[FunctionName("Function2")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req)
        {
            var PAT = "";
            var body = new
            {
                name = "mytest7878or",

            };

            using (HttpClient client = new HttpClient())
            {

                client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                    Convert.ToBase64String(
                        ASCIIEncoding.ASCII.GetBytes(
                            string.Format("{0}:{1}", "", PAT))));

                //Connecting to the DevOps REST API
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, $"https://dev.azure.com/{}/_apis/projects/{}/teams?api-version=6.0");
                requestMessage.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

                //Reading Server Response
                using (HttpResponseMessage response = await client.SendAsync(requestMessage))
                {
                    if (!response.IsSuccessStatusCode)
                    {
                        
                        response.EnsureSuccessStatusCode();
                    }

                    return req.CreateResponse(HttpStatusCode.Created,"create successfully");
                }
            }
        }
[FunctionName(“Function2”)]
公共静态异步任务运行([HttpTrigger(AuthorizationLevel.Anonymous,“get”,“post”,Route=null)]HttpRequestMessage请求)
{
var PAT=“”;
变量体=新
{
name=“mytest7878or”,
};
使用(HttpClient=new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“基本”,
Convert.tobase64字符串(
ascienceoding.ASCII.GetBytes(
格式(“{0}:{1}”,”,PAT));
//连接到DevOps REST API
var requestMessage=newhttprequestmessage(HttpMethod.Post,$)https://dev.azure.com/{}/_api/projects/{}/teams?api版本=6.0“;
requestMessage.Content=newstringcontent(JsonConvert.SerializeObject(body),Encoding.UTF8,“application/json”);
//读取服务器响应
使用(HttpResponseMessage response=wait client.SendAsync(requestMessage))
{
如果(!response.issucessStatusCode)
{
response.EnsureSuccessStatusCode();
}
返回请求CreateResponse(HttpStatusCode.Created,“创建成功”);
}
}
}

Hi@merum请与我们分享错误。我也没有看到
$”https://dev.azure.com/{organization}/_api/projects/{projectId}/teams?api version=6.0“
organization
projectId
来自此字符串替换的变量。只是为了确定,您的原始代码中是否有这些变量?是的,我的原始代码中有这些变量。另外,错误是Run()应该返回它没有返回的内容。错误是CS0161-“TeamAdd.Run(HttpRequestMessage)”:并非所有代码路径都返回值。只是缺少return语句。请在`response.EnsureSuccessStatusCode()之后添加
返回响应
`您可能不应该在静态类/方法中调用SendAsync()方法。添加.Result将强制它运行,但它是否真的能工作有点不确定。如果您正在使用HttpClient进行调用,通常最安全的方法是使用async/await模式,这样您的方法签名将是
public asyc Task Run
。另外,还不清楚您传入的
HttpRequestMessage
是用于。。。?