3

我的理解是,使用信号器,我可以来回发送对象。我正在尝试设置一个 .net 客户端来接收已在网站上放置订单的通知。我正在尝试设置一个非常简单的示例,以便我理解这些概念。当我将字符串通知发送回客户端时效果很好,但是当我尝试发送对象时出现错误:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException was unhandled by user code
  HResult=-2146233088
  Message=The best overloaded method match for 'ConsoleHub.Program.DisplayOrder(ConsoleHub.Order)' has some invalid arguments
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at CallSite.Target(Closure , CallSite , Type , Object )
       at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
       at ConsoleHub.Program.<Main>b__6(Object o) in c:\Working\OrderNotifier\ConsoleHub\Program.cs:line 23
       at Microsoft.AspNet.SignalR.Client.Hubs.HubProxyExtensions.<>c__DisplayClass6`1.<On>b__4(JToken[] args)
       at Microsoft.AspNet.SignalR.Client.Hubs.Subscription.OnData(JToken[] data)
       at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.InvokeEvent(String eventName, JToken[] args)
       at Microsoft.AspNet.SignalR.Client.Hubs.HubConnection.OnReceived(JToken message)
       at Microsoft.AspNet.SignalR.Client.Connection.Microsoft.AspNet.SignalR.Client.IConnection.OnReceived(JToken message)
       at Microsoft.AspNet.SignalR.Client.Transports.HttpBasedTransport.ProcessResponse(IConnection connection, String response, Boolean& timedOut, Boolean& disconnected)
  InnerException: 

我的课:

public class Order
{
    public int OrderId { get; set; }
    public string Name { get; set; }
    public string OrderItem { get; set; }
}

我的枢纽:

using Microsoft.AspNet.SignalR.Hubs;
using OrderNotifier.Models;
using System.Linq;
using System.Threading.Tasks;

namespace OrderNotifier.Hubs
{

    public class NotifierHub : Hub
    {
        OrderContext db = new OrderContext();

        public void Hello()
        {
            Clients.Caller.Welcome("hello");
        }

    }
}

我的控制器动作:

    [HttpPost]
    public ActionResult Create(Order order)
    {
        if (ModelState.IsValid)
        {
            db.Orders.Add(order);
            db.SaveChanges();

            SendNotifier.SendOrderNotification(String.Format("{0} ordered {1}", order.Name, order.OrderItem), order);
            return RedirectToAction("Index");
        }

        return View(order);
    }

SendNotifier - 这有点奇怪,因为我让它同时发送一个字符串版本和一个对象版本进行测试:

public class SendNotifier
{
    public static void SendOrderNotification(string message, Order order)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<NotifierHub>();
        context.Clients.All.Notify(message);
        context.Clients.All.Order(order);
    }
}

还有我的控制台应用程序:

using Microsoft.AspNet.SignalR.Client.Hubs;
using OrderNotifier.Models;
using System;


namespace ConsoleHub
{
    class Program
    {
        static void Main(string[] args)
        {
            var hubConnection = new HubConnection("http://localhost:60692");

            var order = hubConnection.CreateHubProxy("NotifierHub");

            //
            // Set up action handlers
            //
            order.On("Welcome", message => Console.WriteLine(message));
            order.On("Notify", message => Console.WriteLine(message));
            order.On("Order", o => DisplayOrder(o));
            hubConnection.Start().Wait();

            order.Invoke("Hello").Wait();


            Console.WriteLine("Initialized...");
            Console.ReadLine();
        }


        public void DisplayOrder(Order o)
        {
            Console.WriteLine(String.Format("Order object received.../r/nOrderId: {0}/r/nName: {1}/r/nOrderItem: {2}", o.OrderId, o.Name, o.OrderItem));
            //Console.WriteLine(o);
        }
    }
}

如果我将 DisplayOrder 参数更改为字符串,它就可以工作。我知道我可能可以使用 Json.Net 手动反序列化它,但我的理解是我应该能够将它作为一个对象使用并让信号器反序列化。我错过了什么?

4

1 回答 1

4

您正在使用 On 的动态对象重载。您需要指定类型:

order.On<Order>("Order", DisplayOrder);
于 2012-12-07T10:16:03.450 回答