Description
Description
When trying to get data from the clipboard, the following exception is thrown:
System.Runtime.InteropServices.COMException
HResult=0x8001010E
Message=The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at TempClipboardPartialTrust.MainWindow.<Button_Click>d__1.MoveNext() in C:\Users\freddy\source\Repos\TempClipboardPartialTrust\TempClipboardPartialTrust\MainWindow.xaml.cs:line 34
The issue only occurs when the application is packaged and running in partial trust. No exception is thrown when unpackaged or when packaged running in full trust.
Environment Information
.NET SDK version: 5.0.100-rc.1.20452.10
VS Version: Version 16.8.0 Preview 3.1
Repro Steps
- Create a WPF app
- Use 'net5.0-windows10.0.18362.0' as the target framework.
- Add code to get data from the clipboard (e.g. a button handler like the following):
private async void Button_Click(object sender, RoutedEventArgs e)
{
Windows.ApplicationModel.DataTransfer.DataPackageView content = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
Windows.Foundation.IAsyncOperation<string> getTextOperation = content.GetTextAsync();
string s = await getTextOperation;
this.clipboardResultTextBox.Text = s;
}
- (Optional) run the app and observe that the API can get data from the clipboard when not packaged (ensure appropriate data exists on the clipboard)
- Add a "Windows Application Packaging Project" to the solution
- Under the "Applications" node, add a reference to the WPF app
- Change the "Trust Level" in the properties of the referenced app from "Full Trust" to "Partial Trust"
7b. Workaround: Currently, it looks like the packaging project doesn't automatically copy the needed dependencies when targeting 'net5.0-windows10.0.18362.0'. After building the packaging project, manually copyWinRT.Runtime.dll
andMicrosoft.Windows.SDK.NET.dll
to thebin\AnyCPU\Debug\AppX\[AppName]
folder. Omitting this workaround was giving the following message when trying to debug the packaged app:
The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core. The program '[13840] WpfApp1.exe' has exited with code -2147450740 (0x8000808c).
- Run the packaged application and observe the exception being thrown (ensure appropriate data exists on the clipboard)
Edit: packaging project now seems to copy dependencies correctly, so workaround in step 7 should no longer be needed (tested using .NET 5 RC2/Visual Studio 16.8.0 Preview 4)