Base solution for your next web application
Open Closed

SignalR as Broadcasting Mechanism of EventBus-Events #210


User avatar
0
boeseb created

Hi, i recently started to work with your awesome framework to create a new company internal application. I realy like how everything is just plugable with the convention based dependency injection but now i reacht my limit of understanding how all works together. I try to use a SignalR Hub to broadcast some Events on the EventBus to the connected Clients but this fails. For more Info pls see my post on stackoverflow where i already posted some code that i use to get things to work.

[http://stackoverflow.com/questions/31294992/use-signalr-as-broadcaster-for-eventbus-events])

i just thought i would leave a link here so some experts for abp could have a look at it and maybe help me out solving this problem or hint me to an even better solution =)

Thx in advance.

And special thanks to hikalkan for his awesome work!


5 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    I tried to answer on stackoverflow :)

  • User Avatar
    0
    boeseb created

    Thanks for your fast response.

    While trying to fix the problem i noticed some other issue with the serialization of objects from the AppService and the Serialization signalR is doing.

    In serialized objects from AppService all properties of the serialized object start with a lower case letter in the javascript object. The same object sent via SignalR however has properties starting with a upper case letter. This difference is breaking my knockout bindings.

    Is there a way to fix this?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Unfortunately, I don't know how to override serializing in SignalR. It may be easy but I don't know now. Did you searched it?

    ABP does it for web api here: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp.Web.Api/WebApi/AbpWebApiModule.cs#L52">https://github.com/aspnetboilerplate/as ... ule.cs#L52</a>

    formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    
  • User Avatar
    0
    boeseb created

    Thanks for the hint to the ContractResolver. I found following article that shows how to add a ContractResolver to SignalR

    EDIT: I found this code but am not sure where i need to register the Serializer into the IoC container

    using System;
    using System.Reflection;
    using Newtonsoft.Json.Serialization;
    using SignalR;
    
    namespace Loveboat.Configuration
    {
        public class SignalRContractResolver : IContractResolver
        {
            private readonly Assembly _assembly;
            private readonly IContractResolver _camelCaseContractResolver;
            private readonly IContractResolver _defaultContractSerializer;
    
            public SignalRContractResolver()
            {
                _defaultContractSerializer = new DefaultContractResolver();
                _camelCaseContractResolver = new CamelCasePropertyNamesContractResolver();
                _assembly = typeof (Connection).Assembly;
            }
    
            #region IContractResolver Members
    
            public JsonContract ResolveContract(Type type)
            {
                if (type.Assembly.Equals(_assembly))
                    return _defaultContractSerializer.ResolveContract(type);
    
                return _camelCaseContractResolver.ResolveContract(type);
            }
    
            #endregion
        }
    }
    

    And added this to the AbpModule Initialize Method:

    var serializersettings = new JsonSerializerSettings {
                    ContractResolver = new SignalRContractResolver()
                };
    
                var serializer = JsonSerializer.Create(serializersettings);
                IocManager.IocContainer.Register(Component.For<JsonSerializer>().Instance(serializer));
    

    It works, but is there a better way to solve it?

  • User Avatar
    0
    boeseb created

    Also my other problem is solved now. It had to do with the custom DependencyResolver. After setting it in my HubConfig getting a valid ConnectionManager from GlobalHost failed. So i got a not working HubContext. Setting the custom DependencyResolver in GlobalHost.DependencyResolver instead of only in the Hub Configuration fixed the problem. See [http://stackoverflow.com/a/31315380/4369295]) for code.