基于Microsoft Bot Framework(C#)本地IIS部署后的机器人测试方法咨询
Got it, let’s walk through practical, actionable ways to test your IIS-deployed Bot Framework bot using Direct Line—no Azure required. These methods will help you confirm your bot is responding correctly and ready to embed into your own site:
This is a great way to validate basic connectivity and message flow directly from code, matching your bot’s tech stack:
- Create a new Console App (.NET Framework or .NET Core) in Visual Studio
- Install the
Microsoft.Bot.Connector.DirectLineNuGet package - Replace the default code with this sample (update the bot URL and user ID as needed):
using System; using System.Threading.Tasks; using Microsoft.Bot.Connector.DirectLine; namespace DirectLineBotTest { class Program { static async Task Main(string[] args) { // Replace with your bot's IIS URL (e.g., http://your-local-ip/api/directline) var directLineClient = new DirectLineClient("<your-bot-iis-url>/api/directline"); // Generate a token for the test user var tokenResponse = await directLineClient.Tokens.GenerateTokenForNewUserAsync("test-user-001"); directLineClient.Authorization = $"Bearer {tokenResponse.Token}"; // Start a new conversation var conversation = await directLineClient.Conversations.StartConversationAsync(); // Send a test message to the bot await directLineClient.Conversations.PostActivityAsync(conversation.ConversationId, new Activity { Type = ActivityTypes.Message, From = new ChannelAccount("test-user-001"), Text = "Hello, bot!" }); // Poll for the bot's response Console.WriteLine("Waiting for bot response..."); var activities = await directLineClient.Conversations.GetActivitiesAsync(conversation.ConversationId); foreach (var activity in activities.Activities) { if (activity.From.Name == "<your-bot-name>" && activity.Type == ActivityTypes.Message) { Console.WriteLine($"Bot replied: {activity.Text}"); } } Console.ReadLine(); } } }
- Run the console app—if you see the bot’s reply printed, your deployment is working.
If you prefer working with HTTP requests directly, you can test the Direct Line flow manually:
Step 1: Get a Direct Line Token
Send a POST request tohttp://<your-bot-iis-domain>/api/directline/tokens/generate- Headers:
Content-Type: application/json - Body (optional):
{"user": {"id": "test-user-002"}} - You’ll get a JSON response with a
tokenfield—save this.
- Headers:
Step 2: Start a Conversation
Send a POST request tohttps://directline.botframework.com/v3/directline/conversations- Headers:
Authorization: Bearer <your-token>,Content-Type: application/json - Save the
conversationIdfrom the response.
- Headers:
Step 3: Send a Message to the Bot
Send a POST request tohttps://directline.botframework.com/v3/directline/conversations/<conversationId>/activities- Headers: Same as Step 2
- Body:
{ "type": "message", "from": {"id": "test-user-002"}, "text": "What's your status?" }
Step 4: Fetch the Bot’s Reply
Send a GET request tohttps://directline.botframework.com/v3/directline/conversations/<conversationId>/activities- Headers: Same as Step 2
- Look for activities where
from.idmatches your bot’s ID—this will be the bot’s response.
To simulate the exact experience your site visitors will have, create a simple test HTML page with the Bot Framework Web Chat control:
<!DOCTYPE html> <html> <head> <title>Bot Web Chat Test</title> <script src="https://cdn.botframework.com/botframework-webchat/latest/webchat.js"></script> <style> body { height: 100vh; margin: 0; } #webchat { height: 100%; width: 100%; } </style> </head> <body> <div id="webchat" role="main"></div> <script> async function startWebChat() { // Fetch a token from your locally deployed bot const tokenResponse = await fetch('http://<your-bot-iis-domain>/api/directline/tokens/generate', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ user: { id: "webchat-test-user" } }) }); const { token } = await tokenResponse.json(); // Render the Web Chat control window.WebChat.renderWebChat({ directLine: window.WebChat.createDirectLine({ token }), userID: "webchat-test-user" }, document.getElementById('webchat')); } startWebChat().catch(err => console.error("Web Chat error:", err)); </script> </body> </html>
- Save this as
test-chat.html, then host it locally (use VS Code’s Live Server extension or add it to your IIS site) - Open the page in a browser—you should be able to chat with your bot directly, just like on your future production site.
If tests fail, check your IIS logs to diagnose issues:
- In IIS Manager, navigate to your bot’s site > Logging
- The log file path is shown here (typically
C:\inetpub\logs\LogFiles\W3SVC<site-id>) - Open the latest log file and look for HTTP 500 errors or failed requests—this will help you spot issues like missing dependencies or incorrect .NET runtime configuration.
A quick note: Make sure your IIS application pool is configured to use the correct .NET version matching your bot (e.g., .NET 6 for modern bots, set the pool to "No Managed Code" if using self-contained deployment). Also, use your machine’s local IP address instead of localhost when testing from other devices or tools to avoid connectivity issues.
内容的提问来源于stack exchange,提问作者Maha

