剛拜讀 @Learning hard 的 [Asp.net 開發系列之SignalR篇]專題一:Asp.net SignalR快速入門
跟着博文一步步操作,這是新人的學習方式
然而筆者的開發環境和 @Learning hard 的有所不同,導致出現了一些小的問題!
首先,新建了一個MVC環境
筆者環境 VS2013 + .net 4.5 + MVC 4.0
新建項目選擇的是 Internet 應用程序,為了支持 SignalR,使用 NuGet 控制台往項目中安裝了 SignalR (這些都是系統默認,並沒有更改所謂的身份驗證,其實我也並不知道@Learning hard 及網絡上所說的身份認證是什么)
Install-Package Microsoft.AspNet.SignalR
然后,就是需要添加的主體
新建繼承 Hub 類的 【SignalR集線器(v2)】類文件,創建一個業務邏輯需要的方法,本例中主要應用到的方法:
// 調用所有客戶端的sendMessage方法 Clients.All.sendMessage(name, message);
最后,頁面中實例化 SignalR 類,做具體業務邏輯
代碼可以參考原文中給出的部分 【原文】
@section scripts { <!--引用SignalR庫. --> <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script> <!--引用自動生成的SignalR 集線器(Hub)腳本.在運行的時候在瀏覽器的Source下可看到 --> <script src="~/signalr/hubs"></script> <script> $(function () { // 引用自動生成的集線器代理 var chat = $.connection.serverHub; // 定義服務器端調用的客戶端sendMessage來顯示新消息 chat.client.sendMessage = function (name, message) { // 向頁面添加消息 $('#discussion').append('<li><strong>' + htmlEncode(name) + '</strong>: ' + htmlEncode(message) + '</li>'); }; // 設置焦點到輸入框 $('#message').focus(); // 開始連接服務器 $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // 調用服務器端集線器的Send方法 chat.server.send($('#message').val()); // 清空輸入框信息並獲取焦點 $('#message').val('').focus(); }); }); }); // 為顯示的消息進行Html編碼 function htmlEncode(value) { var encodedValue = $('<div />').text(value).html(); return encodedValue; } </script> }
其他
控制器和視圖的添加部分就不說了,看看 MVC 入門基本上就會了。
問題來了!
The following errors occurred while attempting to load the app. - No assembly found containing an OwinStartupAttribute. - No assembly found containing a Startup or [AssemblyName].Startup class. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.
運行之后,應該不少朋友有這樣的報錯(可能是中文的錯誤提示)!
看着錯誤提示,其實直接就知道大概什么情況了,OwinStartupAttribute 沒有找到!
緊跟着提示了兩種做法,也就是這兩種做法誤導了初學者!
我們按照提示,於是在網上搜索得到的做法是:
<appSettings> <add key="owin:AutomaticAppStartup" value="false" /> </appSettings>
嘿嘿,你還真別看,居然就真的不報錯了,但是下面的問題更大了,因為世界的業務邏輯不行啊,怎么回事,調試發現,瀏覽到業務所在頁面,報錯:
GET http://localhost:10292/signalr/hubs 404 (Not Found)
Uncaught TypeError: Cannot read property 'client' of undefined
嘿嘿,這個又是什么……到網上搜了很多,最后搜到: https://github.com/SignalR/SignalR/issues/2649
最后的 @Xiaohongt 回復中給出了正確方法,同時也是官方使用 SignalR 步驟:
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
其中有一步驟:
6. In Solution Explorer, right-click the project, then click Add | OWIN Startup Class. Name the new class Startup and click OK.
Note: If you are using Visual Studio 2012, the OWIN Startup Class template will not be available. You can add a plain Class called Startup instead.
然后給出了代碼(注意紅色):
using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(SignalRChat.Startup))] namespace SignalRChat { public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here app.MapSignalR(); } } }
果然,當添加完這樣的類之后,問題解決了,業務功能實現了(去掉之前web.config中加入的錯誤代碼)!
結束言
是的是的,問題解決了,然而……什么情況的,於是乎,就有了下面的結束語:
Owin:Open Web Interface for .NET
支持 Owin 的Web框架有:
更多參見: http://kb.cnblogs.com/page/509236/
每一個使用的OWin組件的Web框架都需要一個StartUp入口類,用來聲明OWin組件:
1.項目會自動掃描程序集根下的名為StartUp的類作為入口類
2.通過添加 [assembly: OwinStartup(typeof(SignalRChat.Startup))] 標簽標記入口類
3.如果你的啟動類不在當前命名空間下 <add key="owin:AppStartup" value="[NameSpace].Startup" />
4.由於第一點的存在,所以如果有名為StartUp的類,並且不是入口點,需要使用 <add key="owin:AutomaticAppStartup" value="false" />
如果有什么錯誤的地方,請園友指出,以防誤導新人,謝謝!