3

私の理解では、Signalrを使用すると、オブジェクトを前後に送信できます。Webサイトで注文が行われたという通知を受信するように.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を使用して手動で逆シリアル化できる可能性があることはわかっていますが、オブジェクトとして操作し、Signalerを逆シリアル化できるようにする必要があると理解しています。私は何が欠けていますか?

4

1 に答える 1

4

Onの動的オブジェクトオーバーロードを使用しています。タイプを指定する必要があります。

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