实例化带有Resolve <interface>的对象

时间:2018-08-28 01:02:47

标签: c# botframework autofac

当我调用await SendAsync(activity, scope => new TransferToAgentDialog(scope.Resolve<IUserToAgent>()))时,出现范围在当前上下文中不存在的错误。范围在初始化新的scope.Resolve<IUserToAgent>类时需要传递TransferToAgentDialog

我尝试初始化范围:

public TransferToAgent scope = new TransferToAgent(scope.Reslove<IUserToAgent>); 

但这带来了一个问题,当初始化scope.Resolve<IUserToAgent>时,您无法通过scope

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        await SendAsync(activity, scope => new TransferToAgentDialog(scope.Resolve<IUserToAgent>()));
    }
    else
    {
        HandleSystemMessage(activity);
    }
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

SendAsync类

private async Task SendAsync(IMessageActivity toBot, Func<ILifetimeScope, IDialog<object>> MakeRoot, CancellationToken token = default(CancellationToken))
{
    using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, toBot))
    {
        DialogModule_MakeRoot.Register(scope, () => MakeRoot(scope));
        var task = scope.Resolve<IPostToBot>();
        await task.PostAsync(toBot, token);
    }
}

1 个答案:

答案 0 :(得分:1)

您可以这样启动范围:

using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
    {
        await SendAsync(activity, () => new TransferToAgentDialog(scope.Resolve<IUserToAgent>()));                       
    }