IServerSideEvents 的 IOC 注入 [英] IOC injection of IServerSideEvents

查看:29
本文介绍了IServerSideEvents 的 IOC 注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的 IOC 编写单元测试.我的一个接口注入了 IServerEvents.

I am writing unit tests for my IOC. One of my interfaces injects IServerEvents.

我通过以下方式包括事件:

I am including events via:

  ServerEventsFeature serverEventsFeature = new ServerEventsFeature()
            {
                LimitToAuthenticatedUsers = false,
                NotifyChannelOfSubscriptions = false,
                OnConnect = (eventSubscription, dictionary) =>
                {
                },
                OnSubscribe = (eventSubscription) =>
                {
                }
            };

但是,container.Resolve 在调试时出现以下错误(不是通过单元测试):

However, container.Resolve gives the following error when debugging (Not through unit tests) :

'container.Resolve<IServerEvents>()' threw an exception of type 'System.Exception'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233088
    HelpLink: null
    InnerException: {System.InvalidOperationException: No service for type 'ServiceStack.IServerEvents' has been registered.
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing)}
    Message: "Error trying to resolve Service 'ServiceStack.IServerEvents' or one of its autowired dependencies (see inner exception for details)."
    Source: "ServiceStack"
    StackTrace: "   at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing)"
    TargetSite: {TService ResolveImpl[TService](System.String, Boolean)}

这在正常使用中确实有效,但是界面的手动解析不起作用.

This does work in normal usage, but manual Resolve of the interface does not work.

我想知道的是:

A) 对这种服务器事件集成进行单元测试的正确方法

A) proper way to unit test this integration of server events

B) 我应该在单元测试中使用 RegisterAs<>() 来模拟容器上的 IServerEvents

B) Should I merely mock the IServerEvents on the container with a RegisterAs<>() in unit tests

C) 为什么注入工作正常但 container.Resolve() 失败.

C) Why the injection works fine but container.Resolve() fails.

感谢任何反馈.

推荐答案

您不能在单元测试中使用 ServerEventsFeature,您只能在 集成测试.您可以在 ServerEventTests 中找到一些工作示例.

You can't use ServerEventsFeature in a unit test, you'll only be able to make use of it in an integration test. You can find some working examples in ServerEventTests.

由于ServerEventsFeature是一个插件,需要注册为插件才能运行:

As ServerEventsFeature is a plugin, it needs to be registered as a plugin to function:

Plugins.Add(new ServerEventsFeature { ... });

插件注册后会rel="nofollow noreferrerMemoryServerEvents 依赖:

Which when the plugin is registered will register the MemoryServerEvents dependency:

container.Register<IServerEvents>(memoryServerEvents);

这篇关于IServerSideEvents 的 IOC 注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