Skip to content

Update C# Background Task samples for trimming and AOT #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public App()
{
this.InitializeComponent();
Guid taskGuid = typeof(BackgroundTask).GUID;
ComServer.CoRegisterClassObject(ref taskGuid,
new ComServer.BackgroundTaskFactory<BackgroundTask, IBackgroundTask>(),
ComServer.CoRegisterClassObject(in taskGuid,
new ComServer.BackgroundTaskFactory(),
ComServer.CLSCTX_LOCAL_SERVER,
ComServer.REGCLS_MULTIPLEUSE,
out _RegistrationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ namespace BackgroundTaskBuilder
{
// Background task implementation.
// {87654321-1234-1234-1234-1234567890AE} is GUID to register this task with BackgroundTaskBuilder. Generate a random GUID before implementing.
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("87654321-1234-1234-1234-1234567890AA")]
[ComSourceInterfaces(typeof(IBackgroundTask))]
public class BackgroundTask : IBackgroundTask
public partial class BackgroundTask : IBackgroundTask
{
/// <summary>
/// This method is the main entry point for the background task. The system will believe this background task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<!-- This tag is required for the usage of WinAppSDK Background Task API -->
<WindowsAppSDKBackgroundTask>true</WindowsAppSDKBackgroundTask>
<StartupObject></StartupObject>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -51,13 +52,4 @@
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>

<!-- Publish Properties -->
<PropertyGroup>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<PublishTrimmed Condition="'$(Configuration)' == 'Debug'">False</PublishTrimmed>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Windows.ApplicationModel.Background;
using WinRT;

namespace BackgroundTaskBuilder
{
// COM server startup code.
internal class ComServer
internal partial class ComServer
{
[DllImport("ole32.dll")]
public static extern int CoRegisterClassObject(
ref Guid classId,
[LibraryImport("ole32.dll")]
public static partial int CoRegisterClassObject(
in Guid classId,
[MarshalAs(UnmanagedType.Interface)] IClassFactory objectAsUnknown,
uint executionContext,
uint flags,
out uint registrationToken);
[DllImport("ole32.dll")]
public static extern int CoRevokeObject(

[LibraryImport("ole32.dll")]
public static partial int CoRevokeObject(
out uint registrationToken);

public const uint CLSCTX_LOCAL_SERVER = 4;
Expand All @@ -31,40 +34,39 @@ public static extern int CoRevokeObject(
public const string IID_IUnknown = "00000000-0000-0000-C000-000000000046";
public const string IID_IClassFactory = "00000001-0000-0000-C000-000000000046";

[ComImport]
[ComVisible(false)]
[GeneratedComInterface]
[Guid(IID_IClassFactory)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IClassFactory
public partial interface IClassFactory
{
[PreserveSig]
uint CreateInstance(IntPtr objectAsUnknown, ref Guid interfaceId, out IntPtr objectPointer);
uint CreateInstance(IntPtr objectAsUnknown, in Guid interfaceId, out IntPtr objectPointer);

[PreserveSig]
uint LockServer(bool Lock);
uint LockServer([MarshalAs(UnmanagedType.Bool)] bool Lock);
}

internal class BackgroundTaskFactory<TaskType, TaskInterface> : IClassFactory where TaskType : TaskInterface, new()
[GeneratedComClass]
internal partial class BackgroundTaskFactory : IClassFactory
{
public BackgroundTaskFactory()
{
}

public uint CreateInstance(IntPtr objectAsUnknown, ref Guid interfaceId, out IntPtr objectPointer)
public uint CreateInstance(IntPtr objectAsUnknown, in Guid interfaceId, out IntPtr objectPointer)
{
if (objectAsUnknown != IntPtr.Zero)
{
objectPointer = IntPtr.Zero;
return CLASS_E_NOAGGREGATION;
}

if ((interfaceId != typeof(TaskType).GUID) && (interfaceId != new Guid(IID_IUnknown)))
if ((interfaceId != typeof(IBackgroundTask).GUID) && (interfaceId != new Guid(IID_IUnknown)))
{
objectPointer = IntPtr.Zero;
return E_NOINTERFACE;
}

objectPointer = MarshalInterface<TaskInterface>.FromManaged(new TaskType());
objectPointer = MarshalInterface<IBackgroundTask>.FromManaged(new BackgroundTask());
return S_OK;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ namespace BackgroundTaskBuilder
{
// Background task implementation.
// {87654321-1234-1234-1234-1234567890AE} is GUID to register this task with BackgroundTaskBuilder. Generate a random GUID before implementing.
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("87654321-1234-1234-1234-1234567890AE")]
[ComSourceInterfaces(typeof(IBackgroundTask))]
public class BackgroundTask : IBackgroundTask, IDisposable
public partial class BackgroundTask : IBackgroundTask, IDisposable
{
private bool disposed = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<WindowsAppSDKBackgroundTask>true</WindowsAppSDKBackgroundTask>
<DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
<StartupObject>BackgroundTaskBuilder.Program</StartupObject>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand All @@ -40,7 +41,7 @@
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.2454" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.241114004-experimental1" />
</ItemGroup>

Expand All @@ -52,13 +53,4 @@
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>

<!-- Publish Properties -->
<PropertyGroup>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<PublishTrimmed Condition="'$(Configuration)' == 'Debug'">False</PublishTrimmed>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using Windows.ApplicationModel.Background;
using WinRT;

namespace BackgroundTaskBuilder
{
// COM server startup code.
internal class ComServer
internal partial class ComServer
{
[DllImport("ole32.dll")]
public static extern int CoRegisterClassObject(
ref Guid classId,
[LibraryImport("ole32.dll")]
public static partial int CoRegisterClassObject(
in Guid classId,
[MarshalAs(UnmanagedType.Interface)] IClassFactory objectAsUnknown,
uint executionContext,
uint flags,
Expand All @@ -28,40 +30,39 @@ public static extern int CoRegisterClassObject(
public const string IID_IUnknown = "00000000-0000-0000-C000-000000000046";
public const string IID_IClassFactory = "00000001-0000-0000-C000-000000000046";

[ComImport]
[ComVisible(false)]
[GeneratedComInterface]
[Guid(IID_IClassFactory)]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IClassFactory
public partial interface IClassFactory
{
[PreserveSig]
uint CreateInstance(IntPtr objectAsUnknown, ref Guid interfaceId, out IntPtr objectPointer);
uint CreateInstance(IntPtr objectAsUnknown, in Guid interfaceId, out IntPtr objectPointer);

[PreserveSig]
uint LockServer(bool Lock);
uint LockServer([MarshalAs(UnmanagedType.Bool)] bool Lock);
}

internal class BackgroundTaskFactory<TaskType, TaskInterface> : IClassFactory where TaskType : TaskInterface, new()
[GeneratedComClass]
internal partial class BackgroundTaskFactory : IClassFactory
{
public BackgroundTaskFactory()
{
}

public uint CreateInstance(IntPtr objectAsUnknown, ref Guid interfaceId, out IntPtr objectPointer)
public uint CreateInstance(IntPtr objectAsUnknown, in Guid interfaceId, out IntPtr objectPointer)
{
if (objectAsUnknown != IntPtr.Zero)
{
objectPointer = IntPtr.Zero;
return CLASS_E_NOAGGREGATION;
}

if ((interfaceId != typeof(TaskType).GUID) && (interfaceId != new Guid(IID_IUnknown)))
if ((interfaceId != typeof(IBackgroundTask).GUID) && (interfaceId != new Guid(IID_IUnknown)))
{
objectPointer = IntPtr.Zero;
return E_NOINTERFACE;
}

objectPointer = MarshalInterface<TaskInterface>.FromManaged(new TaskType());
objectPointer = MarshalInterface<IBackgroundTask>.FromManaged(new BackgroundTask());
return S_OK;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

using System;
Expand All @@ -22,8 +22,8 @@ static void Main(string[] args)
if (args.Contains("-RegisterForBGTaskServer"))
{
Guid taskGuid = typeof(BackgroundTask).GUID;
ComServer.CoRegisterClassObject(ref taskGuid,
new ComServer.BackgroundTaskFactory<BackgroundTask, IBackgroundTask>(),
ComServer.CoRegisterClassObject(in taskGuid,
new ComServer.BackgroundTaskFactory(),
ComServer.CLSCTX_LOCAL_SERVER,
ComServer.REGCLS_MULTIPLEUSE,
out _RegistrationToken);
Expand Down