Word Automation with ASP.NET - asp.net

I have an old application that uses Microsoft Word automation in asp.net and I need to install it on a Windows Server 2012 R2 x64 with Office 2013 Standard x86. I know that Office automation in server technologies like IIS should be avoided but I don't have the green light to rewrite the application at the moment, so I have to take it as-is.
Initially I started the application and it gave this error:
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
I found some information online that {000209FF-0000-0000-C000-000000000046} is the identifier of generic Word, no specific version.
I went to Component Services -> My Computer -> DCOM Config -> Microsoft Word 97 - 2003 Document (note that there is no node for Microsoft Word or MS Word or Word while there is one for Microsoft Excel) and changed the security to allow the IIS AppPool user "Local Launch", "Local Activation" and "Local Access". This makes the application take some time to respond (more than a minute) and fail with the following error:
Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
At the same time the event viewer System log shows:
The server {000209FF-0000-0000-C000-000000000046} did not register with DCOM within the required timeout.
I searched online and found some information that access rights might not be OK, including system drive paths and registry. I ran Process Monitor and checked what the process is trying to open. I gave the IIS AppPool user full access to the following:
C:\Windows\SysWOW64\config\systemprofile
C:\Windows\Temp
HKU.DEFAULT\Software\Microsoft\Office
HKLM\Software\Wow6432Node\Microsoft\Office
HKLM\Software\Wow6432Node\Microsoft\Shared Tools
When I ran the application again it only showed a few access problems in Process Monitor which seem to be just attempts to read different configurations. The application itself did not show any kind of error any more, it just freezed. I don't have Visual Studio installed on that machine, but I don't see any errors in the application logs, so no exceptions were thrown.
I tried also setting a specific user (local administrator) in the DCOM Identity but there was no change.
Now I restored all permissions and I am back to the 0x80080005 error because it at least gives me some information to try to go with.
I succeeded to reproduce the error in a simple application with this code:
try
{
l1.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
var application = new Microsoft.Office.Interop.Word.Application();
l2.Text = "OK";
}
catch (Exception ex)
{
this.Label2.Text = ex.ToString().Replace("\r\n", "<br/>");
}
It fails also on Windows 8 x64 with Office 2013 Pro x64, with exactly the same error.
If I impersonate the current user to be an administrator, then it works. However even adding IIS APPPOOL\DefaultAppPool to the administrators group, it still fails.
Any ideas on the next steps?

So this is what worked at the end:
Create an admin user and set Word to run with it in the DCOM settings
In the settings give activation, launch and access permissions to the AppPool user as well as IIS_IURS and IUSR. Note that if you don't give the rights for the others, only to the AppPool user, Windows will still report that the AppPool user does not have sufficient privileges which is totally misleading
I also figured out what the problem with the freezing Word was: creating the new file was causing Word to open in the special mode where it tells you that the file might be dangerous. Of course using COM you don't see the problem but it would then not be able to continue until you allow it. This is a new behavior in Word 2013 and that's why I didn't have it before.

I had the exact same error with legacy code using Office Interop on Windows Server 2016 with Office 2013 and it drove me mad. So first I want to join those who already mentioned that the best practice would be to not use it and rewrite your code to DocX or similar if you have any chance. It just has too many traps that eat up time.
I followed all hints of Vladimirs post but still got the error. After all it was the doc file templates of the project Office failed to open in Interop mode (but which opened without any problem in the RD session on the server). They were created in an older Office Version. I opened/saved the templates in Word once and everything worked fine.

Related

Excel workbook.saveas inside IIS

