Creating a Sync Database makes Visual Studio hang and then exit code, while in a try catch!

I am trying to integrate a simple Sync Database for an Azure easytable. I am following many of James Montemagno tutorial/example projects. like this one
https://github.com/jamesmontemagno/app-coffeesync/blob/master/CoffeeApp.Shared/Model/Azure/AzureService.cs
and this one

I am so stumped. I have been working on this for days and tried so much, and created new problems that took a lot of time to fix. But I am finally fully stuck. I don't know what to do now :disappointed:

When I run this in Debug and step through everything Visual Studio Hangs for about 6 seconds and then it exits the code and the Output says
"Resolved pending breakpoint at 'ChatStorageAzureService.cs:120,1' to void CFFA.Services.ChatStorageAzureService.d__15.MoveNext () [0x000d6].
Thread finished: #22
Thread finished: #24
Thread finished: #23
Thread finished: #25
The thread 0x16 has exited with code 0 (0x0).
The thread 0x18 has exited with code 0 (0x0).
The thread 0x17 has exited with code 0 (0x0).
The thread 0x19 has exited with code 0 (0x0)."
And then the app on my phone is frozen and I have to Stop the debug.
This all happens when I just run this line
var path = "syncstore.db";

How in the world is just assign a string var going to crash the program!?!?! Oh, and on top of all this, that code block is in a freaking try catch! But no exception is ever thrown :disappointed:
I am assuming another part of the code is breaking it. But I am trying to follow this simple tut to the t. Now I have to make it work inside my app so I have to change parts, but stuff that should not affect the overall function.
I also could not create a Singleton of the ChatStorageAzureService with Dependency like the tut tells me to because of this issue.https://forums.xamarin.com/discussion/comment/312753#Comment_312753
But I don't think that should be an issue as I only create ChatStorageAzureService one time when I run this code.
Ideas?
Here is the code block that breaks

public async Task InitializeAsync()
{
if (client?.SyncContext?.IsInitialized ?? false)
return;

        var appUrl = "http://MY-SITE.azurewebsites.net";
        try
        {
            client = new MobileServiceClient(appUrl);

            var path = "syncstore.db";

            var store = new MobileServiceSQLiteStore(path);

            store.DefineTable<MessageViewModel>();

            await client.SyncContext.InitializeAsync(store);

            chatTable = client.GetSyncTable<MessageViewModel>();
        }
        catch (Exception e)
        {
            Debug.WriteLine("Exception thrown in Initialize: " + e);
            throw;
        }
    }

