You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于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:

1. Test with a C# Direct Line Client Console App

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.DirectLine NuGet 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.
2. Use Postman/curl for REST API Testing

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 to http://<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 token field—save this.
  • Step 2: Start a Conversation
    Send a POST request to https://directline.botframework.com/v3/directline/conversations

    • Headers: Authorization: Bearer <your-token>, Content-Type: application/json
    • Save the conversationId from the response.
  • Step 3: Send a Message to the Bot
    Send a POST request to https://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 to https://directline.botframework.com/v3/directline/conversations/<conversationId>/activities

    • Headers: Same as Step 2
    • Look for activities where from.id matches your bot’s ID—this will be the bot’s response.
3. Embed a Web Chat Control for End-to-End Testing

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.
4. Troubleshoot with IIS Logs

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 14:19:08