Quantcast
Channel: Azure Mobile Apps forum
Viewing all articles
Browse latest Browse all 3756

Azure mobile service - accessing a table controller from another custom controller

$
0
0

I  am trying to access a table using its controller from another controller method. But when the method tries to call the table controller method I get an exception:Exception=System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.WindowsAzure.Mobile.Service.TableController.....

  • I manage to access the table controller method from the web API and execute it successfully.
  • I tried the same thing with TodoItem given as an example by the initial mobile service.
  • After several publishes to the server trying to fix the issue the web API stopped working and I get this exception : An exception of type 'Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException' occurred in mscorlib.dll but was not handled in user code Additional information: The request could not be completed. (Internal Server Error) I managed to solve it when I reopened a mobile service and database with the exact same code that didn't work.

Any tips ?

Here is my table controller created by the controller wizard:

using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.OData;
using Microsoft.WindowsAzure.Mobile.Service;
using FringProjectMobileService.DataObjects;
using FringProjectMobileService.Models;

namespace FringProjectMobileService.Controllers
{
    public class StorageItemController : TableController<StorageItem>
    {
        protected override void Initialize(HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            FringProjectMobileServiceContext context = new FringProjectMobileServiceContext();
            DomainManager = new EntityDomainManager<StorageItem>(context, Request, Services);
        }

        // GET tables/StorageItem
        public IQueryable<StorageItem> GetAllStorageItem()
        {
            return Query();
        }

        // GET tables/StorageItem/xxxxxxxxxx
        public SingleResult<StorageItem> GetStorageItem(string id)
        {
            return Lookup(id);
        }

        // PATCH tables/StorageItem/xxxxxxxx
        public Task<StorageItem> PatchStorageItem(string id, Delta<StorageItem> patch)
        {
             return UpdateAsync(id, patch);
        }

        // POST tables/StorageItem
        public async Task<IHttpActionResult> PostStorageItem(StorageItem item)
        {
            StorageItem current = await InsertAsync(item);
            return CreatedAtRoute("Tables", new { id = current.Id }, current);
        }

        // DELETE tables/StorageItem/xxxxxxxxxx
        public Task DeleteStorageItem(string id)
        {
             return DeleteAsync(id);
        }

    }
}

Below the other controller code trying to access the method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.WindowsAzure.Mobile.Service;


namespace FringProjectMobileService.Controllers
{
    public class ArduinoController : ApiController
    {
        public ApiServices Services { get; set; }

        // GET api/Arduino
        public string Get()
        {
            Services.Log.Info("Hello from custom controller!");
            return "Hello";
        }


        public async void PostProcessTag(String id)
        {
            Microsoft.WindowsAzure.MobileServices.MobileServiceClient client = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("http://some-service.azure-mobile.net", "XXXXXXXXXXXXXXX");
            Microsoft.WindowsAzure.MobileServices.IMobileServiceTable<DataObjects.StorageItem> storage_item_table = client.GetTable<DataObjects.StorageItem>();
            await storage_item_table.ToEnumerableAsync();
        }
    }
}

I also tried a different implementation for the method :

 public void PostProcessTag(String id)
        {
            StorageItemController table_controller = new StorageItemController();
            IQueryable<DataObjects.StorageItem> item = table_controller.GetAllStorageItem();
        }

The service context:

using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using Microsoft.WindowsAzure.Mobile.Service;
using Microsoft.WindowsAzure.Mobile.Service.Tables;

namespace FringProjectMobileService.Models
{
    public class FringProjectMobileServiceContext : DbContext
    {
        // You can add custom code to this file. Changes will not be overwritten.
        //
        // If you want Entity Framework to alter your database
        // automatically whenever you change your model schema, please use data migrations.
        // For more information refer to the documentation:
        // http://msdn.microsoft.com/en-us/data/jj591621.aspx
        //
        // To enable Entity Framework migrations in the cloud, please ensure that the
        // service name, set by the 'MS_MobileServiceName' AppSettings in the local
        // Web.config, is the same as the service name when hosted in Azure.

        private const string connectionStringName = "Name=MS_TableConnectionString";

        public FringProjectMobileServiceContext() : base(connectionStringName)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            string schema = ServiceSettingsDictionary.GetSchemaName();
            if (!string.IsNullOrEmpty(schema))
            {
                modelBuilder.HasDefaultSchema(schema);
            }

            modelBuilder.Conventions.Add(
                new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>("ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
        }

        public System.Data.Entity.DbSet<FringProjectMobileService.DataObjects.StorageItem> StorageItems { get; set; }
    }
}



Viewing all articles
Browse latest Browse all 3756

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>