Fixing ASP.NET Core 10 OpenAPI Error: 'IOpenApiMediaType.Example cannot be assigned'
Introduction
With the release of .NET 10, ASP.NET Core continues to enhance its built-in OpenAPI support, reducing reliance on third-party libraries like Swashbuckle. However, as early adopters migrate their applications or build new Minimal APIs, they may encounter compilation errors from the new built-in source generators.
A common issue developers are facing is a compilation failure pointing to auto-generated files in the obj directory, producing the following error:
error CS0200: Property or indexer 'IOpenApiMediaType.Example' cannot be assigned to -- it is read onlyIn this article, we will analyze why this error occurs and walk through the simple steps to resolve it.
The Root Cause: Package Version Mismatch
The error originates from the Microsoft.AspNetCore.OpenApi.SourceGenerators (specifically the XmlCommentGenerator). This source generator parses your C# XML comments to enrich your OpenAPI documentation with details like request/response examples.
The issue occurs due to a breaking contract change between different major versions of the underlying Microsoft.OpenApi library.
If your project file (.csproj) explicitly references these mismatched versions:
Microsoft.AspNetCore.OpenApi(targeting version 10.x)Microsoft.OpenApi(targeting version 3.x, such as 3.9.0)
The source generator in ASP.NET Core 10 expects the object model of Microsoft.OpenApi v2.x. In Microsoft.OpenApi v3.x, the IOpenApiMediaType.Example property was made read-only or restructured, causing the generated code to fail compilation with the CS0200 error.
How to Fix the Issue
There are three primary ways to resolve this compilation error, depending on your project's requirements.
Solution 1: Downgrade Microsoft.OpenApi to 2.x (Recommended)
The most straightforward fix is to align your project with the version of the OpenAPI object model that the ASP.NET Core 10 source generator expects. Downgrading Microsoft.OpenApi to the 2.x.x release cycle solves the issue immediately.
Update your .csproj file to reflect the following package reference:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.10" />
<!-- Downgrade this from 3.x to 2.x -->
<PackageReference Include="Microsoft.OpenApi" Version="2.1.1" />
</ItemGroup>Solution 2: Remove the Explicit Microsoft.OpenApi Reference
In many cases, you do not need to explicitly reference Microsoft.OpenApi in your project. The Microsoft.AspNetCore.OpenApi package transitively restores the correct, compatible version of Microsoft.OpenApi automatically.
Try removing the explicit reference altogether:
<ItemGroup>
<!-- The SDK/Package will transitively bring in the correct Microsoft.OpenApi version -->
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.10" />
</ItemGroup>After removing the package, clean your solution, delete your bin and obj folders, and rebuild.
Solution 3: Disable XML Documentation (Workaround)
If you do not rely on XML documentation comments to generate your OpenAPI schemas, you can disable documentation generation in your build properties. This prevents the XmlCommentGenerator from running, bypassing the error entirely.
In your Directory.Build.props or .csproj file, change the property:
<PropertyGroup>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
</PropertyGroup>Note: This is a workaround. Disabling this means your API endpoint summaries, remarks, and parameter descriptions written in XML comments will no longer appear in your Swagger/OpenAPI UI.
Conclusion
The CS0200 error in ASP.NET Core 10's OpenAPI generator is a classic case of dependency misalignment. By ensuring your project uses Microsoft.OpenApi v2.x (either by downgrading or relying on transitive dependencies), you can enjoy the full benefits of native .NET 10 OpenAPI generation without any compiler friction.