Bot Framework:ActivityTypes.Typing无法正常工作求助
Hey Kevin, sorry to hear you're stuck on this seemingly simple yet frustrating issue—null reference errors are always a pain when you expect something to just work. Let's walk through the most common causes and fixes for this problem:
Common Causes & Fixes
1. Your ITurnContext instance is null
This is the most frequent culprit. The typing activity relies on a valid turn context to know who to send the indicator to, which conversation it belongs to, etc.
- Check where you're sending the typing activity: If you're trying to send it outside of the bot's turn processing pipeline (like in a background task or a custom service without passing the context), the
turnContextvariable will be null. - Fix: Always send typing activities within methods that receive a valid
ITurnContext(likeOnMessageActivityAsyncorOnTurnAsync). If you need to send it from a separate service, make sure to pass the context through properly.
2. You're manually constructing the typing activity incorrectly
If you're creating the Activity object from scratch instead of using the built-in helper method, you might be missing required properties (like ConversationId, Recipient, or From), which leads to a null reference when the framework tries to process it.
- Bad practice:
var badTypingActivity = new Activity { Type = ActivityTypes.Typing }; await turnContext.SendActivityAsync(badTypingActivity); // Missing context properties = error - Fix: Use the static
Activity.CreateTypingActivity()method—it automatically populates all necessary context-related properties from the turn context:await turnContext.SendActivityAsync(Activity.CreateTypingActivity(), cancellationToken);
3. Issues with dependency injection or adapter initialization
If your bot's adapter or context isn't being properly registered or injected, the context instance might be null when you try to use it.
- Check your startup configuration: Ensure you've correctly registered your bot and adapter in your dependency injection container. For example, in a .NET bot:
// In Startup.cs services.AddBot<YourBotClass>(options => { options.CredentialProvider = new SimpleCredentialProvider(appId, appPassword); // Other configuration... }); - Verify context is passed correctly: Make sure your bot's handlers are receiving a non-null
ITurnContextfrom the adapter.
Working Example
Here's a complete, error-free snippet of sending a typing activity in a v4 bot:
using Microsoft.Bot.Builder; using Microsoft.Bot.Schema; using System.Threading; using System.Threading.Tasks; public class EchoBot : ActivityHandler { protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) { // Send typing indicator first await turnContext.SendActivityAsync(Activity.CreateTypingActivity(), cancellationToken); // Simulate some processing time await Task.Delay(1500); // Send the actual response await turnContext.SendActivityAsync(MessageFactory.Text($"You said: {turnContext.Activity.Text}"), cancellationToken); } }
Start by verifying the turnContext isn't null at the point you're sending the typing activity—this will eliminate the most obvious cause. If that checks out, switch to using the CreateTypingActivity() helper method, and you should be good to go!
内容的提问来源于stack exchange,提问作者Kevin D.

