ASP.NET 4.5 Webサイトでクライアントからサーバーを呼び出すときのSignalRエラー500
-
11-12-2019 - |
質問
ASP.NET 4.5 ウェブフォームで signal を使用しています。私はクライアントをサーバーと話すことができず、その逆も同様でした。私が達成したいのは、クライアントがサーバー機能をトリガーできる方法と、サーバーがクライアント機能をトリガーできる方法をテストすることができます。これが私が使うコードです:
クライアントサイドコード(HitCounter.aspx)
.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script>
<script src="Scripts/jquery.signalR-0.5.3.min.js"></script>
<script type="text/javascript" src="SignalR/Hubs"></script>
<style>
#currentHitCount {
font-family: Arial;
font-size:40pt;
margin-left:auto;
margin-right:auto;
display:block;
text-align:center;
}
</style>
</head>
<body>
<div id="currentHitCount"></div>
<script type="text/javascript">
$(function () {
var hub = $.connection.hitCounter;
$.extend(hub, {
showHitCount: function (hitCount){
if(hitCount > 1){
$('#currentHitCount')
.html("This site has had " + hitCount + " hits.");
}
else{
$('#currentHitCount')
.html("This site has had " + hitCount + " hit.");
}
},
addMessage: function (str) {
$('#currentHitCount')
.html("Getting Message: " + str);
}
});
$.connection.hub.start(function () {
hub.addMessage("test");
hub.addHit();
});
$.connection.hub.stateChanged(function (change) {
if ($.signalR.connectionState["connected"] === change.newState) {
}
});
$.connection.hub.error(function () {
});
});
</script>
<div style="background-color:red; width:290px; height:200px; color:white; position:absolute; top:100px; left:30px;" id="thebutton">test</div>
</body>
</html>
サーバーサイドコード(APP_CODE / HITCOUNTERHUB.CS)
.
using SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
[HubName("hitCounter")]
public class HitCounterHub : Hub, IDisconnect
{
static int _hitCount;
public void addHit(string pageId)
{
_hitCount += 1;
Clients.showHitCount(_hitCount);
}
public Task ClientCallingServer()
{
_hitCount += 1;
return Clients["foo"].showHitCount(_hitCount);
}
public Task Join()
{
return Groups.Add(Context.ConnectionId, "foo");
}
public Task Send(string message)
{
_hitCount += 1;
return Clients["foo"].addMessage(message);
}
public Task Disconnect()
{
return Clients["foo"].leave(Context.ConnectionId);
}
}
私はIIS7.5でローカルにコードを実行していますが、Visual Studio 2012でコードを実行するときに取得してエラーが発生します。
エラー:
localhost / signalr / signal / send?transport= serverSentEvents&ConnectionID= 400F1302-6C8E-418E-A14C-DA95F836A29D 500(内部サーバーエラー)
Chromeデバッガの使用エラーページには、次のように表示されます。 'addhit'メソッドは解決できませんでした。
再び、私がやろうとしているのは、サーバーからサーバーとクライアントからサーバーを呼び出す方法を確認するための簡単なテストを行うことです。
ありがとう。
解決
addHitメソッドを解決できない理由は、クライアントを実行するときにサーバーに同じ機能名があるためです。
AKA Hub.AdDhitを実行しているサーバーを呼び出す( "laksjdf")。しかしクライアントに電話するには、同じことになります.Hub.AdDhit( "laksjdfldsj");それゆえあいまいさがあります。クライアントまたはサーバー側の関数の名前を変更して、それらが命名意味でユニークであるように変更します。
この問題はSignalRの次のリリースで修正されます。