저는 signalr을 사용하여 객체를 앞뒤로 보낼 수 있다는 것을 알고 있습니다. 웹 사이트에 주문이 접수되었다는 알림을 받으려면 .net 클라이언트를 설정하려고합니다. 나는 개념을 이해할 수 있도록 아주 간단한 예제를 설정하려고합니다. 내가 다시 클라이언트로 문자열 알림을 보내고 때 잘 작동하지만, 내가 개체를 보내려고하면 오류 얻을 :.net signalr 클라이언트에 객체를 보내는 중 오류가 발생했습니다.
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 - 나는 그것이 문자열 버전 및 테스트를위한 객체 버전을 모두 보내는 데 있기 때문에 조금 이상한 : 나는 DisplayOrder 매개 변수를 변경하는 경우
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);
}
}
}
:
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);
}
}
그리고 내 콘솔 응용 프로그램을 작동하는 문자열이 될 수 있습니다. 아마도 수동으로 Json.Net을 사용하여 deserialize 할 수는 있지만, 필자가 객체로 작업하고 신호 변환기를 deserialize 할 수 있어야한다는 것을 이해했습니다. 내가 뭘 놓치고 있니?
참고 : 나는 어리석은 일을 할 때 그것을 싫어합니다. 나는 문서에서 그 구문을보고 그것을 시도했지만 컴파일되지는 않을 것이다. 문제는 방금 Main에 DisplayOrder를 선언했는데 정적이 아니기 때문에 컴파일하지 않을 것이라는 것이 었습니다. 나는 정적으로 순서를 표시하도록 변경하고 완벽하게 작동합니다. 나를 올바른 방향으로 돌려 주셔서 고마워요. 그리고 기록을 위해, 나는 완전히 신호에 깊은 인상을 받았습니다. 너희들 잘 했어! – pcbliss