C# 编译自动测试生成的客户端时出错

C# 编译自动测试生成的客户端时出错,c#,autorest,C#,Autorest,使用Swagger OpenApi 3.0规范自动生成客户端后,我无法编译生成的客户端。我在对象ClientDiagnostics,RawRequestUriBuilder,Utf8JsonRequestContent上出错 RestClient.cs // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // <auto-generated/>

使用Swagger OpenApi 3.0规范自动生成客户端后,我无法编译生成的客户端。我在对象ClientDiagnosticsRawRequestUriBuilderUtf8JsonRequestContent上出错

RestClient.cs

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Core.Pipeline;
using Planner.Client.Models;

namespace Planner.Client
{
    internal partial class RestClient
    {
        private Uri endpoint;
        private ClientDiagnostics _clientDiagnostics;
        private HttpPipeline _pipeline;

        /// <summary> Initializes a new instance of RestClient. </summary>
        /// <param name="clientDiagnostics"> The handler for diagnostic messaging in the client. </param>
        /// <param name="pipeline"> The HTTP pipeline for sending and receiving REST requests and responses. </param>
        /// <param name="endpoint"> server parameter. </param>
        public RestClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint = null)
        {
            endpoint ??= new Uri("https://localhost:49157");

            this.endpoint = endpoint;
            _clientDiagnostics = clientDiagnostics;
            _pipeline = pipeline;
        }


        internal HttpMessage CreateCreateDataRequest(IEnumerable<DataCreateModel> body)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;
            request.Method = RequestMethod.Post;
            var uri = new RawRequestUriBuilder();
            uri.Reset(endpoint);
            uri.AppendPath("/api/Budget/CreateData", false);
            request.Uri = uri;
            if (body != null)
            {
                request.Headers.Add("Content-Type", "application/json");
                var content = new Utf8JsonRequestContent();
                content.JsonWriter.WriteStartArray();
                foreach (var item in body)
                {
                    content.JsonWriter.WriteObjectValue(item);
                }
                content.JsonWriter.WriteEndArray();
                request.Content = content;
            }
            return message;
        }
    }
}

//版权所有(c)微软公司。版权所有。
//根据麻省理工学院许可证授权。
// 
#可空禁用
使用制度;
使用System.Collections.Generic;
使用System.Text.Json;
使用系统线程;
使用System.Threading.Tasks;
使用Azure;
使用Azure.Core;
使用Azure.Core.Pipeline;
使用Planner.Client.Models;
名称空间规划器.Client
{
内部部分类RestClient
{
私有Uri端点;
私人客户端诊断(ClientDiagnostics);;
私有HttpPipeline\u管道;
///初始化RestClient的新实例。
///客户端中诊断消息传递的处理程序。
///用于发送和接收REST请求和响应的HTTP管道。
///服务器参数。
公共RestClient(ClientDiagnostics ClientDiagnostics,HttpPipeline管道,Uri端点=null)
{
端点???=新Uri(“https://localhost:49157");
this.endpoint=端点;
_clientDiagnostics=clientDiagnostics;
_管道=管道;
}
内部HttpMessage CreateDataRequest(IEnumerable正文)
{
var message=_pipeline.CreateMessage();
var request=message.request;
request.Method=RequestMethod.Post;
var uri=新的RawRequestUriBuilder();
重置(端点);
AppendPath(“/api/Budget/CreateData”,false);
Uri=Uri;
if(body!=null)
{
Add(“内容类型”、“应用程序/json”);
var content=new Utf8JsonRequestContent();
content.JsonWriter.writestarray();
foreach(主体中的变量项)
{
content.JsonWriter.WriteObjectValue(项);
}
content.JsonWriter.WriteEndArray();
request.Content=内容;
}
返回消息;
}
}
}
另外,在IUtf8JsonSerializableOptional上的下面自动生成的序列化类中获取错误

DataCreateModel.Serialization.cs

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// <auto-generated/>

#nullable disable

using System.Text.Json;
using Azure.Core;

namespace Planner.Client.Models
{
    public partial class DataCreateModel : IUtf8JsonSerializable
    {
        void IUtf8JsonSerializable.Write(Utf8JsonWriter writer)
        {
            writer.WriteStartObject();
            if (Optional.IsDefined(ProjectCode))
            {
                if (ProjectCode != null)
                {
                    writer.WritePropertyName("projectCode");
                    writer.WriteStringValue(ProjectCode);
                }
                else
                {
                    writer.WriteNull("projectCode");
                }
            }
            if (Optional.IsDefined(TeamCode))
            {
                if (TeamCode != null)
                {
                    writer.WritePropertyName("teamCode");
                    writer.WriteStringValue(TeamCode);
                }
                else
                {
                    writer.WriteNull("teamCode");
                }
            }
            if (Optional.IsDefined(YearMonth))
            {
                writer.WritePropertyName("yearMonth");
                writer.WriteNumberValue(YearMonth.Value);
            }
            if (Optional.IsDefined(Hours))
            {
                writer.WritePropertyName("hours");
                writer.WriteNumberValue(Hours.Value);
            }
            writer.WriteEndObject();
        }
    }
}

//版权所有(c)微软公司。版权所有。
//根据麻省理工学院许可证授权。
// 
#可空禁用
使用System.Text.Json;
使用Azure.Core;
名称空间规划器.Client.Models
{
公共部分类DataCreateModel:IUtf8JsonSerializable
{
void IUtf8JsonSerializable.Write(Utf8JsonWriter)
{
writer.WriteStartObject();
if(可选。已定义(项目代码))
{
if(ProjectCode!=null)
{
writer.WritePropertyName(“项目代码”);
writer.WriteStringValue(项目代码);
}
其他的
{
writer.WriteNull(“项目代码”);
}
}
如果(可选。已定义(组码))
{
如果(组码!=null)
{
writer.WritePropertyName(“团队代码”);
writer.WriteStringValue(团队代码);
}
其他的
{
writer.WriteNoll(“团队代码”);
}
}
如果(可选。已定义(年-月))
{
writer.WritePropertyName(“yearMonth”);
writer.WriteNumberValue(YearMonth.Value);
}
如果(可选。已定义(小时))
{
writer.WritePropertyName(“小时”);
writer.WriteNumberValue(小时数值);
}
writer.WriteEndObject();
}
}
}
此项目使用.net 5、autorest v3.1.0、cli v3.1.2和节点v14.15.5


我遗漏了什么?

新的自动测试似乎需要对csproj进行一些更改

对于我的情况,我不得不把它改成

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
  <Nullable>annotations</Nullable>
  <IncludeGeneratorSharedCode>true</IncludeGeneratorSharedCode>
  <RestoreAdditionalProjectSources>https://azuresdkartifacts.blob.core.windows.net/azure-sdk-tools/index.json</RestoreAdditionalProjectSources>
  <LangVersion>8</LangVersion>
</PropertyGroup>
  
<ItemGroup>
  <PackageReference Include="Azure.Core" Version="1.10.0" />
  <PackageReference Include="Microsoft.Azure.AutoRest.CSharp" Version="3.0.0-beta.20210311.1" PrivateAssets="All" />
</ItemGroup>

netstandard2.0
注释
符合事实的
https://azuresdkartifacts.blob.core.windows.net/azure-sdk-tools/index.json
8.

我只添加了这些更改,它可以正常工作

<PropertyGroup>
  <IncludeGeneratorSharedCode>true</IncludeGeneratorSharedCode>
  <RestoreAdditionalProjectSources>https://azuresdkartifacts.blob.core.windows.net/azure-sdk-tools/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>

<ItemGroup>
  <PackageReference Include="Azure.Core" Version="1.10.0" />
  <PackageReference Include="Microsoft.Azure.AutoRest.CSharp" Version="3.0.0-beta.20210311.1" PrivateAssets="All" />
</ItemGroup>

无论何时报告出现错误,请具体说明错误的具体内容。对不起,我假设错误明显出现在我用粗体显示的对象上。我应该说得更清楚些。编译期间未找到对象时出错。“未找到对象”-或未找到特定类?如果您将错误信息直接复制/粘贴到问题中,会更加清晰。谢谢!添加对Microsoft.Azure.AutoRest.CSharp的引用对我很有效。nuget包的来源是什么?我得到了“找不到包Microsoft.Azure.AutoRest.CSharp。在源代码中不存在具有此id的包”:DevExpress nuget.org“它不再是beta版并被重命名了吗?啊,我认为Anton答案中propertygroup中的>提供了很好的答案!这真的阻碍了我,现在效果很好。
class MyClientOptions : ClientOptions
{
}

static void Main(string[] args)
{
    var client = new TestRestClient(
        new ClientDiagnostics(new MyClientOptions()),
        new HttpPipeline(HttpClientTransport.Shared),
        new Uri("https://api.domain.com"));
}