在Controller和Azure Mobile Client Sync Table中返回DTO时出现错误400.0

时间:2016-03-03 15:03:08

标签: azure azure-mobile-services

由于我们在Mobile Client中需要的对象需要访问其相关/关联对象,因此我们决定在调用控制器中的GetAllObjects方法时返回objectDTO而不是对象。

使用Postman查询后端服务器结果的正确行为,检索到的列表具有DTO的所有属性。

使用移动客户端时出现问题。根据日志," HTTP错误400.0 - 错误请求"发生了"由于语法格式错误,服务器无法理解请求。"在"更多信息下显示。"

我不知道为什么会发生此错误。我更新了客户端应用程序中的Object类,以匹配服务器中的ObjectDTO类。为了比较:

服务器中的ObjectDTO

public class SaleDto
    {
        public string Id { get; set; }
        public string ProductId { get; set; }
        public string PromoterId { get; set; }
        public string StoreId { get; set; }
        public string PaymentMethodId { get; set; }
        public bool CorporateSale { get; set; }
        public DateTime? DateSold { get; set; }
        public double PriceSold { get; set; }
        public int QuantitySold { get; set; }
        public string Remarks { get; set; }
        public bool Deleted { get; set; }
        public DateTimeOffset? CreatedAt { get; set; }
        public DateTimeOffset? UpdatedAt { get; set; }
        public byte[] Version { get; set; }

        public string ProductSku { get; set; }
        public string ProductPartNumber { get; set; }
        public string StoreName { get; set; }
        public string PaymentMethodName { get; set; }
    }

客户端应用中的对象模型

public class Sale
{
    [JsonProperty(PropertyName = "id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "productId")]
    public string ProductId { get; set; }

    [JsonProperty(PropertyName = "promoterId")]
    public string PromoterId { get; set; }

    [JsonProperty(PropertyName = "storeId")]
    public string StoreId { get; set; }

    [JsonProperty(PropertyName = "paymentMethodId")]
    public string PaymentMethodId { get; set; }

    [JsonProperty(PropertyName = "corporateSale")]
    public bool CorporateSale { get; set; }

    [JsonProperty(PropertyName = "dateSold")]
    public DateTime? DateSold { get; set; }

    [JsonProperty(PropertyName = "priceSold")]
    public double PriceSold { get; set; }

    [JsonProperty(PropertyName = "quantitySold")]
    public int QuantitySold { get; set; }

    [JsonProperty(PropertyName = "remarks")]
    public string Remarks { get; set; }

    [JsonProperty(PropertyName = "deleted")]
    public bool Deleted { get; set; }

    [JsonProperty(PropertyName = "createdAt")]
    public DateTime CreatedAt { get; set; }

    [JsonProperty(PropertyName = "updatedAt")]
    public DateTime UpdatedAt { get; set; }

    [JsonProperty(PropertyName = "version")]
    public string Version { get; set; }

    [JsonProperty(PropertyName = "productSku")]
    public string ProductSku { get; set; }

    [JsonProperty(PropertyName = "productPartNumber")]
    public string ProductPartNumber { get; set; }

    [JsonProperty(PropertyName = "storeName")]
    public string StoreName { get; set; }

    [JsonProperty(PropertyName = "paymentMethodName")]
    public string PaymentMethodName { get; set; }

    public virtual Product Product { get; set;}
    public virtual Store Store { get; set; }
    public virtual PaymentMethod PaymentMethod { get; set; }
}

或者可能是因为同步表?以下是处理同步的代码(为简洁起见省略了东西)

public class DataStore
{
    private static DataStore _instance;

    public MobileServiceClient MobileService { get; set; }

    IMobileServiceSyncTable<Sale> saleTable;

    public static DataStore Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new DataStore();
            }

            return _instance;
        }
    }

    private DataStore()
    {
        MobileService = new MobileServiceClient("url");

        var store = new MobileServiceSQLiteStore("tabletable.db");

        store.DefineTable<Sale>();

        MobileService.SyncContext.InitializeAsync(store);

        saleTable = MobileService.GetSyncTable<Sale>();
    }

    public async Task<Sale> AddSaleAsync(Sale sale)
    {
        await saleTable.InsertAsync(sale);
        bool wasPushed = await SyncSalesAsync();
        if (wasPushed) return null;
        return sale;
    }

    public async Task<List<Sale>> GetSalesAsync(int take = 20, int skip = 0)
    {
        IEnumerable<Sale> items = await saleTable
            .Where(sale => !sale.Deleted)
            .OrderByDescending(sale => sale.CreatedAt)
            .Take(take)
            .Skip(skip)
            .ToEnumerableAsync();

        return new List<Sale>(items);
    }

    public async Task<bool> SyncSalesAsync()
    {
        ReadOnlyCollection<MobileServiceTableOperationError> syncErrors = null;
        bool wasPushed = true;

        try
        {
            await MobileService.SyncContext.PushAsync();
            await saleTable.PullAsync("allSales", saleTable.CreateQuery());
        }
        catch (Exception e)
        {
            Debug.WriteLine(@"/Sale/ Catch all. Sync error: {0}", e.Message);
            Debug.WriteLine(e.StackTrace);
        }

        return wasPushed;
    }
}

非常感谢任何形式的帮助。

1 个答案:

答案 0 :(得分:1)

让SaleDto扩展/实现EntityData解决了问题

public class SaleDto : EntityData