Realm Cloud Guidance for .Net


#1

Having setup an account using the beta of Realm Cloud I’m given the options to proceed with iOS, Android or Node. Is there Realm Cloud Guidance for .Net?


#3

We don’t have a .Net tutorial yet though we have plans to add one.

In the meantime, you could use our tasks app which could be configured to use a cloud instance.


#4

Thanks for that. I’ve had a look at the tasks app but I might have to wait until there is an example for UWP.


#5

I managed to connect using the Realm docs for Sync

Authentication seemed to work ok but Realm.GetInstanceAsync is taking forever. This is the code I’m using

public static async Task<Realm> GetInstanceAsync()
{
       Credentials credentials = Credentials.UsernamePassword("xxx", "xxxxxx", false);
       Uri authURL = new Uri("https://xxx.us1.cloud.realm.io");
       User user = await User.LoginAsync(credentials, authURL);
       Uri serverURL = new Uri("realms://xxx.us1.cloud.realm.io");
       SyncConfiguration config = new SyncConfiguration(user, serverURL);
       Stopwatch watch = Stopwatch.StartNew();
       Realm realm = await Realm.GetInstanceAsync(config);
       watch.Stop();
       Debug.WriteLine($"GetInstanceAsync {watch.Elapsed.Seconds} seconds");
       return realm;
}

It’s been running the line Realm realm = await Realm.GetInstanceAsync(config) for about an hour so far. As you can see I have a stopwatch timing it. When it returns I’ll post the time here.

I am connecting from Australia. We current run an Azure SQL Server database in the UK and connections to it from here are quite fast. I have a 40mbps symmetrical internet connection. Our customers typically have slower connections.

Is there anything in the code above that might cause the slow response?


#6

@mgeerling My app has been trying to connect for 9 hours … and still trying


#7

@Nosl, it looks like you didn’t specify a realm within your serverURL which seems to be your Realm URL path. You would typically have something like realms://xxx.us1.cloud.realm.io/ToDo


#8

Thank you for that @mgeerling. We want each user to connect to their own realm. If they haven’t connected before, a new realm should be created. The following code returns Unknown Error

User user = await User.LoginAsync(credentials, authURL);
Uri serverURL = new Uri($"realms://xxx.us1.cloud.realm.io/{user.Identity}");
SyncConfiguration config = new SyncConfiguration(user, serverURL);
Stopwatch watch = Stopwatch.StartNew();
Realm realm = await Realm.GetInstanceAsync(config);

Is the error caused by there not being a realm to connect to? If so how do we ensure that where the realm does not exist, a new realm is created?


#9

Try a serverURL like: Uri serverURL = new Uri($"realms://xxx.us1.cloud.realm.io/~/user");

The ~ will resolve to the user’s userID. If this still throws an error, could you share your repo?


#10

Thanks for the tip. I’ve made the change but am still getting the same error. This is our complete connection code.

public static async Task<Realm> GetInstanceAsync(bool startUp = false)
{
      Credentials credentials = Credentials.UsernamePassword(xxx, xx, false);
      Uri authURL = new Uri("https://xxx.us1.cloud.realm.io");
      User user = await User.LoginAsync(credentials, authURL);
      Uri serverURL = new Uri($"realms:/xxx.us1.cloud.realm.io/~/user");
      SyncConfiguration config = new SyncConfiguration(user, serverURL);
      Stopwatch watch = Stopwatch.StartNew();
      Realm realm = await Realm.GetInstanceAsync(config);
      watch.Stop();
      Debug.WriteLine($"GetInstanceAsync {watch.Elapsed.Seconds} seconds");
      return realm;
}

I will send login and server details by pm.


#11

I have updated my code

public static class RealmService
{
    const string SERVER = "xxx.us1.cloud.realm.io";
    internal static string authURL = $"https://{SERVER}";
    internal static string dataURL = $"realms://{SERVER}";
    public static async Task<Realm> GetInstanceAsync()
    {
        Credentials credentials = Credentials.UsernamePassword("xxx", "xxxx", false);
        Uri authURI = new Uri(authURL);
        User user = await User.LoginAsync(credentials, authURI);
        Uri dataURI = new Uri($"{dataURL}/~/user");
        SyncConfiguration config = new SyncConfiguration(user, dataURI);
        Realm realm = await Realm.GetInstanceAsync(config);
        return realm;
    }
}

The line Realm realm = await Realm.GetInstanceAsync(config) throws 'System.InvalidOperationException' in System.Private.CoreLib.ni.dll


#12

Realm.GetInstanceAsync will not create the server-side Realm which is why you’re getting that error. You can use the non-async version: realm = Realm.GetInstance(config).


#13

Thank you @nirinchev but Realm realm = Realm.GetInstance(config) throws the same error.


#14

Can you post the entire error - message + stack trace?


#15

It doesn’t appear in the errors just n the output so I don’t get a stack trace with the error. The line
Realm realm = Realm.GetInstance(config)
throws the error twice and doesn’t return. The output is

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.ni.dll
Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.ni.dll

I messaged @mgeerling a link to a simple UWP app that demonstrates the issue. Shall I message that link to you too?


#16

Is this a bug in Realm Cloud? The simple app I messaged to @nirinchev and @mgeerling to demo this issue is only a few lines of code so it should be easy to see where I got it wrong. We’re blocked on this so if I didn’t get it wrong and it is a bug in Realm Cloud, please let me know when we can expect a fix.


#17

I checked the app that you sent me and managed to modify the code so that it works. First, I wrapped your RealmService.GetInstanceAsync method in try-catch block which allowed me to inspect the exceptions being thrown. The first error you get is because your code doesn’t declare any RealmObject inheritors (thus the Realm schema is empty). This is easily fixed by creating a dummy model class:

public class Foo : RealmObject
{
    public int Value { get; set; }
}

Then you get a different exception with error code 105. Unfortunately, that’s a native error, so it was harder to debug. Ultimately, using some intuition and googling, it appears it’s an SSL issue where the client doesn’t trust the certificate. OpenSSL on Windows doesn’t seem to be using the Windows certificate store, so it doesn’t trust our Cloud SSL certificates. We’ll work around that in a later version, but for the time being you can set EnableSSLValidation = false on your config. The connection will still be secure, but the client won’t validate the certificate.

Doing these two things, I was able to open a realm successfully with the app you sent over.


#18

Thank you for debugging that issue @nirinchev; that works a treat.