将Azure Bicep文件构建为JSON时遇System.FormatException的排查与解决
问题
执行命令bicep build simple.bicep --outfile simple.json将Azure Bicep文件编译为ARM模板时,触发未处理的System.FormatException,错误提示:The input string 'dons/microsoft.addons/2017-05-15/types.json#/17' was not in a correct format.
使用的simple.bicep代码:
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = { name: 'mystorageaccount' location: 'West US' kind: 'StorageV2' sku: { name: 'Standard_LRS' } }
环境信息:
- Bicep CLI版本:0.28.1
- Azure CLI版本:2.62.0
- 操作系统:Windows 11 Pro
完整报错堆栈:
Unhandled exception. System.FormatException: The input string 'dons/microsoft.addons/2017-05-15/types.json#/17' was not in a correct format. at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1) at System.Number.ThrowOverflowOrFormatException[TChar,TInteger](ParsingStatus, ReadOnlySpan`1) at System.Number.ParseBinaryInteger[TChar,TInteger](ReadOnlySpan`1, NumberStyles, NumberFormatInfo) at System.Int32.Parse(ReadOnlySpan`1 s, NumberStyles style, IFormatProvider provider) at System.Int32.Parse(String s, NumberStyles style, IFormatProvider provider) at System.Int32.Parse(String s) at Azure.Bicep.Types.Serialization.CrossFileTypeReferenceConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , T& , Boolean& ) at System.Text.Json.Serialization.JsonDictionaryConverter`3.OnTryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , TDictionary& ) at System.Text.Json.Serialization.Converters.JsonMetadataServicesConverter`1.OnTryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , T& ) at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , T& , Boolean& ) at System.Text.Json.Serialization.JsonConverter`1.TryReadAsObject(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , Object& ) at System.Text.Json.Serialization.Converters.LargeObjectWithParameterizedConstructorConverter`1.ReadAndCacheConstructorArgument(ReadStack& , Utf8JsonReader&, JsonParameterInfo) at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.ReadConstructorArgumentsWithContinuation(ReadStack& , Utf8JsonReader&, JsonSerializerOptions) at System.Text.Json.Serialization.Converters.ObjectWithParameterizedConstructorConverter`1.OnTryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , T& ) at System.Text.Json.Serialization.Converters.JsonMetadataServicesConverter`1.OnTryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , T& ) at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader&, Type, JsonSerializerOptions, ReadStack& , T& , Boolean& ) at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader&, JsonSerializerOptions, ReadStack&) at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.ContinueDeserialize(ReadBufferState&, JsonReaderState&, ReadStack&) at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Stream) at System.Text.Json.JsonSerializer.Deserialize[TValue](Stream , JsonSerializerOptions ) at Azure.Bicep.Types.Serialization.TypeSerializer.DeserializeIndex(Stream contentStream) at Azure.Bicep.Types.TypeLoader.LoadTypeIndex() at Bicep.Core.TypeSystem.Providers.Az.AzResourceTypeLoader..ctor(ITypeLoader typeLoader) at Bicep.Core.Semantics.Namespaces.AzNamespaceType.<>c.<.cctor>b__27_0() at System.Lazy`1.ViaFactory(LazyThreadSafetyMode) at System.Lazy`1.ExecutionAndPublication(LazyHelper, Boolean) at System.Lazy`1.CreateValue() at System.Lazy`1.get_Value() at Bicep.Core.Semantics.Namespaces.AzNamespaceType.get_BuiltInTypeProvider() at Bicep.Core.TypeSystem.Providers.ResourceTypeProviderFactory.GetBuiltInAzResourceTypesProvider() at Bicep.Core.Semantics.Namespaces.NamespaceProvider.GetNamespaceTypeForConfigManagedProvider(RootConfiguration rootConfig, IFeatureProvider features, BicepSourceFile sourceFile, ResourceScope targetScope, ArtifactResolutionInfo artifact, ProviderDeclarationSyntax syntax, String providerName) at Bicep.Core.Semantics.Namespaces.NamespaceProvider.GetNamespaceTypeForImplicitProvider(RootConfiguration rootConfig, IFeatureProvider features, BicepSourceFile sourceFile, ResourceScope targetScope, ImplicitProvider implicitProvider, ProviderDeclarationSyntax syntax) at Bicep.Core.Semantics.Namespaces.NamespaceProvider.GetNamespaces(RootConfiguration rootConfig, IFeatureProvider features, IArtifactFileLookup artifactFileLookup, BicepSourceFile sourceFile, ResourceScope targetScope)+MoveNext() at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1) at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1) at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items) at System.Collections.Immutable.ImmutableArray.ToImmutableArray[TSource](IEnumerable`1) at Bicep.Core.Semantics.Binder..ctor(INamespaceProvider namespaceProvider, RootConfiguration configuration, IFeatureProvider features, IArtifactFileLookup sourceFileLookup, ISemanticModelLookup modelLookup, BicepSourceFile sourceFile, ISymbolContext symbolContext) at Bicep.Core.Semantics.SemanticModel..ctor(Compilation compilation, BicepSourceFile sourceFile) at Bicep.Core.Semantics.Compilation.CreateSemanticModel(BicepSourceFile bicepFile) at Bicep.Core.Semantics.Compilation.<>c__DisplayClass1_1.<.ctor>b__2() at System.Lazy`1.ViaFactory(LazyThreadSafetyMode) at System.Lazy`1.ExecutionAndPublication(LazyHelper, Boolean) at System.Lazy`1.CreateValue() at System.Lazy`1.get_Value() at Bicep.Core.Semantics.Compilation.GetSemanticModel(ISourceFile sourceFile) at Bicep.Core.Semantics.Compilation.GetSemanticModel[T](ISourceFile sourceFile) at Bicep.Core.Semantics.Compilation.GetSemanticModel(BicepSourceFile bicepFile) at Bicep.Core.Semantics.Compilation.<GetAllDiagnosticsByBicepFile>b__31_1(BicepSourceFile bicepFile) at System.Collections.Immutable.ImmutableDictionary.<>c__DisplayClass9_0`3.<ToImmutableDictionary>b__0(TSource element) at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext() at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1, MutationInput, KeyCollisionBehavior ) at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1, Boolean) at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 ) at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1, Func`2, Func`2, IEqualityComparer`1 , IEqualityComparer`1 ) at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1, Func`2, Func`2) at Bicep.Core.Semantics.Compilation.GetAllDiagnosticsByBicepFile() at Bicep.Cli.Logging.DiagnosticLogger.LogDiagnostics(DiagnosticOptions options, Compilation compilation) at Bicep.Cli.Commands.BuildCommand.RunAsync(BuildArguments args) at Bicep.Cli.Program.RunAsync(String[] args, CancellationToken cancellationToken) at Bicep.Cli.Program.<>c__DisplayClass3_0.<Main>b__0>d.MoveNext() --- End of stack trace from previous location --- at Bicep.Cli.Program.RunWithCancellationAsync(Func`2 runFunc) at Bicep.Cli.Program.Main(String[] args) at Bicep.Cli.Program.<Main>(String[] args)
成因分析
这个错误源于Bicep CLI的类型加载逻辑缺陷:
- 0.28.1版本的Bicep CLI在解析Azure资源类型定义时,错误地将类型引用路径中的
#/17部分当作整数解析,导致格式转换失败。 - 本地Bicep类型缓存文件可能已损坏,或者缓存的资源类型定义格式与当前CLI版本不兼容,触发了该解析bug。
解决步骤
清理本地Bicep类型缓存
打开文件资源管理器,导航到路径%USERPROFILE%\.bicep\br,删除该目录下的所有文件和子目录,清除损坏的缓存数据。升级Bicep CLI到最新稳定版本
打开命令提示符或PowerShell,执行以下命令升级:az bicep upgrade执行
bicep --version确认版本已更新。重新执行构建命令
再次运行构建命令,Bicep会重新下载并解析正确的资源类型定义:bicep build simple.bicep --outfile simple.json
内容的提问来源于stack exchange,提问作者ทศพล พิทยาสาธิต
相关产品推荐
相关产品推荐

