Introduction
The integration of AI into enterprise software has moved beyond chatbots and recommendation systems — it’s now about embedding reasoning, automation, and contextual intelligence directly into your application workflows.
Two powerful frameworks — LangChain and Semantic Kernel (SK) — make this possible. Both empower .NET developers to connect LLMs (Large Language Models) like GPT with custom business data, workflows, and APIs.
In this article, we’ll explore how to integrate LangChain and Semantic Kernel with ASP.NET Core, explaining their architecture, use cases, and implementation — all in a simple, educational, step-by-step way.
1. Why Integrate AI with ASP.NET Core
Modern enterprise APIs built with ASP.NET Core are excellent for performance, scalability, and modular design. But they lack one thing: contextual intelligence.
By integrating LLM frameworks such as LangChain or Semantic Kernel, you can enhance APIs to:
Generate smart summaries or insights from data
Automate decision-making based on rules and AI reasoning
Understand and process natural language queries
Build conversational interfaces backed by your business data
2. What Are LangChain and Semantic Kernel?
LangChain (for Python/JS/.NET)
LangChain is a framework designed to connect LLMs with external data sources and tools.
It provides modules for:
Prompt engineering
Memory (context retention)
Tool use (API calling, data retrieval)
Chaining — connecting model outputs into a reasoning pipeline
Semantic Kernel (for .NET, Python, Java)
Semantic Kernel is Microsoft’s open-source orchestration framework for integrating AI into applications.
It supports:
Semantic Functions (prompts + LLMs)
Native Functions (C# logic)
Memory storage (Redis, Cosmos DB, etc.)
Planner — automatically decides which function to execute next
3. Technical Workflow (Flowchart)
Below is a simple technical workflow showing how AI is integrated into an ASP.NET Core API:
+-------------------------+
| Client (Angular App) |
+-----------+-------------+
|
| HTTP Request (user query)
v
+-----------+-------------+
| ASP.NET Core API |
|-------------------------|
| Controller / Endpoint |
| Semantic Kernel Setup |
| LangChain Agent Logic |
+-----------+-------------+
|
| LLM Request (via OpenAI / Azure OpenAI)
v
+-----------+-------------+
| LLM Model (GPT, etc.) |
+-----------+-------------+
|
| Response (reasoned output)
v
+-----------+-------------+
| ASP.NET Core Response |
+-----------+-------------+
|
v
+-----------+-------------+
| Angular / UI Component |
+-------------------------+
4. Setting Up Semantic Kernel in ASP.NET Core
Step 1: Install Required Packages
dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Connectors.AI.OpenAI
Step 2: Configure Semantic Kernel
Program.cs
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
var builder = WebApplication.CreateBuilder(args);
// Register Semantic Kernel
builder.Services.AddSingleton<IKernel>(sp =>
{
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
modelId: "gpt-4o-mini",
apiKey: builder.Configuration["OpenAI:ApiKey"]
)
.Build();
return kernel;
});
var app = builder.Build();
app.MapControllers();
app.Run();
5. Creating a Semantic Function
Semantic functions are AI-powered skills defined in .skprompt.txt files or inline.
Example: Summarize Customer Feedback
using Microsoft.SemanticKernel;
[ApiController]
[Route("api/[controller]")]
public class FeedbackController : ControllerBase
{
private readonly IKernel _kernel;
public FeedbackController(IKernel kernel)
{
_kernel = kernel;
}
[HttpPost("summarize")]
public async Task<IActionResult> SummarizeFeedback([FromBody] string feedback)
{
var prompt = @"Summarize this customer feedback in 3 key points:
{{$input}}";
var summarize = _kernel.CreateSemanticFunction(prompt);
var result = await summarize.InvokeAsync(feedback);
return Ok(result.GetValue<string>());
}
}
Request Example
POST /api/feedback/summarize
Body: "The product works fine but delivery was late. Support was slow but helpful."
Response
1. Product quality is satisfactory.
2. Delivery delays affected user satisfaction.
3. Customer support was responsive but slow.
6. Integrating LangChain with ASP.NET Core
While LangChain.NET is still maturing, you can use LangChainJS through a Node.js microservice or Python REST bridge to interact with .NET APIs.
Example Architecture
ASP.NET Core → Calls → LangChainJS API → LLM Reasoning → Returns to .NET
LangChainJS Example (Express Server)
import express from 'express';
import { ChatOpenAI } from "langchain/chat_models/openai";
import { LLMChain } from "langchain/chains";
import { PromptTemplate } from "langchain/prompts";
const app = express();
app.use(express.json());
app.post('/api/ai/summary', async (req, res) => {
const model = new ChatOpenAI({ modelName: "gpt-4o-mini", temperature: 0.7 });
const prompt = new PromptTemplate({
template: "Summarize the following text:\n{text}",
inputVariables: ["text"]
});
const chain = new LLMChain({ llm: model, prompt });
const result = await chain.call({ text: req.body.text });
res.send(result.text);
});
app.listen(3001, () => console.log("LangChain API running"));
Then your ASP.NET Core API calls this service:
var client = new HttpClient();
var response = await client.PostAsJsonAsync("http://localhost:3001/api/ai/summary", new { text });
var result = await response.Content.ReadAsStringAsync();
7. Use Cases for LangChain / Semantic Kernel in ASP.NET Core
| Use Case | Description |
|---|
| Document Summarization | Summarize large reports or emails dynamically. |
| Natural Language Querying | Convert plain English to SQL queries or filters. |
| Intelligent Agents | Automate reasoning workflows (e.g., ticket triage). |
| Email Drafting | Generate contextual messages based on templates. |
| AI-Powered Search | Retrieve and re-rank knowledge base articles. |
8. Comparing LangChain and Semantic Kernel
| Feature | LangChain | Semantic Kernel |
|---|
| Language | JS / Python / .NET (partial) | .NET / Python / Java |
| Focus | Chained LLM reasoning | Semantic + Native orchestration |
| Integrations | Extensive toolkits | Tight Microsoft ecosystem |
| Memory & Context | Advanced | Structured memory support |
| Ideal For | Complex agent workflows | Enterprise .NET applications |
Recommendation:
If your backend is in ASP.NET Core, Semantic Kernel offers the smoothest integration and enterprise security alignment.
9. Best Practices
Secure API Keys: Use Azure Key Vault or environment variables.
Rate Limiting: Protect your AI endpoints using ASP.NET Core middleware.
Logging: Store prompt and response logs for debugging.
Caching: Use Redis for repeated AI responses.
Hybrid Approach: Combine SK for orchestration and LangChain for reasoning.
10. Conclusion
Integrating LangChain or Semantic Kernel into ASP.NET Core transforms static APIs into intelligent, conversational, and context-aware systems.
Semantic Kernel fits best for enterprise-grade, secure .NET apps — while LangChain is ideal for experimental or cross-language AI workflows.