Reading File on Server Caused System.UnauthorizedAccessException

Hello All,
I was trying to read information from an Excel file inside Application Folder.
On Development it went well but on production it keeps giving me the following error:

Failed
An exception occurred during performance of the job.
System.UnauthorizedAccessException

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at iCPO.ProjectsVerification.Verification.ExcelImport.SubImportPlan(String file, String Project)

I gave the IIS_USER full access to application but still the error occurs.
Do I have to give permission to Hangfire service and if yes how to do that
Thanks in advance.

First, you have to give permissions to the user the service is executing as, not to the web application user.

Second, it should be not only file permissions, but also COM Activation permission (since you’re using Excel as COM object). It can be configured with dcomcnfg.msc panel.

But it is not recommended to use Excel as COM object in server application, so you’d better consider using some 3rd-party library for that, like open-source ExcelDataReader, or its commercial analogs.

1 Like