I’m encountering an issue deploying my .NET Core application that uses Microsoft.Office.Interop.Word via an Azure DevOps pipeline. Locally, I managed to compare Word documents using Interop Word through COMReference, resolving a previous NuGet package issue. However, during deployment through Azure DevOps, I’m facing errors.
Here’s the context:
Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
object wordTrue = (object)true;
object wordFalse = (object)false;
object fileToOpen = @"";
object missing = Type.Missing;
Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
ref missing, ref wordFalse, ref wordFalse, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref wordTrue, ref missing,
ref missing, ref missing, ref missing);
object fileToOpen1 = @"";
Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
ref missing, ref wordFalse, ref wordFalse, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
Word.Document doc = wordApp.CompareDocuments(doc1, doc2, Word.WdCompareDestination.wdCompareDestinationNew, Word.WdGranularity.wdGranularityWordLevel,
true, true, true, true, true, true, true, true, true, true, "", true);
doc1.Close(ref missing, ref missing, ref missing);
doc2.Close(ref missing, ref missing, ref missing);
// Hides both original and revised documents
wordApp.ActiveWindow.ShowSourceDocuments = WdShowSourceDocuments.wdShowSourceDocumentsNone;
wordApp.Visible = true;
Initially, I faced a NuGet package error when using the Microsoft.Office.Interop.Word package, which led me to utilize COMReference as a workaround. While this solution worked locally, the Azure DevOps pipeline fails during the build process with the following error:
Error MSB4803: The task "ResolveComReference" is not supported on the .NET Core version of MSBuild.
Error CS0234: The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
Is there an alternative approach or solution to successfully deploy a .NET Core application using Microsoft.Office.Interop.Word or any workaround to resolve this issue with Azure DevOps? Any guidance on resolving this deployment error would be greatly appreciated.
2
Answers
Okay so my Interop word solution did not work using self-hosted agents in pipeline, so I ran my code in Visual Studio inside a VM and hosted those build files in Windows IIS server.
Here are the steps I followed :-
Publish an ASP.NET core app to IIS : - https://learn.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-8.0&tabs=visual-studio
Host ASP.NET Core on Windows with IIS : - https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1#install-the-net-core-hosting-bundle
After following above two steps and creating an Application pool and Site : -
Open Computer Management: Press Windows + R:
This opens the Run dialog. Type compmgmt.msc and Press Enter:
This will open the Computer Management console. Create a New User: Navigate to "Local Users and Groups" -> "Users":
In the left pane of the Computer Management console, expand "System Tools," then "Local Users and Groups," and finally click on "Users" Right-Click and Choose "New User":
In the right pane, right-click on an empty space and select "New" -> "User"
The "New User" wizard will appear. Enter the information for the new user, including the username and password.
Uncheck the option that says "User must change password at next logon" if you want to set a specific password. Complete the Wizard:
Click "Finish" to complete the wizard. Add User to IIS_IUSRS Group: Navigate to "Local Users and Groups" -> "Groups":
In the left pane of the Computer Management console, under "System Tools," click on "Local Users and Groups," and then select "Groups." Find and Open "IIS_IUSRS" Group:
In the right pane, find the group named "IIS_IUSRS" and double-click on it to open. Add the Newly Created User:
Click "Add" and add the user account you created earlier. Click "OK" to close the dialog.
Press Windows + R to open the Run dialog, type inetmgr, and press Enter. Navigate to Application Pools:
In IIS Manager, click on the server node in the Connections pane. Open "Application Pools" Select Your Application Pool:
Locate and select the application pool your web application is using. Change Identity:
In the right pane, click on "Advanced Settings" Under "Process Model," find the "Identity" property. Click on the ellipsis (...) to change the identity. Choose the user account you created earlier.
Custom Account:
If you choose the "Custom account" option, you need to provide the credentials for a user account. Click on the "Custom account" option. Click on the "Set" button to the right of the "Custom account" field. In the "Set Credentials" dialog, enter the username and password for the user account you created earlier. Click "OK" to close the "Set Credentials" dialog. After choosing either the "Built-in account" or providing the credentials for a "Custom account," make sure to save your changes by clicking "OK" on the "Advanced Settings" page. Following these steps will configure the identity under which the IIS application pool runs.
Restart Application Pool:
After changing the identity, restart the application pool. 3. Configure DCOM Settings: Open Component Services:
Press Windows + R, type dcomcnfg, and press Enter. Navigate to DCOM Config:
In Component Services, expand "Component Services" -> "Computers" -> "My Computer" -> "DCOM Config" Locate Microsoft Word DCOM Component:
Scroll through the list to find the DCOM component with (Microsoft Word 97-2003). Configure Security Settings:
Right-click on the Microsoft Word DCOM component, select "Properties." Go to the "Security" tab. Under "Launch and Activation Permissions" click "Edit Default" Add the user account you created earlier and grant it "Local Launch" and "Local Activation" permissions. Configure Identity:
Go to the "Identity" tab. Choose "This user" and provide the credentials for the user account you created. Apply Changes:
Click "OK" to apply the changes. 4. Verify and Test: Restart IIS:
After making these changes, restart the IIS server. Test Your API:
Access your API and check if the "Access is denied" error is resolved. By following these steps, you are configuring both the IIS application pool and the DCOM settings to use a specific user account with the necessary permissions to interact with Microsoft Word.
to check logs on IIS click window + X ->Event Viewer -> Windows logs -> Applications
Check this Github Issue comments by quicoli and xDaevax.
You can also make use of other libraries like
OpenXml
instead ofOfficeIntercop
that will work well with Azure DevOps pipeline. Here’s an example ofOpenXml
library for comparing two Word Documents..NET 6.0 Web App
and the build Pipeline withVsBuild
was successful:-My Controller Code:-
My Devops Build pipeline:-
Output:-