Alireza Noori
Alireza Noori

Reputation: 15275

Send/Receive complex object on SignalR

I'm trying to pass a complex object from server to the client. My client code looks like this:

var hubConnection = new HubConnection("http://somewebsite.com/");
var serverHub = hubConnection.CreateHubProxy("searchHub");

serverHub.On<Complex>("newSearch",
    obj =>
    {
        Console.WriteLine(obj.ToString());
    });

and the Complex class is:

public class Complex
{
    public string url { get; set; }
    public string title { get; set; }
    public int results { get; set; }
    public DateTime date { get; set; }
}

On the server, I use:

public class SearchHub: Hub
{
    public static void NewSearch(string url, string title, int results)
    {
        var context = GlobalHost.ConnectionManager.GetHubContext<StatsHub>();

        title = Encoder.HtmlEncode(title);
        url = Encoder.UrlEncode(url);
        var date = DateTime.UtcNow;
        context.Clients.All.newSearch(url, title, results, date);
    }
}

But it's not working. If I remove the type from the On<Complex>("newSearch" and use it like .On("newSearch", I get the connection but it only prints the first parameter (the url). How should I pass a complex object from the server?

Upvotes: 2

Views: 5666

Answers (2)

user2832577
user2832577

Reputation: 87

Hope this will help someone, this is a basic implementation.

enter image description here

enter image description here

Upvotes: -1

Anders
Anders

Reputation: 17564

You are calling newSearch on the server with the primitive types as argument, not a instance of your class. Either change the signature on your client or on the server. They need to match

Upvotes: 2

Related Questions