I have a problem when I published web application in IIS
sometimes the WorkBook.SaveAs method works fine, sometimes not.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.InteropServices.COMException: The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
Source Error:
workbook.Saved = True
workbook.SaveAs(Server.MapPath("~/MyFolder/Excel.xlsx"))
workbook.Close()
APP.Quit()
and sometimes the error is on APP.Quit
Well the first thing to note, is that office interop is not supported server side by Microsoft, due to both licensing and stability/reliability issues:
Microsoft does not currently recommend, and does not support,
Automation of Microsoft Office applications from any unattended,
non-interactive client application or component (including ASP,
ASP.NET, DCOM, and NT Services), because Office may exhibit unstable
behavior and/or deadlock when Office is run in this environment.
Still, many people do this/try to do this, and run into similar problems as you did. Generally this appears to be caused by permission issues (security context and the DCOM security configuration). For which different potential solutions have been described, such as
How to make IIS7 play nice with Office Interop With a 2 different solutions, one of which is mentioned several times elsewhere:
Open Windows Explorer
Depending on whether you installed a 32bit or
64bit version of office you will need to do one (or both) of the
following:
32bit Office installation: Navigate to
C:\Windows\System32\config\systemprofile
64bit Office installation:
Navigate to C:\Windows\SysWOW64\config\systemprofile
Verify the folder
"Desktop" exists (create it if it's not there)
Right click > Properties
On the security tab: Add the account under which the site
is running (eg: Network Service) with default permissions (Read &
execute; List folder contents; Read)
RPC failure opening an excel document. Listing the following solution:
Type Dcomcnfg.exe in Run which opens the dcomconfig utility.
In the dcomconfig utility,
go to Component Services --> Computers --> My Computer --> DCom Config
--> Microsoft Excel Application (and Powerpoint)
Right click it and go to properties. Go to security tab. Under
Security
1) Launch & Activation Permissions --> Choose customise and click
Edit. Add User Network Service and check everything under Allow for
it.
2) Do the same for Access Permissions and Configuration Permissions.
Then click Apply and close everything.
Another one using Windows Server 2003, with a different solution
It turns out that there was some kind of permission that the COM
Object needed. Setting the AppPool (for the site) on the Win2003
server to "SYSTEM" fixed the problem. For some reasion (by Default)
the ASPNET account has permission to whatever the COM object was
trying to access.
Perhaps one of these solutions will work for you.
The reason it sometimes works for you and not other times, may be related to the contents of the specific excel file, e.g. whether it contains any VBA scripts or references any (specific) modules. So that is something you should look into.

Microsoft Release Management Server for TFS 2013 - install issue

I've been trying to install the new Microsoft Release Management app that has just been released to MSDN and am having issues
It consists of a server side component for TFS 2013, and a client side component for VS2013
The server side component runs through and says that it is installed fine, but then i can't seem to connect the client side part up to it
After a bit of investigation it seems that the web services site that is installed on the server side is not working properly
It is installed on a Windows 2012 box that is also running TFS 2013, SQL 2012, and SharePoint 2013
If i browse to the newly created ReleaseManagement website I get a 503 (Service Unavailable) message.
I checked out IIS and the app pool had stopped
in the application event log I could see a load of messages saying "current configuration only supports loading images for x86 processor architectures"
I thought this was possibly to do with the "Enable 32-bit Applications" option in the IIS app pool (was set to FALSE)
I changed that to true and tried the site again. This time the app pool doesn't crash, but I get ASP.NET errors stating "Could not load file or assembly 'Interop.ActiveDs' or one of its dependencies. An attempt was made to load a program with an incorrect format."
Has anyone managed to install this yet? Any ideas on what's going wrong here?
I know it's very new, but I'm struggling to find much information about the product at all
Thanks a lot
Here is a link towards some documentation regarding this problem. As pointed out in the comments, it is related to a bug caused by SharePoint: http://support.inreleasesoftware.com/entries/24794668
Interesting section:
The request failed with HTTP status 503: Service Unavailable.
This error message usually means that the InReleaseAppPool application
pool is stopped. Please ensure that it is started. If the application
pool stops after some time by itself, it might be due to a bug caused
by SharePoint 2013. Validate your problem by looking at the Event
Viewer. Confirm that your error is similar to the following:
Event Log Error Event ID 2282
The Module DLL 'C:\Program Files\Common Files\Microsoft Shared\Web
Server Extensions\15\isapi\spnativerequestmodule.dll' could not be
loaded due to a configuration problem. The current configuration only
supports loading images built for a x86 processor architecture. The
data field contains the error number. To learn more about this issue,
including how to troubleshooting this kind of processor architecture
mismatch error, see http://go.microsoft.com/fwlink/?LinkId=29349.
We can apply a quick fix using this command
appcmd.exe set config -section:system.webServer/globalModules
/[name='SPNativeRequestModule'].preCondition:integratedMode,bitness64
Or you can manually enter the preCondition entry in the
ApplicationHost.config file
More information can be found here:
http://blogs.flexnetconsult.co.uk/colinbyrne/2012/11/11/RunningA32bitApplicationAlongsideSharePoint2013Problems.aspx
FIX:
the user which you specified to run the TFS service needs "log on as a service" and "Log on as batch job" permissions. I've set that up via GPO.
This solution worked for me. I execute this command
appcmd.exe set config -section:system.webServer/globalModule/[name='SPNativeRequestModule'].preCondition:integratedMode,bitness64
in C:\Windows\System32\inetsrv and C:\Windows\System64\inetsrv.

ASP.Net: MySQL Error SecurityException: System.Security.Permissions.SecurityPermission

