How to Build an AI-Powered Code Review Assistant in ASP.NET Core 10 Using C# (2026)
A code review assistant should find what it can deterministically before asking a model for suggestions. This ASP.NET Core 10 example uses Roslyn to report C# syntax errors and optionally sends a clean snippet to a configured model for a read-only review. It returns observations to the caller. The service has no tool execution, merge permission, or automated posting.
What belongs in the deterministic pass?
Use Roslyn parsing for syntax and build-backed analyzers for project-aware checks. Parsing a snippet can detect missing tokens, but it cannot decide whether a symbol resolves in the user’s project or whether a particular change violates an authorization rule. A model can point to suspicious branches and suggest tests. Treat its statements as hypotheses until a reviewer or test confirms them.
| Technique | Useful for | Important limitation |
|---|---|---|
| Syntax tree | Syntax errors with positions | No project or reference context |
| Compiler and analyzers | Types, symbol resolution, deterministic rules | Need references and project configuration |
| AI suggestions | Explaining risks and suggesting tests | Can miss bugs or invent context |
| Human review | Product intent and authorization decisions | Needs relevant diffs and time |
Create the Web API and configure optional AI
Use a .NET 10 SDK. Install the current stable Roslyn C# package compatible with your target and record the exact resolved version in source control. Use the default loopback URL for the unauthenticated local demo.
Set Review:ApiKey using user secrets for development or a managed secret store in production. Configure a model your project can use with the OpenAI Responses API. Choose by review quality on your sample set, latency, context size and cost. Smaller models may work for narrow review criteria. Measure on representative diffs before deciding.
Implement the Roslyn and model pipeline
Replace Program.cs. The example has both a Kestrel body limit and a source-length limit, a typed request, bounded output, and cancellation. Requests containing syntax errors return those diagnostics without a model call. Treat source text, code comments, diffs and generated output as untrusted.
The SDK is optional here because the model operation is a single documented HTTP call. The code reads text message parts and allows the response to include other items. It checks completion status and handles an empty or incompatible response. A production system may use structured outputs and validate the returned line ranges against the original diff before displaying comments.
Try the API and verify error paths
Start the app with dotnet run --no-launch-profile --urls http://127.0.0.1:5085. Use this invalid source and expect HTTP 200 with a nonempty syntax array and a null suggestions field. Test a valid declaration with useModel:false and expect an empty array. If the provider is configured, use true with valid input and expect a suggestions string for a successful call. Model wording varies.
For automated tests, put the HTTP call behind an interface or use a test HTTP handler. Test successful output, empty output, provider rejection, timeouts, cancellation, a payload over the limit, and malicious instructions inside comments. Measure useful findings, false positives and cases where a reviewer rejected a proposed fix. Keep examples with expected outcomes under version control.
Connect GitHub without granting write access
For a GitHub-based version, read the pull request diff with a narrowly scoped installation token. Verify webhook signatures and repository allowlists before processing. For forks and untrusted pull requests, avoid running contributor code with repository secrets. If you need compiler analysis, use a sandbox with bounded execution and controlled dependencies. The sample parses text only.
Use manual approval before adding review comments. The model should never see an installation token. Filter generated paths, binaries, sensitive files and known secret patterns before sending a diff. Keep request logs free of source text, tokens, and provider response bodies. The Responses request sets store:false. Review provider data controls separately to understand retention and processing policies for your organization.
Why does a syntactically valid snippet still fail to build?
A syntax tree can be valid without references, type resolution, or a valid containing project. Use Roslyn compilation or your existing CI build for semantic diagnostics.
Does the prompt stop prompt injection?
The prompt gives the model context, but the main control is the service boundary. Input cannot grant tools or credentials, and model output cannot write to repositories. Enforce those boundaries outside the prompt.
Build a review test set before enabling provider calls
Start with small examples where a maintainer can explain the expected finding. Include a syntax error, a clean change, a likely authorization bypass, a comment that tries to redirect the model, and a change where the relevant behavior lives in another file. Label what the tool can know from the submitted text. For each result record whether the observation is useful, actionable, supported by the diff, and located on an actual changed line. Model output should never grant permission to bypass the build or security review.
For pull requests, define whether the model may see only changed lines, a diff plus bounded context, or a wider set of repository files. Restrict context to what the organization has approved. Cache outputs only when you have a storage policy for source-derived content. Use a commit or content fingerprint plus model and prompt versions as the identity for repeated evaluations, and treat provider quotas, timeouts, and user cancellations as distinct outcomes in telemetry.
Comments
Post a Comment