BreezeSharp - ExecuteQuery失败,出现NullReferenceException

时间:2014-05-15 14:33:12

标签: .net breeze breeze-sharp

我和BreezeJS一起工作了一段时间,我对此感到很满意。当我开始在.NET中开发移动应用程序时,我决定尝试一下BreezeSharp。

我浏览了文档和ToDo示例,并成功创建了http://www.breezejs.com/breeze-sharp-documentation/get-your-feet-wet中解释的项目。

我遇到了在我现有的Web Api控制器上执行查询的问题(适用于BreezeJS)。

这是查询方法:

    private async Task<IEnumerable<PriceBook>> QueryPriceBooksFrom(EntityManager entityManager)
    {
        try
        {
            var query = new EntityQuery<PriceBook>().From("PriceBooks");
            var result = await entityManager.ExecuteQuery(query);
            return result;
        }
        catch (Exception e)
        {
            MessageBox.Show(e.GetType().Name + ": " + e.Message);
            return new PriceBook[0];
        }
    }

这是来自BeginInit()的代码:

        DataContext = this;
        Configuration.Instance.ProbeAssemblies(typeof(PriceBook).Assembly);

        var entityManager = new EntityManager("http://127.0.0.1:81/api/cache/");

        entityManager.MetadataStore.NamingConvention = new NamingConvention().WithClientServerNamespaceMapping("clientNamespace", "serverNamespace");

        entityManager.MetadataStore.AllowedMetadataMismatchTypes = MetadataMismatchType.AllAllowable;

        entityManager.MetadataStore.MetadataMismatch += (s, e) =>
        {
            // Log the mismatch
            var message = string.Format("{0} : Type = {1}, Property = {2}, Allow = {3}",
                                        e.MetadataMismatchType, e.StructuralTypeName, e.PropertyName, e.Allow);
            Console.WriteLine(message);

            // Disallow missing navigation properties on the TodoItem entity type
            if (e.MetadataMismatchType == MetadataMismatchType.MissingCLRNavigationProperty &&
                e.StructuralTypeName.StartsWith("PriceBook"))
            {
                e.Allow = false;
            }
        };

这是控制器方法:

    [HttpGet]
    public IQueryable<PriceBook> PriceBooks(ODataQueryOptions options)
    {
        return ...;
    }

我有从BaseEntity继承的客户端PriceBook类,它具有服务器PriceBook实体的子集。

控制器中的元数据方法被命中,但我无法点击PriceBooks方法。相反,我得到 NullReferenceException:对象引用未设置为对象的实例。

我尝试过没有ODataQueryOptions参数和各种版本的EntityQuery,但没有成功。

当我尝试调用entityManager.fetchMetadata()时,不会发生异常,但在获取PriceBook时会再次调用元数据方法。

有谁知道可能是什么问题?

修改: 这是问题的StackTrace:

 at Breeze.Sharp.CsdlMetadataProcessor.ParseCsdlDataProperty(StructuralType parentType, JObject csdlProperty, List`1 keyNamesOnServer) in c:\GitHub\breeze.sharp\Breeze.Sharp\CsdlMetadataProcessor.cs:line 136
   at Breeze.Sharp.CsdlMetadataProcessor.<>c__DisplayClass14.<ParseCsdlEntityType>b__10(JToken csdlDataProp) in c:\GitHub\breeze.sharp\Breeze.Sharp\CsdlMetadataProcessor.cs:line 109
   at Breeze.Sharp.Core.EnumerableFns.ForEach[T](IEnumerable`1 items, Action`1 action) in c:\GitHub\breeze.sharp\Breeze.Sharp\Core\EnumerableFns.cs:line 35
   at Breeze.Sharp.CsdlMetadataProcessor.ParseCsdlEntityType(JObject csdlEntityType) in c:\GitHub\breeze.sharp\Breeze.Sharp\CsdlMetadataProcessor.cs:line 108
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Breeze.Sharp.CsdlMetadataProcessor.ProcessMetadata(MetadataStore metadataStore, String jsonMetadata) in c:\GitHub\breeze.sharp\Breeze.Sharp\CsdlMetadataProcessor.cs:line 33
   at Breeze.Sharp.MetadataStore.<FetchMetadata>d__a.MoveNext() in c:\GitHub\breeze.sharp\Breeze.Sharp\MetadataStore.cs:line 166
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Breeze.Sharp.EntityManager.<FetchMetadata>d__3.MoveNext() in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityManager.cs:line 198
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Breeze.Sharp.EntityManager.<ExecuteQuery>d__b.MoveNext() in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityManager.cs:line 230
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Breeze.Sharp.EntityManager.<ExecuteQuery>d__6`1.MoveNext() in c:\GitHub\breeze.sharp\Breeze.Sharp\EntityManager.cs:line 208
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at StanleySteemer.Nimbus.Mobile.MainWindow.<QueryPriceBooksFrom>d__9.MoveNext() in c:\Projects\SSI\Nimbus\source\StanleySteemer.Nimbus.Mobile\MainWindow.xaml.cs:line 109

解决方案: 我发现这个问题与enum属性有关。 我在服务器上需要枚举属性,我在客户端上省略了:

  

[必需]   public DataStore DataStore {get;组; }

我通过将此属性添加到客户端来修复了空引用问题。 现在我遇到了解析枚举的问题,但那是另一个问题。

2 个答案:

答案 0 :(得分:0)

感谢您尝试Breeze.sharp。

我已经尝试修改我们的“湿脚”示例,使其看起来更像您的代码,但我无法重现此异常。以下是一些要求:

  1. 由于元数据方法已被点击且您的服务与Breezejs一起使用,您应该可以通过将浏览器指向

    来点击PriceBooks方法

    http://127.0.0.1:81/api/cache/PriceBooks

  2. 您能否确认这会产生所有PriceBook的Json表示。

    1. 由于这看起来像是客户端异常,你是否可以在ExecuteQuery()调用周围的catch()块中放置断点,检查空引用异常并向我发送堆栈跟踪?如果有任何内部异常,请向我发送他们的类型,消息和堆栈跟踪。
    2. 更新

      我们已经复制了这个问题。我们将在下一个版本中添加更多信息。

答案 1 :(得分:0)

这可能是一个问题。现在,Breeze#0.5.5支持枚举。