TinyIocContainer我有點新NancyFx和TinyIoc,但在這裏不用signalr樞紐依賴性:如何註冊使用內NancyFx
我已經能夠使用內NanxyFx TinyIocContainer的汽車登記在南希注入依賴非常整潔的模塊,這一切都很好,工作。
但是,你如何註冊一個像下面這樣的信號集線器的依賴關係?
我不斷地從signalr得到經典的錯誤消息:
No parameterless constructor defined for this object.
我的設置:
我有一個SignalR集線器:
[Hubname("chathub")]
public class ChatHub: Hub {
private IChat _chat;
public ChatHub(Chat chat){
_chat = chat;
}
}
這是我的依賴關係:
public class Chat: IChat{
public Chat(IHubConnectionContext clients, IChatRepo repo){
...
}
}
我創建了一個CustomBootstrapper類:
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
// I'm hoping the registration below will resolve for the IHubConnectionContext parameter for the Chat class
container.Register(GlobalHost.ConnectionManager.GetHubContext<ChatHub>().Clients);
}
}
我有一個啓動類:
public class Startup{
var hubConfiguration = new HubConfiguration { EnableDetailedErrors = true};
app.MapSignalR("/signalr", hubConfiguration);
}
如果我對ChatHub參數的構造函數,並使用差芒Ioc的,一切工作正常。
我很確定我錯過了一些東西。
任何想法?
感謝您的文章。 - 我剛剛創建了一個自定義DependencyResolver,但我不完全確定設置瞭解析器。例如在文章中,它通過HubConfiguration設置客戶解析器。在Nancy內部,我有一個CustomBootstrapper,它繼承自DefaultNancyBootstrapper和ApplicationStartup重載上的I,我是否通過如下方式設置Resolver:GlobalHost.DependencyResolver = new TinyIoCDependencyResolver(container)?我已經嘗試了兩種,但無濟於事。 –
不知道是否爲了簡潔而忽略它,但您的Startup類沒有按照上面鏈接的文章的「配置(IAppBuilder應用程序)」方法。不知道是否與您的棧Owin仍然參與,但在我的MVC5應用程序還有以下彙編指令: '[彙編:OwinStartup(typeof(Namespace.Of.Startup))]]' – Shaun