C# Bot Framework Autofac DI-使用context.Call()时传递依赖项

C# Bot Framework Autofac DI-使用context.Call()时传递依赖项,c#,autofac,botframework,C#,Autofac,Botframework,我一直在玩弄bot框架,但我只是想知道在使用context.Call()时我对Autofac的使用是否正确。我应该像下面这样将评级服务依赖性从RootDialog传递到ReviewDialog,还是有更好的方法 context.Call(new ReviewDialog(_ratingService), ChildDialogHasCompleted); 消息控制器 [BotAuthentication] public class MessagesController : Ap

我一直在玩弄bot框架,但我只是想知道在使用context.Call()时我对Autofac的使用是否正确。我应该像下面这样将评级服务依赖性从RootDialog传递到ReviewDialog,还是有更好的方法

context.Call(new ReviewDialog(_ratingService), ChildDialogHasCompleted);
消息控制器

    [BotAuthentication]
    public class MessagesController : ApiController
    {
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
                {
                    await Conversation.SendAsync(activity, () => scope.Resolve<IDialog<object>>());
                }
            }

            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }
    }
[BotAuthentication]
公共类消息控制器:ApiController
{
/// 
///帖子:api/Messages
///接收来自用户的消息并回复
/// 
公共异步任务发布([FromBody]活动)
{
if(activity.Type==ActivityTypes.Message)
{
使用(var scope=DialogModule.BeginLifetimeScope(Conversation.Container,activity))
{
wait Conversation.sendaync(活动,()=>scope.Resolve());
}
}
var response=Request.CreateResponse(HttpStatusCode.OK);
返回响应;
}
}
RootDialog

[Serializable]
public class RootDialog : LuisDialog<object>
{
    private IRatingService _ratingService;

    public RootDialog(IRatingService ratingService)
    {
        this._ratingService = ratingService;
    }

    [LuisIntent("Movie")]
    public async Task IntentSearch(IDialogContext context, LuisResult result)
    {
        // Do stuff

        context.Call(new ReviewDialog(_ratingService), ChildDialogHasCompleted);
    }

    private async Task ChildDialogHasCompleted(IDialogContext context, IAwaitable<object> msg)
    {
        context.Done(true);
    }
}
[Serializable]
public class ReviewDialog : IDialog
{
    private IRatingService _ratingService;

    public ReviewDialog(IRatingService ratingService)
    {
        this._ratingService = ratingService;
    }

    public async Task StartAsync(IDialogContext context)
    {
        PromptDialog.Choice(context, ProcessRating, new List<string> { "1", "2", "3", "4", "5" }, "Please select your rating");
    }

    public async Task ProcessRating(IDialogContext context, IAwaitable<string> msg)
    {
        var message = await msg;

        context.UserData.TryGetValue("SelectedMovieId", out int movieId);

        var rating = int.Parse(message);

        _ratingService.Save(movieId, rating);

        await context.PostAsync("Thank you");

        context.Done(true);
    }
}
[可序列化]
公共类根对话框:LuisDialog
{
私人伊拉克服务(ratingService);;
公共根对话框(IRatingService分级服务)
{
这是。_ratingService=ratingService;
}
[路易辛顿(“电影”)]
公共异步任务意图搜索(IDialogContext上下文,LuisResult结果)
{
//做事
调用(新建ReviewDialog(_ratingService),ChildDialogHasCompleted);
}
专用异步任务ChildDialogHasCompleted(IDialogContext上下文,IAwaitable msg)
{
上下文。完成(true);
}
}
查看对话框

[Serializable]
public class RootDialog : LuisDialog<object>
{
    private IRatingService _ratingService;

    public RootDialog(IRatingService ratingService)
    {
        this._ratingService = ratingService;
    }

    [LuisIntent("Movie")]
    public async Task IntentSearch(IDialogContext context, LuisResult result)
    {
        // Do stuff

        context.Call(new ReviewDialog(_ratingService), ChildDialogHasCompleted);
    }

    private async Task ChildDialogHasCompleted(IDialogContext context, IAwaitable<object> msg)
    {
        context.Done(true);
    }
}
[Serializable]
public class ReviewDialog : IDialog
{
    private IRatingService _ratingService;

    public ReviewDialog(IRatingService ratingService)
    {
        this._ratingService = ratingService;
    }

    public async Task StartAsync(IDialogContext context)
    {
        PromptDialog.Choice(context, ProcessRating, new List<string> { "1", "2", "3", "4", "5" }, "Please select your rating");
    }

    public async Task ProcessRating(IDialogContext context, IAwaitable<string> msg)
    {
        var message = await msg;

        context.UserData.TryGetValue("SelectedMovieId", out int movieId);

        var rating = int.Parse(message);

        _ratingService.Save(movieId, rating);

        await context.PostAsync("Thank you");

        context.Done(true);
    }
}
[可序列化]
公共类复习对话:IDialog
{
私人伊拉克服务(ratingService);;
公共评论对话(IRatingService分级服务)
{
这是。_ratingService=ratingService;
}
公共异步任务StartAsync(IDialogContext上下文)
{
PromptDialog.Choice(上下文、处理等级、新列表{“1”、“2”、“3”、“4”、“5”},请选择您的等级);
}
公共异步任务ProcessRating(IDialogContext上下文,IAwaitable msg)
{
var消息=等待消息;
context.UserData.TryGetValue(“SelectedMovieId”,out int movieId);
var评级=int.Parse(消息);
_评级服务。保存(电影ID、评级);
等待上下文。PostAsync(“谢谢”);
上下文。完成(true);
}
}
全球的

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);

        var builder = new ContainerBuilder();

        builder.RegisterType<RootDialog>()
                .As<IDialog<object>>()
                .InstancePerDependency();

        builder.RegisterType<RatingService>()
                .Keyed<IRatingService>(FiberModule.Key_DoNotSerialize)
                .AsImplementedInterfaces();

        builder.Update(Conversation.Container);
    }
}
公共类WebAPI应用程序:System.Web.HttpApplication
{
受保护的无效应用程序\u Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var builder=new ContainerBuilder();
builder.RegisterType()
.As()
.InstancePerDependence();
builder.RegisterType()
.Keyed(光纤模块.Key\u不序列化)
.a实现接口();
builder.Update(Conversation.Container);
}
}

非常感谢您的帮助。

您这样做是完全正确的。要查看其他实现,请签出


您这样做的方式通常也是我在对话框中使用DI的方式。使用上下文
PrivateConversationData
ConversationData
UserData
中的数据包,还有另一种在对话框之间传递数据/类的方法,但这种方法没有错