The application called an interface that was marshalled for a different thread

Hello
I build UWP with Facebook Login feature. I use winsdkfb.dll for Facebook Login feature. When I click Login game throw error

The application called an interface that was marshalled for a different thread

Here is my code for login button using winsdkfb.dll

void Start()
    {
        btnLogin.onClick.AddListener(() => FacebookLogin());
    }

public void FacebookLogin()
    {
#if UNITY_WSA && ENABLE_WINMD_SUPPORT
        FBSession sess = FBSession.ActiveSession;
        sess.FBAppId = "xxxxxxxxxxxxxxxx";
        sess.WinAppId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        List<string> permissionList = new List<string>();
        permissionList.Add("public_profile");
        permissionList.Add("user_friends");
        permissionList.Add("user_likes");
        permissionList.Add("user_location");
        permissionList.Add("user_photos");
        permissionList.Add("publish_actions");
        FBPermissions permissions = new FBPermissions(permissionList);

        var result = await sess.LoginAsync(permissions);
#endif
}

What I understand is (CMIIW) uwp unity hase two thread. Essentially the ‘UI thread’ is the main thread from the point of view of the WinRT/UWP runtime. In order not to block that thread, all Unity-specific code - MonoBehaviour scripts, coroutines, etc - runs on a separate thread, the ‘application thread,’ which is the main thread from the point of view of the Unity engine. So possibly that error because implement of winsdkfb.dll need to be run on ‘UI Thread’. I try to put implementation of winsdkfb in InvokeOnUIThread

void Start()
    {
        btnLogin.onClick.AddListener(() => FacebookLogin());
    }
public void FacebookLogin()
    {
#if UNITY_WSA && ENABLE_WINMD_SUPPORT
        UnityEngine.WSA.Application.InvokeOnUIThread(() => OnFacebookLogin(),
                waitUntilDone: true);
#endif
}

#if UNITY_WSA && ENABLE_WINMD_SUPPORT
    async private void OnFacebookLogin()
    {
        FBSession sess = FBSession.ActiveSession;
        sess.FBAppId = "xxxxxxxxxxxxxxxx";
        sess.WinAppId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        List<string> permissionList = new List<string>();
        permissionList.Add("public_profile");
        permissionList.Add("user_friends");
        permissionList.Add("user_likes");
        permissionList.Add("user_location");
        permissionList.Add("user_photos");
        permissionList.Add("publish_actions");
        FBPermissions permissions = new FBPermissions(permissionList);

        var result = await sess.LoginAsync(permissions);
}
#endif

But the issue is not solved.

Your understanding of UWP threading (in Unity) is correct and generally invoking APIs on the UI thread will address these threading errors.

In this case, it appears you’re using MediaCapture, or at least the FaceBook API is using it. MediaCapture has an extra catch: it’s not an “Agile” API and all of it’s functions must be invoked on the same thread it was created on. Note that I’m just going off on the name of your method CaptureImageByMediaCapture and don’t know how MediaCapture is actually being used here.

To fix the problem:

  • Make sure MediaCapture (somehow) is initialized on the UI thread
  • All calls that invoke MediaCapture APIs (directly or indirectly) are made from the UI thread

Sorry for the naming, it should be OnFacebookLogin, not CaptureImageByMediaCapture
Let’s call it OnFacebookLogin from now on. (I just edited it)

Doesn’t UnityEngine.WSA.Application.InvokeOnUIThread makes my OnFacebookLogin method runs on the UI thread ?

Yes, the code block will run on the UI thread and the LoginAsync should also run on the UI thread. By default C# await schedules the code to run on the same thread (context) as the invocation.

In general though, the The application called an interface that was marshalled for a different thread does mean you’re trying to call a non-Agile WinRT method from a different thread than the object was created on.

So something within winsdkfb.dll is using a non-Agile API (MediaCapture isn’t the only one).

I found the GitHub repo for winsdkfb.dll and briefly looked through the sample code, but unfortunately nothing stands out to why you’re still getting the error. LoginAsync simply spawns a bunch of other tasks and task chains to perform the authentication and login operations.

Can you debug the script and/or IL2CPP to see where exactly this error is coming from? The LoginAsync may just be a red herring.

Hello, sorry, I miss read the log after I use . UnityEngine.WSA.Application.InvokeOnUIThread
at first yes I got this error
The application called an interface that was marshalled for a different thread

but after I use UnityEngine.WSA.Application.InvokeOnUIThread , I got different error :

Does this mean I can’t acces ActiveSession from FBSession ?

Something is wrong with the integration of the native library with Unity; basically it looks like it’s calling an invalid native address for ActiveSession. From GitHub code, it should be executing this C++/CX code to initialize and return FBSession object:

static property FBSession^ ActiveSession
{
FBSession^ get()
{
static FBSession^ activeFBSession = ref new FBSession();
return activeFBSession;
}
}```

Are you building this DLL yourself or using pre-built versions?
Are you sure you're matching the CPU architecture (x64, x86, etc.) of the DLL with Unity's build?
Does this library work in a non-Unity project, i.e. a simple UWP app from Visual Studio?

BTW you're not the first to have trouble integrating this library with Unity, several others have reported (different) issues on the Unity forums in the past:

https://discussions.unity.com/t/637239
https://discussions.unity.com/t/592935
https://discussions.unity.com/t/614126

AFAIK *nobody* has succeeded in getting this library to work in Unity. Since the code is old and the GitHub repo is "archived" there's probably no support for it right now. I recommend finding another FB plugin.

No, I download it from NuGet Gallery | winsdkfb 0.15.0

Yes

Yes, it works.

BTW you're not the first to have trouble integrating this library with Unity
I know, I ask about how to integrating this lib in this thread winsdkfb can't be load in unity . I though it was succesfull just because no compile error or crash at runtime. Not trying login at that time. After Im trying to login and got error, I made this thread.

Sadly I can’t find any FB Plugin for UWP except this one. There is NuGet Gallery | Microsoft.Toolkit.Uwp.Services 7.0.0-preview2 but this library dependen to this winsdkfb library.

Unfortunately, I’m out of ideas on this problem, and I’d have to debug it myself to figure out why it’s failing (unfortunately don’t have time for that right now).

One last suggestion: build your Unity project as a XAML app (instead of D3D) and use the XAML controls/code for logging in and managing the FB session. Since XAML always runs on the UI thread, this would hopefully sidestep the problem in Unity. This probably isn’t what you’d like for your app but it could help get things working.

Thank you for your support and ideas.
I’ll share to you if I can make this work.

Wish me luck!