Add new AppServiceComponent.RunAsync API
This commit is contained in:
Родитель
e789fd92e8
Коммит
a9357a9d22
|
@ -1,4 +1,4 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
|
@ -17,7 +17,7 @@ namespace CommunityToolkit.AppServices.SourceGenerators;
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// That is, this diagnostic suppressor will suppress the following diagnostic:
|
||||
/// <code>
|
||||
/// <code language="cs">
|
||||
/// public partial class MyAppService : IMyAppService
|
||||
/// {
|
||||
/// public async Task<string> GreetUserAsync()
|
||||
|
|
|
@ -72,6 +72,42 @@ public abstract class AppServiceComponent : IDisposable
|
|||
_appServiceName = appServiceName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a given app service component (in a simplified way).
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of app service component to run.</typeparam>
|
||||
/// <returns>A <see cref="Task"/> representing the app service operaton.</returns>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// This method provides an easy, high-level API in cases where an app service component
|
||||
/// just needs to initialize itself and handle incoming connections, with no additional
|
||||
/// configuration needed. In that case, its entry point can just do:
|
||||
/// <code language="cs">
|
||||
/// await AppServiceComponent.RunAsync<MyAppService>();
|
||||
/// </code>
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// For more advanced cases (eg. when additional logging is required), consider using
|
||||
/// <see cref="InitializeAppServiceAsync"/> directly instead, and handle things manually.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static async Task RunAsync<T>()
|
||||
where T : AppServiceComponent, new()
|
||||
{
|
||||
T appServiceComponent = new();
|
||||
TaskCompletionSource<object?> taskCompletionSource = new();
|
||||
|
||||
// Initialize the app service
|
||||
await appServiceComponent.InitializeAppServiceAsync();
|
||||
|
||||
// Handle connection failures and closures
|
||||
appServiceComponent.ConnectionFailed += (s, e) => taskCompletionSource.TrySetResult(null);
|
||||
appServiceComponent.ConnectionClosed += (s, e) => taskCompletionSource.TrySetResult(null);
|
||||
|
||||
// Wait for the connection to be completed
|
||||
await taskCompletionSource.Task;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the app service.
|
||||
/// </summary>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
|
@ -83,7 +83,7 @@ public abstract class AppServiceHost
|
|||
/// </para>
|
||||
/// <para>
|
||||
/// This method should be used as follows (from <c>App.xaml.cs</c>):
|
||||
/// <code>
|
||||
/// <code language="cs">
|
||||
/// protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)
|
||||
/// {
|
||||
/// base.OnBackgroundActivated(args);
|
||||
|
@ -745,4 +745,4 @@ public abstract class AppServiceHost
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче