C# Azure移动服务-从另一个自定义控制器访问表控制器
我正在尝试从另一个控制器方法使用其控制器访问表。 但当该方法尝试调用table controller方法时,我得到一个异常: Exception=System.NullReferenceException:对象引用未设置为对象的实例。在Microsoft.WindowsAzure.Mobile.Service.TableController 我设法从web API访问table controller方法并成功执行它。 我用TodoItem做了同样的尝试,最初的移动服务就是一个例子。 在多次发布到服务器并尝试修复该问题后,web API停止工作,我收到了此异常:mscorlib.dll中出现了类型为“Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException”的异常,但未在用户代码中处理 其他信息:无法完成请求。内部服务器错误当我用完全相同的代码重新打开移动服务和数据库时,我设法解决了它。 有什么建议吗 以下是由控制器向导创建的我的表控制器: 下面是尝试访问该方法的其他控制器代码: 我还尝试了不同的方法实现: 服务上下文:C# Azure移动服务-从另一个自定义控制器访问表控制器,c#,asp.net,azure,azure-sql-database,azure-mobile-services,C#,Asp.net,Azure,Azure Sql Database,Azure Mobile Services,我正在尝试从另一个控制器方法使用其控制器访问表。 但当该方法尝试调用table controller方法时,我得到一个异常: Exception=System.NullReferenceException:对象引用未设置为对象的实例。在Microsoft.WindowsAzure.Mobile.Service.TableController 我设法从web API访问table controller方法并成功执行它。 我用TodoItem做了同样的尝试,最初的移动服务就是一个例子。 在多次发布到
这两种方法的错误是否相同?如果您选择新的StorageItemController路线,会得到什么?是的,同样的错误。我设法使它工作,我不知道为什么,但当我从头开始创建项目时,问题得到了解决。但是现在,当我尝试打开另一个表时,我得到了相同的错误,似乎每当我添加一个表控制器时,就会出现一些问题,为什么不在API控制器中使用DomainManager或DBContext,而不使用TableController呢?我知道您希望重用,但该控制器是DomainManager的HTTP前端,用于处理HTTP请求基础结构和域管理器之间的交互。
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);
}
}
}
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();
}
}
}
public void PostProcessTag(String id)
{
StorageItemController table_controller = new StorageItemController();
IQueryable<DataObjects.StorageItem> item = table_controller.GetAllStorageItem();
}
using System.Data.Entity;
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; }
}
}