Answers

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    How do you call InitializeAsyc?
  • JohnButlerJohnButler USMember ✭✭
    edited December 2017

    A few different ways, all with the same outcome.
    1. I run it inside the ChatStorageAzureService object, whenever an operation is called like AddChat, GetChat, or SyncChat. This is what James recommends in his tutorials. And there is that code that checks to see if it has been initialized a the top,
    " if (client?.SyncContext?.IsInitialized ?? false)
    return;"
    He recommends you do this just so you know that whenever you invoke a method in that class that you know the database is created.

    1. I thought maybe it dosen't have enough time or something, (I was very confused) so I desided to run it when I create the object So I put a Try catch after I create it in my constructor of my ChatMessageTableViewModel (which creates an instance of ChatStorageAzureService
      public ChatMessageTableViewModel()
      {
      _chatStorageAzureService = new ChatStorageAzureService();
      try
      {
      Task.Run(() => _chatStorageAzureService.InitializeAsync()).Wait();
      }
      catch (Exception e)
      {
      Debug.WriteLine(e);
      throw;
      }
      }
      This execution actually doesn't make VS hang. It runs and continues through the code. Problem is. It doesn't actually run InitializeAsync(I have the appUrl and path tracked in my Watch 1 window), nor does it throw and exception! it just continues on with the code. Even if I try to Step in(F11) with the debugger, it does not step in. If I create a breakpoint inside the InitializeAsync Method. Than when I step through the ChatMessageTableViewModel constructor it does step into InitializeAsync but then it makes VS hang for the same point, assigning a string to var path.

    2. I also did it a different way where a press of an asyn button, created and invoked InitializeAsyn, without doing that weird .Wait() thingy that I hear is a bad practice. I just used the normal await. But the same thing. Now i did try this hours and hours ago so I don't remember the details. I will give it a shot soon so to be sure it acts the same.

  • JohnButlerJohnButler USMember ✭✭

    Okay I retried the the 3rd way in the above post. Clicking a button to create and run initializeAsync. It did the same thing, VS hung while trying to assign a string to var path. I also created ChatStorageAzureService in the constructor of a page. And then only tried to initializeAsync with a button press. Same thing.

    But then while writing this and the whole time I have been dealing with the issue I didnt see how making a string var ever freeze a program. So I moved it up a line, before the
    client = new MobileServiceClient(appUrl)
    then it went to the next line as it has always done, but then after trying to run the next line,
    var store = new MobileServiceSQLiteStore(path);
    it froze VS just as before. So I really feel like it is the "client = new MobileServiceClient(appUrl)" line that breaks it. But why it runs and then breaks on the next line is far beyond my understanding.
    This whole situation has sparked SO many questions. How can code in a try catch freeze VS and the android app, but not throw an exception.
    I would love to fix this issue, but more so get all those questions answered. And I am not expecting one of you all to do it. But maybe a way to read about this. I don't even know what to google here.
    Thanks a million!

  • JohnButlerJohnButler USMember ✭✭
    edited December 2017

    Could the problem be that I have both a SignalR hub and the Table storage code in one project? Should I break it into two different ones. They have different web address, even though they are one project that is published to my Azure cloud. Not sure how that is all working. But as it is abundantly clear I don't know most of the stuff I am doing :disappointed:

    My thought behind putting them in one project, was so that it was easy to just just send the chat message up to the cloud via SignalR and then have the Hub push the message to the Clients but also send it to the Table for long term storage. Instead of sending the message twice, or instead of turning the SignalR project into the client of the table storage project and have to worry about connecting to 2 separate project. Is that wise? lol

  • LyndonHugheyLyndonHughey USUniversity ✭✭✭

    Debugging these issues can be a pain, but there are a couple of things you can do to try to isolate or identify the problem.
    1. You can attach to the app service with your debugger. Check out the link located at https://channel9.msdn.com/Shows/Cloud+Cover/Episode-224-Developing-and-Debugging-Azure-Mobile-Apps-with-Adrian-Hall. The debugging section is located around the 3:30 marker.

    Also, I always run my server code locally to work out the kinks before deploying to the server. I don't see any issues with your code (giving it a quick glance). Often times you'll find your problem on the server side in the logs or the local application insights (if running locally). Good luck.

  • JohnButlerJohnButler USMember ✭✭

    @LyndonHughey said:
    Debugging these issues can be a pain, but there are a couple of things you can do to try to isolate or identify the problem.
    1. You can attach to the app service with your debugger. Check out the link located at https://channel9.msdn.com/Shows/Cloud+Cover/Episode-224-Developing-and-Debugging-Azure-Mobile-Apps-with-Adrian-Hall. The debugging section is located around the 3:30 marker.

    Also, I always run my server code locally to work out the kinks before deploying to the server. I don't see any issues with your code (giving it a quick glance). Often times you'll find your problem on the server side in the logs or the local application insights (if running locally). Good luck.

    LyndonHughey, Thanks for that info! I watched that vid and will be working on debugging my back end today.
    I tried doing all my dev work locally, not in the cloud. But I read a lot of people have trouble with Xamarin and having a local backend. Im sure it can be done, but it seemed like one more hurdle for me to jump over and I have enough of them as is. :disappointed:
    Do you think having both SignalR and Azure Table in one project is bad?

  • JohnButlerJohnButler USMember ✭✭

    I did all the stuff in the video and set my server based code up for debugging. Super cool! Thanks for sending me that. I still have not found the issue causing the Android app to freeze.
    But something to note when I use Postman to add Post and Get form the table it works perfectly fine. So I don't think the issue is server based. I mean I think my server definitely is janky AF, and I will need to clean it up later. But it seems to be working, based on Postman. It is my android project that is the problem, I think lol.

  • JohnButlerJohnButler USMember ✭✭

    So I finally got it to work!!! "How?" you ask. I went on 2 week vacation, came back, started a again. Copied the new URL of the tut project into my actual project, did some testing. And then this is the big thing, I then put the same address I had been using back into my app, and... it just worked.... I did NOTHING to the code, and now it seems to work... So almost a month of work time lost and all I had to do was just put a different URL in, run it, and then put the original URL back in.... Lovely. I am guessing it cleared out some weird temp file that was messing up the program or something... even though I erased the temp files countless times... I don't get it, but onward I go!

Sign In or Register to comment.