I am receiving the following error message:
Server Error in '/' Application.
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: System.Security.Permissions.SecurityPermission
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SecurityException: System.Security.Permissions.SecurityPermission]
MySql.Data.MySqlClient.MySqlClientFactory..cctor() +23
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
MySQL: mysql-connector-net-6.7.4
The problem only occurs if run from the shared web hosting environment. The site works great locally. I know the version of the .Net connector that my shared hosting environment uses, as that was another issue that I resolved and I was in communication with the hosting provider.
The error is interesting. I can go to the default page, no issues, the very first time. I then go to bring up a popup dialog, which throws another error message box.
Exception has been thrown by the target of an invocation.
The above is everything, except for the okay button.
I can bring up the first popup, a sign in dialog, which comes up. That dialog does not invoke MySQL. I bring up the second dialog, a registration dialog, which then throws the "Exception has been thrown..." message box. After that, I cannot go back into the sign-in dialog, as I get the "Exception has..." message box. If I press F5 to refresh the browser (IE or Chrome), I get the error from above. The above error indicates MySQL and some permissions.
I am suspecting that the "Exception has been thrown..." error is the result of the same MySQL, just the page has memory. Closing the browser window and launching the browser window again does not help, although on Chrome that works, just the browser window there really has to close down all the way.
I am in a shared hosting environment, so I have access to basically nothing, so I do not have access to any logs, at least that I can think of.
One final thought that may or may not be relevant. Yesterday and previous days my development work was done on a system with Windows 7 Professional and Visual Studio 2012 Professional (all latest service packs and updates), whereas today I am working at a different computer of mine, which is a system with Windows 8 Professional and Visual Studio 2012 Ultimate (all latest service packs and updates on the OS and VS). I was reading some other posts on unrelated issues that people with VS2012 Ultimate had issues, so I am not sure that has anything to do with it, but that is something that changed from yesterday to today.
Yes, I deleted everything off of the server and uploaded everything anew. I did a clean solution first, built the release, and then published. I still received the same error.
Any thoughts?
By default most hosts use "Medium" trust level. You can change your web.config as below to get full trust:
<configuration>
<system.web>
<trust level="Full" />
</system.web>
</configuration>

ASP.NET virtual directory error

I set up virtual directory in IIS 6. The asp.net setting for my application pool is 4.0.
When I try to access my site I get the following error.
Server Application Unavailable
The web application you are attempting to access on this web server is
currently unavailable. Please hit the "Refresh" button in your web
browser to retry your request.
Administrator Note: An error message detailing the cause of this
specific request failure can be found in the application event log of
the web server. Please review this log entry to discover what caused
this error to occur.
When I check the event application log I get the error below.
Failed to execute request because the App-Domain could not be created. Error: 0x80070005 Access is denied.
It looks like some sort of permissions error but I can't figure out what. Anyone have any ideas?
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.
Use ProcMon from Microsoft to trace the file access of your web server worker process (aspnet_wp.exe on XP, w3wp.exe on Win7, 2003, 2008). If you filter where Process Name contains one of those two EXE names, you will eventually see an ACCESS DENIED entry in the status column. This will tell you what folder/file that your web server app cannot access or open.
Tip: for easier reading of results, turn off the registry and network tracing in ProcMon before doing your test.
You can try giving the IIS_WPG group (or whatever group the application is running as) read/write access to you app directory and see if it helps.
Your pool is set as .NET 4 framework but is your application set to using .NET 4?
Verify the ASP.NET tab is set to correction version on your virtual directory properties:
Also check to see that your ASP.NET user has the correct access by:
Right click the website folder in IIS
Select Permissions
Select Network Service
Permit read access to the user that the ASP.NET process runs as.
You are probably using a custom user or else the persmissions would most likely be correct out of the box.

ASP.NET App Exports to Excel (or not in this case)

Exising .net 2 app migrated to .net 4 and moved to an IIS7 Windows 2008 R2 server.
We used to be able to run some code on the web page to export some stuff to excel.
Now, when we do... we get the following error:
The machine-default permission settings do not grant Local Activation permission for the COM Server application with CLSID {00024500-0000-0000-C000-000000000046} and APPID
Unavailable to the user d"omain\username" SID (S-1-5-21-2084383492-816144152-925700815-4150) from address LocalHost (Using LRPC). This security permission can be modified using the Component Services administrative tool.
Any thoughts?
I did wonder if it may be to do with the authenticated user being on a different domain to the webserver - where we see this error??
Try following this resolution:
Microsoft Support
I believe that the basics of the error are the same. The user you are trying to run the DCOm component as does not have permission to start it.
Restarted the web server and this error no longer appears... I get another error when I try and do the workbook save... I'll post another question on that.
Thanks for your help

Resources