Hello @DotNET Fan ,
This is my test code for your reference. I use WPF project as fulltrust process. Appservice can be used normally.
In WPF(.net 7.0),
private AppServiceConnection connection = null;
public MainWindow()
{
InitializeComponent();
InitializeAppServiceConnection();
}
private async void InitializeAppServiceConnection()
{
connection = new AppServiceConnection();
connection.AppServiceName = "SampleInteropService";
connection.PackageFamilyName = Package.Current.Id.FamilyName;
connection.RequestReceived += Connection_RequestReceived;
connection.ServiceClosed += Connection_ServiceClosed;
AppServiceConnectionStatus status = await connection.OpenAsync();
if (status != AppServiceConnectionStatus.Success)
{
// something went wrong ...
MessageBox.Show(status.ToString());
this.IsEnabled = false;
}
}
Add Extensions in WAPP Package.appxmanifest.
<Extensions>
<desktop:Extension Category="windows.fullTrustProcess"
Executable="WPFfulltrust\WPFfulltrust.exe"/>
<uap:Extension Category="windows.appService">
<uap:AppService Name="SampleInteropService" />
</uap:Extension>
</Extensions>
In UWP App.xaml.cs, activate the UWP in the background and establish the AppServiceConnection.
public static BackgroundTaskDeferral AppServiceDeferral = null;
public static AppServiceConnection Connection = null;
public static event EventHandler AppServiceDisconnected;
public static event EventHandler<AppServiceTriggerDetails> AppServiceConnected;
public static bool IsForeground = false;
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.EnteredBackground += App_EnteredBackground;
this.LeavingBackground += App_LeavingBackground;
}
private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
{
IsForeground = true;
}
private void App_EnteredBackground(object sender, EnteredBackgroundEventArgs e)
{
IsForeground = false;
}
protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
{
base.OnBackgroundActivated(args);
if (args.TaskInstance.TriggerDetails is AppServiceTriggerDetails details)
{
// only accept connections from callers in the same package
if (details.CallerPackageFamilyName == Package.Current.Id.FamilyName)
{
// connection established from the fulltrust process
AppServiceDeferral = args.TaskInstance.GetDeferral();
args.TaskInstance.Canceled += OnTaskCanceled;
Connection = details.AppServiceConnection;
AppServiceConnected?.Invoke(this, args.TaskInstance.TriggerDetails as AppServiceTriggerDetails);
}
}
}
private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
AppServiceDeferral?.Complete();
AppServiceDeferral = null;
Connection = null;
AppServiceDisconnected?.Invoke(this, null);
}
In UWP MainPage, send message.
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
{
App.AppServiceConnected += MainPage_AppServiceConnected;
App.AppServiceDisconnected += MainPage_AppServiceDisconnected;
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
}
/// <summary>
/// When the desktop process is connected, get ready to send/receive requests
/// </summary>
private async void MainPage_AppServiceConnected(object sender, AppServiceTriggerDetails e)
{
App.Connection.RequestReceived += AppServiceConnection_RequestReceived;
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// enable UI to access the connection
btnRegKey.IsEnabled = true;
});
}
private async void btnClick_ReadKey(object sender, RoutedEventArgs e)
{
ValueSet request = new ValueSet();
request.Add("KEY", tbKey.Text);
AppServiceResponse response = await App.Connection.SendMessageAsync(request);
// display the response key/value pairs
tbResult.Text = "";
foreach (string key in response.Message.Keys)
{
tbResult.Text += key + " = " + response.Message[key] + "\r\n";
}
}
This code refers to StefanWick's blog and code.
Feel free to comment on how there are other questions.
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.