Home > Azure, C#, Mobile Services > Accessing service information in custom APIs of Mobile Services .NET Backend

Accessing service information in custom APIs of Mobile Services .NET Backend

07/04/2014

When working with Mobile Services .NET Backend, we could have the need to access service information, like settings, log, push client, etc.

The TableController class provides a Services property that exposes all the service information, so if we inherit from this object, we have immediately use it. However, if we want to define a custom API, we need to inherit from the standard ApiController class, that doesn’t contain any reference to the Mobile Service.

If we look deeper, we’ll see that TableController inherits from ApiController and defines the following property:

public ApiServices Services { get; set; }

It provides an object that allows to access all the service information and that it is automatically set via Autofac property injection. So, we can add the same property in our custom APIs classes that directly inherit from ApiController. For example:

public class ImagesController : ApiController
{
    public ApiServices Services { get; set; }

    [HttpGet]
    public string Get()
    {
        // Writes a message in the Service Log.
        Services.Log.Info("Method Images/Get called");

        return "Data";
    }
}

Of course, we can also create a base class with this property and make custom APIs inherit from it, so that they can directly use it.

Categories: Azure, C#, Mobile Services
%d bloggers like this: