0

I have been working with BreezeJS for a while, and I had grate experience with it. As I started developing mobile application in .NET, I decided to give BreezeSharp a try.

I went through documentation and ToDo sample, and successfully created project explained in http://www.breezejs.com/breeze-sharp-documentation/get-your-feet-wet.

I am having an issue with executing query on mine existing Web Api controller (which works great with BreezeJS).

Here is query method:

    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];
        }
    }

And here is code from 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;
            }
        };

And here is controller method:

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

I have client PriceBook class inherited from BaseEntity which have subset of server PriceBook entities.

Metadata method in controller is hit, but I am unable to hit PriceBooks method. Instead I am getting NullReferenceException: Object reference not set to an instance of an object.

I have tried without ODataQueryOptions parameter and with various versions of EntityQuery, but without success.

Exception doesn't occur when I try calling entityManager.fetchMetadata(), but metadata method is called again when fetching PriceBook.

Does anyone have idea what could be the issue?

Edit: Here is StackTrace of the issue:

 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

Solution: I figured out that issue was with enum property. I have required enum property on server that I omitted on client:

[Required] public DataStore DataStore { get; set; }

I fixed null reference issue by adding this property to client side. Now I have issue with parsing that enum, but that is for another question.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Not really enough information to help, and you should probably read the answers to [this](http://stackoverflow.com/q/4660142/2278086). Assuming that you are seeing the message box from the `QueryPriceBooksFrom` method then I would suspect that the `entityManager` being passed in is null. The `var entityManager = new EntityManager` in `BeginInit` is suspect, since it creates a new local variable. Setting a breakpoint in `QueryPriceBooksFrom` should make it obvious what is going wrong, if that's where the error is coming from. – Mark May 15 '14 at 18:43
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ekad Jan 07 '19 at 13:12

2 Answers2

0

Thanks for trying Breeze.sharp.

I’ve tried modifying our “wet feet” example to look more like your code, but I’ve been unable to reproduce this exception. Here are a couple of requests:

  1. Since the metadata method is hit and your service works with Breezejs, you should be able to hit the PriceBooks method by pointing your browser at

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

Could you confirm that this yields Json representations of all of your PriceBooks.

  1. Since this looks like a client-side exception, could you put a breakpoint in the catch() block surrounding your ExecuteQuery() call, examine the null reference exception and send me the stack trace? If there are any inner exceptions, send me their types, message and stack traces as well.

Update:

We have reproduced this problem. We'll add a more informative message in the next release.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • PriceBooks url is fine, it was returning everything as expected. The issue was with enum property. See question post for details (I added StackTrace as well). Thanks – markolazic88 May 16 '14 at 12:43
0

This was likely an enum issue. Enums are now supported in Breeze# 0.5.5.

Jay Traband
  • 17,053
  • 1
  • 23
  • 44
  • 1
    Jay, the issue is still present. If enum property is not present on client side entity, NullReferenceException is thrown. Here is property metadata: `{ "name": "DataStore", "type": "Edm.Self.DataStore", "nullable": "false" },` I tried omitting required string property on client side, but it passed without exception. `{ "name": "Description", "type": "Edm.String", "fixedLength": "false", "maxLength": "Max", "unicode": "true", "nullable": "false" },` Which implies that issue is present only when enum property is omitted on client side. – markolazic88 Jun 04 '14 at 14:39