1
votes

I am in the progress of migrating (upgrading to be precise) from Azure Mobile Services to Azure Mobile Apps. My azure backend (.net) used to offer hybrid chat implementation in both SignalR and notification hubs.

The following snippet is working in Azure Mobile Services environment

[HubName("chatHub")]
public class ChatHub : Hub
{
    public ApiServices Services { get; set; }
    public IServiceTokenHandler handler { get; set; }
    protected async Task NotifyParticipants(IPushMessage message, String tag)
    {
        await Services.Push.SendAsync(message, tag);
    }
}

but since ApiServices is no longer available in Azure Mobile Apps environment, how should I refactor the implementation ?

I am using the following snippet in my API controllers that send notifications but I can't use the same in chatHub above because I'm not able to get HttpConfiguration needed to retrieve my notification hub settings.

[MobileAppController]
public class SomeController : ApiController
{
    protected NotificationHubClient hubClient { get; set; }
    protected override void Initialize(HttpControllerContext controllerContext)
    {
        base.Initialize(controllerContext);

        // Get the settings for the server project.
        HttpConfiguration config = this.Configuration;
        MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();

        // Get the Notification Hubs credentials for the Mobile App.
        string notificationHubName = settings.NotificationHubName;
        string notificationHubConnection = settings.Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;

        // Create a new Notification Hub client.
        hubClient = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
    }
    protected async Task NotifyParticipants(IPushMessage message, String tag)
    {
        await hubClient.SendGcmNativeNotificationAsync(message.ToString(), tag);
    }
}
1
Is it possible that you're missing some package(s) or assembly reference(s)? GetMobileAppSettingsProvider() is an extension method on HttpConfiguration defined in Microsoft.Azure.Mobile.Server.dll. The Azure documentation article's code is almost identical to yours. So there's a chance some project configuration is incomplete.Nikita R.
Nikita, I think I'm not missing any dependencies. The reason I'm not able to invoke GetMobileAppSettingsProvider() is because I dont have access to HttpConfiguration instance from SignalR.Hub (where ApiController derivatives has access inherited from base class)YeenFei

1 Answers

1
votes

If you need to get access to the NH connection string (or any other setting), you can always use the ConfigurationManager directly like:

var connstr = ConfigurationManager
    .ConnectionStrings[MobileAppSettingsKeys.NotificationHubConnectionString]
    .ConnectionString;

If you'd like to have access to the actual MobileAppSettingsDictionary (it'd be easier to mock for testing, for example), you can wire up a DependencyResolver to SignalR: http://www.asp.net/signalr/overview/advanced/dependency-injection.

I used their Ninject example (but they link to other providers) and my code in Startup.MobileApp.cs looks like:

var kernel = new StandardKernel();
var resolver = new NinjectSignalRDependencyResolver(kernel);
kernel.Bind<MobileAppSettingsDictionary>()
    .ToConstant(config.GetMobileAppSettingsProvider().GetMobileAppSettings());

var signalRConfig = new HubConfiguration();
signalRConfig.Resolver = resolver;
app.MapSignalR(signalRConfig);

And in your Hub you do something like this (you can also use constructor injection):

[Inject]
public MobileAppSettingsDictionary MobileAppSettings { get; set; }

Then you should have access to the settings anywhere in that Hub.