8

I want to use TaskScheduler using "TaskScheduler 1.1 Type Library" from Add COM Reference.

using TaskScheduler;
...
     ITaskService taskservice = null;
     taskservice = new TaskScheduler.TaskScheduler(); <=NotSupportedException
     taskservice.Connect(null, null, null, null);

But the above code will cause an exception.

System.NotSupportedException: 'Built-in COM has been disabled via a feature switch. See https://aka.ms/dotnet-illink/com for more

If PublishTrimmed is set to false, no exception will be raised. However, I don't want to set it to false because it will increase the size of the exe.

project file is following

 <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>false</SelfContained>
    <PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>

I try to add assembly to TrimmerRootAssembly. But following is not worked.

<ItemGroup>
    <TrimmerRootAssembly Include="TaskScheduler" />
    <TrimmerRootAssembly Include="Microsoft.Win32.TaskScheduler" />
</ItemGroup>

1 Answer 1

10

Seems like trimming with COM references has been disabled by default due to issues it might cause.
You can re-enable it by adding <BuiltInComInteropSupport>true</BuiltInComInteropSupport> to your PropertyGroup in your csproj file.

You can also use ComWrappers to get proper AOT interop solution in .NET 5 - https://learn.microsoft.com/en-us/dotnet/standard/native-interop/tutorial-comwrappers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.