备考AZ-204:.NET 8构建Azure Blob存字符串C#项目编译报错求助
问题:.NET 8环境下编译net6.0 Azure Blob项目出现NU1100包解析错误
Java开发者备考AZ-204证书时,使用ChatGPT生成了一个极简.NET C#项目(实现字符串写入Azure Blob存储),已安装.NET 8 SDK,但执行dotnet build -o out命令时触发NU1100错误,无法解析指定NuGet包及.NET 6框架依赖,项目结构需保持极简。
原项目文件、代码及报错信息如下:
原项目文件
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <UseWPF>false</UseWPF> <UseWindowsForms>false</UseWindowsForms> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Storage.Blobs" Version="12.10.0" /> <PackageReference Include="Microsoft.Azure.Identity" Version="1.7.0" /> </ItemGroup> </Project>
原C#代码文件
using System; using System.IO; using System.Text; using System.Threading.Tasks; using Azure.Identity; using Azure.Storage.Blobs; class Program { static async Task Main(string[] args) { // Replace these values with your actual Azure Blob Storage information var blobStorageUrl = "https://somestorage.blob.core.windows.net/"; var containerName = "somecontainer1"; var blobName = "vm1example.txt"; // Name of the file in Blob Storage // Create a token credential using the system-assigned managed identity var tokenCredential = new DefaultAzureCredential(); // Create a BlobServiceClient using the token credential var blobServiceClient = new BlobServiceClient(new Uri(blobStorageUrl), tokenCredential); // Get a reference to the container var blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName); // Create or get a reference to the blob var blobClient = blobContainerClient.GetBlobClient(blobName); // Specify the content to write to the blob var content = "Hello, Azure Blob Storage!"; // Convert the content to bytes var contentBytes = Encoding.UTF8.GetBytes(content); // Create a stream from the content bytes using (var stream = new MemoryStream(contentBytes)) { // Upload the content to the blob await blobClient.UploadAsync(stream, true); } Console.WriteLine("String written to Blob Storage successfully."); } }
编译报错信息
C:\programas\writeblobfile.csproj : error NU1100: Unable to resolve 'Azure.Storage.Blobs (>= 12.10.0)' for 'net6.0'. C:\programas\writeblobfile.csproj : error NU1100: Unable to resolve 'Microsoft.Azure.Identity (>= 1.7.0)' for 'net6.0'. C:\programas\writeblobfile.csproj : error NU1100: Unable to resolve 'Microsoft.NETCore.App.Ref (= 6.0.25)' for 'net6.0'. C:\programas\writeblobfile.csproj : error NU1100: Unable to resolve 'Microsoft.WindowsDesktop.App.Ref (= 6.0.25)' for 'net6.0'. C:\programas\writeblobfile.csproj : error NU1100: Unable to resolve 'Microsoft.AspNetCore.App.Ref (= 6.0.25)' for 'net6.0'. C:\programas\writeblobfile.csproj : error NU1100: Unable to resolve 'Microsoft.NETCore.App.Host.win-x64 (= 6.0.25)' for 'net6.0'. 0 Warning(s) 6 Error(s)
解决方案
1. 调整项目目标框架与NuGet包版本
将项目目标框架改为net8.0(匹配已安装的SDK),同时更新NuGet包到兼容.NET 8的稳定版本,替换旧版Microsoft.Azure.Identity为新版Azure.Identity(包命名已标准化),移除不必要的WPF/WinForms配置项:
修改后的.csproj文件:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" /> <PackageReference Include="Azure.Identity" Version="1.12.0" /> </ItemGroup> </Project>
2. 清理缓存并重新还原依赖
执行以下命令清除本地编译缓存,重新获取依赖包:
dotnet clean dotnet restore
3. 重新编译项目
执行编译命令:
dotnet build -o out
4. 代码说明
原C#代码无需修改,仅需将blobStorageUrl、containerName替换为自己的Azure Blob存储实例信息即可运行。
内容的提问来源于stack exchange,提问作者sekelsenmat
相关产品推荐
相关产品推荐

