本文介绍了SignalR错误500在ASP.NET 4.5网站调用客户端从服务器时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 SignalR ASP.NET 4.5 web表单。我是不是能够使客户与服务器,反之亦然。我想实现的是仅仅是能够测试客户端如何能够触发服务器功能,以及如何在服务器可以触发客户端功能。这里的code我使用:

I'm using SignalR with ASP.NET 4.5 webforms. I wasn't able to make the client talk to the server and vice versa. What I want to achieve is simply being able to to test how the Client can trigger a server function and how the server can trigger a client function. Here's the code I use:

客户端code(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>

服务器端code(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);
}
}

我在本地运行code对IIS7.5但我得到并运行在Visual Studio 2012太code时的错误。

I am running the code locally on IIS7.5 but I get and error when running the code in Visual Studio 2012 too.

该错误:

localhost/signalr/signalr/send?transport=serverSentEvents&connectionId=400f1302-6c8e-418e-a14c-da95f836a29d
  500(内部服务器错误)

使用Chrome调试器的错误页面显示:
addHit'方法无法解析。

Using Chrome debugger the error page shows:'addHit' method could not be resolved.

再次什么,我试图做的是做一个简单的测试来检查如何调用从客户端从服务器服务器和客户端。

Again, what I am trying to do is to make a simple test to check how to call a server from the client and the client from the server.

感谢。

推荐答案

为什么你不能够解决你的addHit方法的原因是因为你在服务器上拥有相同的函数名作为你的客户端。

The reason why you're not able to resolve your addHit method is because you have the same function name on the server as you do the client.

AKA要调用你正在做hub.addHit(laksjdf)到服务器。但在客户端调用它会是同一件事:hub.addHit(laksjdfldksj);因此,有歧义。改变或者您的客户端或服务器端函数名称,以便他们在自己的命名意义上是独一无二的。

AKA To call the server you're doing hub.addHit("laksjdf"). But to call the client it'd be the same thing: hub.addHit("laksjdfldksj"); Hence there's ambiguity. Change the name of either your client or server side functions so they're unique in their naming sense.

这个问题将在SignalR的下一个版本。

This issue will be fixed in the next release of SignalR.

这篇关于SignalR错误500在ASP.NET 4.5网站调用客户端从服务器时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 18:25