{"id":2404,"date":"2022-08-30T15:24:35","date_gmt":"2022-08-30T15:24:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/05\/signalr-mef-and-dependency-injection-working-together-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:24:35","modified_gmt":"2022-08-30T15:24:35","slug":"signalr-mef-and-dependency-injection-working-together-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/signalr-mef-and-dependency-injection-working-together-collection-of-common-programming-errors\/","title":{"rendered":"SignalR, MEF and Dependency Injection working together-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to get SignalR, MEF, and MVC working together.<\/p>\n<p>Here is my resolver class:<\/p>\n<pre><code>public class SignalRDependencyResolver : DefaultDependencyResolver\n{\n    private readonly CompositionContainer _container;\n\n    public SignalRDependencyResolver(CompositionContainer container)\n    {\n        _container = container;\n    }\n\n    public override object GetService(Type serviceType)\n    {\n        if (serviceType == null)\n        {\n            throw new ArgumentNullException(\"serviceType\");\n        }\n\n        var name = AttributedModelServices.GetContractName(serviceType);\n        var export = _container.GetExportedValueOrDefault(name);\n        return export;\n    }\n\n    public override IEnumerable GetServices(Type serviceType)\n    {\n        if (serviceType == null)\n        {\n            throw new ArgumentNullException(\"serviceType\");\n        }\n\n        var exports = _container.GetExportedValues(AttributedModelServices.GetContractName(serviceType));\n        return exports;\n    }\n}\n<\/code><\/pre>\n<p>And here is my Owin startup:<\/p>\n<pre><code>public class Startup\n{\n    public void Configuration(IAppBuilder app)\n    {\n        var resolver = new SignalRDependencyResolver(MefConfigWeb.Container);\n        var config = new HubConfiguration\n        {\n            Resolver = resolver,\n        };\n\n        app.MapSignalR(config);\n    }\n}\n<\/code><\/pre>\n<p>I can break on Startup class and see that the container does include my exports. However, Visual Studio prompts with an exception:<\/p>\n<blockquote>\n<p>Unhandled exception at line 244, column 13 in <code>http:\/\/localhost:3928\/Scripts\/jquery.signalR-2.0.0.js<\/code><\/p>\n<p>0x800a139e &#8211; JavaScript runtime error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. .<\/p>\n<\/blockquote>\n<p>The app works fine if I don&#8217;t set my custom dependency resolver and New up my dependency inside the Hub class.<\/p>\n<p>I am following along from this tutorial: http:\/\/www.asp.net\/signalr\/overview\/signalr-20\/extensibility\/dependency-injection<\/p>\n<p>Any ideas why this is not working?<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-05 22:56:05. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to get SignalR, MEF, and MVC working together. Here is my resolver class: public class SignalRDependencyResolver : DefaultDependencyResolver { private readonly CompositionContainer _container; public SignalRDependencyResolver(CompositionContainer container) { _container = container; } public override object GetService(Type serviceType) { if (serviceType == null) { throw new ArgumentNullException(&#8220;serviceType&#8221;); } var name = AttributedModelServices.GetContractName(serviceType); var export = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2404","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2404"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2404\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}