Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress proxy generation for some hubs or methods

I'm starting out with SignalR and I have a situation where I'm going to have a SignalR site that will be broadcasting messages to clients, but I also need an admin interface that will actually trigger those messages. The admin page will call server side methods that will, in turn, call client side Javascript methods for regular users. So I'm thinking I can either set up two separate hubs (one for admin, one for everybody else) or I can have methods in a single hub that can only be called by the admin that will check authorization.

But in addition to the authorization, I'd like to have SignalR not include admin methods or an admin hub in the generated Javascript proxy classes so that I'm not advertising their existence (again - this is NOT the only security, I will be checking authorization). Is there an attribute or property I can set on individual hubs or on methods within a hub that will suppress them from being included in the proxy (but still have them callable from Javascript)? I know you can set EnableJavaScriptProxies to false in your HubConfiguration, but that seems to be global and I'd like to keep the proxy for the stuff I do want the regular client to be using.

like image 619
Matt Burland Avatar asked Dec 25 '22 16:12

Matt Burland


1 Answers

There is one trick using interfaces. As proxy will generate only public methods in proxy, you can create hub using interface like this:

public class MyHub : Hub, IMyHub
{
    void IMyHub.NotGeneratedOnClient()
    {
    }

    public void GeneratedOnClient()
    {
    }
}

NotGeneratedOnClient method will not be visible if you use object of type MyHub, you can access it only using interface. As method is not public proxy generator is not going to add it to client proxy

like image 82
pajo Avatar answered Dec 28 '22 07:12

pajo