Modern web applications increasingly rely on specialized or self-hosted AI models—ranging from fine-tuned domain classifiers and internal enterprise LLMs to local inference endpoints—rather than a single general-purpose cloud API. In an ASP.NET Core MVC application, wiring a custom model directly into controllers or views quickly creates tight coupling, inconsistent error handling, and fragile JSON serialization code.
By implementing IChatClient from Microsoft.Extensions.AI, you can wrap any custom model behind a standard .NET abstraction. Your MVC controllers, services, and Razor views remain completely independent of the underlying model server while still benefiting from dependency injection, logging, and composable middleware.
1. Architecture Overview
A clean integration separates transport and payload mapping from application logic:
MVC Layer (
Controllers&Views): Collects user prompts or task parameters, invokes an application service orIChatClient, and renders results without knowing how the model is hosted.Custom Adapter (
CustomModelChatClient): ImplementsIChatClient, translatesChatMessageandChatOptionsinto your custom model endpoint's request schema, and maps the response back toChatResponse.HTTP & DI Layer (
Program.cs): UsesIHttpClientFactory(AddHttpClient<T>) for connection pooling and timeouts, then registersIChatClientwith middleware such asUseLogging().
Tip: If your custom model server already exposes an OpenAI-compatible chat endpoint (for example, vLLM or LocalAI), you can point
OpenAIClientat that base URL first. Writing a customIChatClientis ideal when your endpoint uses proprietary request/response fields, custom headers, or internal inference gateways.
2. Project Setup and NuGet Packages
Create or open an ASP.NET Core MVC project (.NET 8 or .NET 9+) and install Microsoft.Extensions.AI:
Bash
dotnet add package Microsoft.Extensions.AI
This single package brings in IChatClient, ChatMessage, ChatResponse, ChatOptions, and ChatClientBuilder middleware extensions.
3. Implementing a Custom IChatClient
Create Infrastructure/CustomModelChatClient.cs to bridge Microsoft.Extensions.AI with your custom model HTTP API.
C#
using System.Net.Http.Json;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using Microsoft.Extensions.AI;
namespace CustomModelMvcApp.Infrastructure;
public sealed class CustomModelChatClient : IChatClient
{
private readonly HttpClient _httpClient;
private readonly string _defaultModel;
public CustomModelChatClient(HttpClient httpClient, string defaultModel)
{
_httpClient = httpClient;
_defaultModel = defaultModel;
}
public ChatClientMetadata Metadata => new(
providerName: "CustomModelEndpoint",
providerUri: _httpClient.BaseAddress,
defaultModelId: _defaultModel);
public async Task<ChatResponse> GetResponseAsync(
IEnumerable<ChatMessage> messages,
ChatOptions? options = null,
CancellationToken cancellationToken = default)
{
string modelId = options?.ModelId ?? _defaultModel;
var requestPayload = new CustomModelRequest
{
Model = modelId,
Temperature = options?.Temperature ?? 0.2f,
MaxTokens = options?.MaxOutputTokens ?? 512,
Messages = messages.Select(m => new CustomMessageDto
{
Role = m.Role.Value,
Content = m.Text ?? string.Empty
}).ToList()
};
using HttpResponseMessage response = await _httpClient.PostAsJsonAsync(
"/v1/custom-chat",
requestPayload,
cancellationToken);
response.EnsureSuccessStatusCode();
CustomModelResponse? result = await response.Content
.ReadFromJsonAsync<CustomModelResponse>(cancellationToken);
string content = result?.ResponseText ?? string.Empty;
var assistantMessage = new ChatMessage(ChatRole.Assistant, content);
return new ChatResponse(assistantMessage)
{
ModelId = modelId,
Usage = new UsageDetails
{
InputTokenCount = result?.InputTokens,
OutputTokenCount = result?.OutputTokens,
TotalTokenCount = (result?.InputTokens ?? 0) + (result?.OutputTokens ?? 0)
}
};
}
public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IEnumerable<ChatMessage> messages,
ChatOptions? options = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
// Replace with chunked/SSE reading if your custom endpoint streams tokens incrementally.
ChatResponse response = await GetResponseAsync(messages, options, cancellationToken);
yield return new ChatResponseUpdate(ChatRole.Assistant, response.Text)
{
ModelId = response.ModelId
};
}
public object? GetService(Type serviceType, object? serviceKey = null) =>
serviceKey is null && serviceType.IsInstanceOfType(this) ? this : null;
public void Dispose()
{
// HttpClient lifetime is managed by IHttpClientFactory.
}
private sealed class CustomModelRequest
{
[JsonPropertyName("model")]
public string Model { get; set; } = string.Empty;
[JsonPropertyName("temperature")]
public float Temperature { get; set; }
[JsonPropertyName("max_tokens")]
public int MaxTokens { get; set; }
[JsonPropertyName("messages")]
public List<CustomMessageDto> Messages { get; set; } = [];
}
private sealed class CustomMessageDto
{
[JsonPropertyName("role")]
public string Role { get; set; } = string.Empty;
[JsonPropertyName("content")]
public string Content { get; set; } = string.Empty;
}
private sealed class CustomModelResponse
{
[JsonPropertyName("response_text")]
public string? ResponseText { get; set; }
[JsonPropertyName("input_tokens")]
public int? InputTokens { get; set; }
[JsonPropertyName("output_tokens")]
public int? OutputTokens { get; set; }
}
}
Join the conversation! Your thoughts help the community grow.