Access Realms from .NET web app


#1

I am developing an application which has a large public database in the cloud accessible to all users. Because Realm cannot offer partial sync or cloud only functionality I’m using Azure SQL for this with a .NET backend.

Each user then has a local private Realm with their personal data, syncd to a Realm Object Server in the cloud. They can select items from the public database which are then copied into their own private realm for further personalisation.

Ultimately I would like to offer a web app where the user can log in to a website and access their private data just as they do on their phone. Its kind of like being able to use Facebook or Gmail or any of a thousand other apps with both mobile and web interfaces.

My question is: can I access realms on the Realm Object Server from my .Net web app code? If so, how. If not, what is the suggested workaround to add a web application as this must cover a huge proportion of use cases? A lot of users only have a mobile which is fine, but a lot of users prefer the size and convenience of a web app on their mac/pc while they are at home, especially if they have a lot of data to enter.


#2

Yes, you can access Realms on your server the same way you access them on the client - you login the user, open the Realm, and read and write data:

var user = User.LoginAsync(username, password);
var config = new SyncConfiguration(user, new Uri("realm://my.server.com/~/realm"));
using (var realm = Realm.GetInstanceAsync(config))
{
    // read/write
}

These changes will later be synchronized with the mobile client.


#3

Wow, that was a lot simpler than I was expecting!
Thanks :smiley:


#4

Okay, finally got round to trying this out. I’m using Azure to host Azure Mobile backend that will ultimately provide an Azure SQL database with a mobile client, an api, and a web app. At the moment I’m just playing with the api. (My Xamarin mobile app using Realm for user data and the Realm server hosted on a linux VM in Azure is working great by the way - fantastically simple and easy to use - I’m now wanting to add access to the Realms from the backend).

Problem I’m having is that the template for the Azure backend app is .NET Framework 4.6. When I add the Realm NuGet and try to access my Realm database I get “The PCL build of Realm is being linked which probably means you need to use NuGet or otherwise link a platform-specific Realm.dll to your main application.”

I guess this is because the NuGet appears to support PCL and .NET Standard, but not Framework 4.6

I know that Azure backends are really not your thing, but any advice you can give that might help would be appreciated. Is there a .NET Framework 4.6 compatible library, or do you know of any guidance for converting a Framework backend to use .NET Standard?

Do I need to start again with a .NET Standard backend (and will that work) or is there some other workaround?


#5

Okay, a bit more googling and I came across this issue: UnitTests failure with Realm Xamarin #1431

Part of the workaround was a pre-release version of Realm 2.0 which apparently should fix what was acknowledged as a bug. I installed the pre-release version of Realm and it did indeed fix that “PCL Build of Realm is being linked…” error… but of course that was too good to be true!

Now getting:
“exceptionMessage”: “The type initializer for ‘Realms.Sync.SharedRealmHandleExtensions’ threw an exception.”,
“exceptionType”: “System.TypeInitializationException”
… and the inner exception…
“Unable to load DLL ‘realm-wrappers’: The specified module could not be found.”

Now I am stuck, as I’m not sure what this dll is or where to get it or where to put it.

Can anyone here help? And should I list this as an issue on GitHub, or does issue #1431 cover it.


#6

More googling, and…

“Sync is not yet implemented. Using the Realm parent NuGet package in a Windows application will result in build-time errors. Instead, you should install just the Realm.Database package which contains just the offline-capable binaries.” (https://realm.io/docs/xamarin/latest/#current-limitations)

This suggests that I would not be able to connect to the Realm Object Server, but only work with a local realm on a Windows platform.

Does that mean that I cannot, in fact (at least at this time) access a user’s realm on the Realm Object Server from a Windows web app?

Confused - would really appreciate some clarification - thanks :slight_smile:


#7

@nirinchev could you comment…

Is your reply above correct, I CAN access Realms on my .Net server using SyncConfiguration (in which case any idea what I’m doing wrong) or…

actually, I CANNOT access Realms from a .Net server because sychronised Realms are not yet supported on the Windows platform (in which case, any idea of a timeline on adding Windows sync)

Thanks :slight_smile:


#8

Sorry for not replying sooner - was traveling and your comments must have slipped under my radar. To clarify, you can access synchronized Realms from .NET Core app on Linux or Mac. Support for Windows is very close, so stay tuned. Sorry for the confusion - I don’t know why I got the impression you’re hosting your project on Linux and answered without being more specific.


#9

Ah, now I understand - thanks for clarifying.
Glad to hear the Windows update is very close. As the web app is at the end of my development plan and not needed just yet then hopefully that will be fine :slight_smile:


#10

Is this fixed now? I’m struggling to LoginAsync from .NET Core 2 Web app. Getting “The type initializer for ‘Realms.Sync.SharedRealmHandleExtensions’ threw an exception.” when connecting to a realm cloud instance from debug session on mac. What gives?


#11

@nirinchev

Heres the stack trace:

System.TypeInitializationException: The type initializer for ‘Realms.Sync.SharedRealmHandleExtensions’ threw an exception. —> System.EntryPointNotFoundException: Unable to find an entry point named ‘realm_install_syncsession_callbacks’ in DLL ‘realm-wrappers’.
at Realms.Sync.SharedRealmHandleExtensions.NativeMethods.install_syncsession_callbacks(RefreshAccessTokenCallbackDelegate refresh_callback, SessionErrorCallback error_callback, SessionProgressCallback progress_callback, SessionWaitCallback wait_callback)
at Realms.Sync.SharedRealmHandleExtensions…cctor() in /Users/realm/workspace/realm_realm-dotnet_PR-1723/Realm/Realm.Sync/Handles/SharedRealmHandleExtensions.cs:line 136
— End of inner exception stack trace —
at Realms.Sync.SharedRealmHandleExtensions.DoInitialFileSystemConfiguration() in /Users/realm/workspace/realm_realm-dotnet_PR-1723/Realm/Realm.Sync/Handles/SharedRealmHandleExtensions.cs:line 168
at Realms.Sync.User.d__4.MoveNext() in /Users/realm/workspace/realm_realm-dotnet_PR-1723/Realm/Realm.Sync/User.cs:line 88


#12

It appears something wasn’t copied correctly by nuget. Can you try removing the package and re-adding it. Also, for .NET Core, make sure you only reference the Realm package and not Realm.Database as nuget is sometimes getting confused which native binaries to copy to the final project.


#13

aaaand it works! Thanks nirinchev!

I removed the Realm nuget and added it again.