ไคลเอนต์ SignalR ใน asp.net

ฉันสร้างฮับเซิร์ฟเวอร์ในแอปพลิเคชัน asp.net เช่นด้านล่าง

public class Calc : Hub
{
    public void CalculateSomething(int a, int b)
    {
        // start working in a new thread
        var task = Task.Factory.StartNew(() => DoCalculate(a, b));

        // attach a continuation task to notify
        // the client when the work is done
        task.ContinueWith(t =>
        {
            System.Threading.Thread.Sleep(2000);
            Clients.addMessage(t.Result);
            Caller.notifyCalculateResult(t.Result);
            System.Threading.Thread.Sleep(2000);
            Caller.notifyCalculateResult("Completed");
            Clients.addMessage("Completed");
        });
    }

    private int DoCalculate(int p1, int p2)
    {
        // do some slow work on the input,
        // e.g. call webservice or I/O.
        int result = p1 + p2;
        //int result = DoSlowWork(p1, p2);
        return result;
    }
}

ตอนนี้ในแอปพลิเคชัน asp.net อื่น ฉันสร้างไคลเอนต์โดยใช้ไคลเอนต์ SiganlR แต่มันทำงานไม่ถูกต้อง ฉันต้องการรับข้อมูลจากเซิร์ฟเวอร์ในขณะที่ส่งไปยังไคลเอนต์

using System.Threading.Tasks;
using SignalR;
using SignalR.Client;
using SignalR.Client.Hubs;
namespace WebApplication2
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Connect to the service
            var hubConnection = new HubConnection("http://localhost:3119/");

            // Create a proxy to the chat service
            var chat = hubConnection.CreateProxy("Calc");

            // Print the message when it comes in
            chat.On("addMessage", message =>Print(message));

            // Start the connection
            hubConnection.Start().Wait();

            // Send a message to the server
            chat.Invoke("CalculateSomething", 1, 2).Wait();
        }

        private async void Print(object message)
        {
            Response.Write(message);
        }
    }
}

แอปพลิเคชันไคลเอนต์คอนโซลทำงานได้ดี ปัญหาหลักอยู่ที่ asp.net เนื่องจากไม่สามารถจัดการการโทรกลับจากเซิร์ฟเวอร์ได้


person Vivek    schedule 19.10.2012    source แหล่งที่มา


คำตอบ (1)


ดูเหมือนว่าคุณเรียกเมธอดฝั่งเซิร์ฟเวอร์ผิด ลองทำเช่นนี้

chat.Invoke("CalculateSomething", 1, 2).ContinueWith(task =>
{ 
  Console.WriteLine("Value from server {0}", task.Result);
});
person Rafeeq    schedule 23.10.2012