Running Winforms application from network share - c#

I have a .Net application that is located on a network drive, when launching it, it immediately crashes. Analyzed the log using WinDBG and this is the exception that caused it:
Exception object: 00000000027806c8
Exception type: System.Security.SecurityException
Message: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
InnerException: <none>
StackTrace (generated):
SP IP Function
000000000018BAC0 0000000000000001 mscorlib_ni!System.Security.CodeAccessSecurityEngine.Check(System.Object, System.Threading.StackCrawlMark ByRef, Boolean)+0x2
000000000018BAC0 000007FEDFD88CC7 mscorlib_ni!System.Security.CodeAccessPermission.Demand()+0x37
000000000018BB00 000007FEDFD8838D mscorlib_ni!System.AppDomainSetup.VerifyDir(System.String, Boolean)+0x8d
000000000018BB50 000007FF001C119B NLog!NLog.Config.ConfigSectionHandler.System.Configuration.IConfigurationSectionHandler.Create(System.Object, System.Object, System.Xml.XmlNode)+0x18b
000000000018BBD0 000007FEDE78EDC2 System_Configuration_ni!System.Configuration.RuntimeConfigurationRecord+RuntimeConfigurationFactory.CreateSectionImpl(System.Configuration.RuntimeConfigurationRecord, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, System.Configuration.ConfigXmlReader)+0x162
000000000018BC20 000007FEDE784566 System_Configuration_ni!System.Configuration.RuntimeConfigurationRecord+RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(System.Configuration.RuntimeConfigurationRecord, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, System.Configuration.ConfigXmlReader)+0x86
000000000018BCA0 000007FEDE78EB4E System_Configuration_ni!System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, System.Configuration.ConfigXmlReader)+0x5e
000000000018BD10 000007FEDE78EA52 System_Configuration_ni!System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, System.Configuration.ConfigXmlReader, System.String, Int32)+0x92
StackTraceString: <none>
HResult: 8013150a
SecurityException Message:
The action that failed was:
Demand
The type of the first permission that failed was:
System.Security.Permissions.FileIOPermission
The first permission that failed was:
<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
version="1"
PathDiscovery="\\172.16.27.229\Apps\MyApp.config"/>
My application doesn't have an SNK defined but it is using NLog which has an SNK.
I also added [assembly: AllowPartiallyTrustedCallers] in NLog so my application will be able to use it.
Using mscorcfg.msc, Runtime security policy, I've given NLog.dll FullTrust.
I also gave my application FullTrust, but the condition that I used was via the Publisher instead of Strong Name. Perhaps that could be the issue, but from the that I got in the exception I believe it has something to do with the MyApp.config.
What am I missing?

Found the solution!
Of course using SNK is great, I believe this is one way to solve the problem.
Apparently the issue that I had was with the fact that I've tried to launch the application from \\172.16.27.229 rather than using \\MyNAS.
I understood it by evaluating the permissions for the given assembly.
When I used the path with \\IP it barely got any permissions, but when I used the \\name it got "Unrestricted" permissions.

Related

Renamed WPF exe throws FileNotFoundException (not typical)

I have a WPF exe that after being built, if the exe is renamed, will not run because a FileNotFoundException is thrown. I have never seen this in ten years of working with .NET executables and this is not supposed to be a problem. Our exe name is built as "Installer.exe", but if we rename it to anything else and try to run it it fails with the following call stack:
Application: test.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
at System.Reflection.RuntimeAssembly._nLoad(System.Reflection.AssemblyName, System.String, System.Security.Policy.Evidence, System.Reflection.RuntimeAssembly, System.Threading.StackCrawlMark ByRef, IntPtr, Boolean, Boolean, Boolean)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(System.Reflection.AssemblyName, System.Security.Policy.Evidence, System.Reflection.RuntimeAssembly, System.Threading.StackCrawlMark ByRef, IntPtr, Boolean, Boolean, Boolean)
at System.Reflection.Assembly.Load(System.Reflection.AssemblyName)
at System.Windows.Navigation.BaseUriHelper.GetLoadedAssembly(System.String, System.String, System.String)
at MS.Internal.AppModel.ResourceContainer.GetResourceManagerWrapper(System.Uri, System.String ByRef, Boolean ByRef)
at MS.Internal.AppModel.ResourceContainer.GetPartCore(System.Uri)
at System.IO.Packaging.Package.GetPartHelper(System.Uri)
at System.IO.Packaging.Package.GetPart(System.Uri)
at System.Windows.Application.GetResourceOrContentPart(System.Uri)
at System.Windows.Application.LoadComponent(System.Object, System.Uri)
at Installer.App.InitializeComponent()
at Installer.App.Main()
If I debug the executable I can further expand that to:
System.IO.FileNotFoundException: 'Could not load file or assembly 'Installer, Version=1.0.0.0,
Culture=neutral' or one of its dependencies. The system cannot find the file specified.'
I have also investigated the Fusion Log:
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\dande\Downloads\test.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = Installer, Version=1.0.0.0, Culture=neutral
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Installer, Version=1.0.0.0, Culture=neutral | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/dande/Downloads/
LOG: Initial PrivatePath = NULL
Calling assembly : PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/dande/Downloads/Installer.DLL.
LOG: Attempting download of new URL file:///C:/Users/dande/Downloads/Installer/Installer.DLL.
LOG: Attempting download of new URL file:///C:/Users/dande/Downloads/Installer.EXE.
LOG: Attempting download of new URL file:///C:/Users/dande/Downloads/Installer/Installer.EXE.
And I have investigated and ruled out the following:
We are not using Assembly.Load to load 'Installer' (which is the executing assembly)
We do not have any pack:// usages in XAML.
We have no code analysis warnings, errors, or XAML errors
It is not related to a application configuration file
It is not related to the application manifest
Application crashes even if I change the code to use a normal Window and not our MainWindow (eliminating a lot of XAML from the equation)
.NET version doesn't make a difference. It's targeting and built for .NET 4.6, but changing this doesn't change the outcome
Confirmed that all dependencies are referenced/present using DotPeek/JustDecompile
Confirmed that all namespaces internally are still 'Installer'
Investigated the partial binding via reading documentation, but this isn't a reference issue, the error indicates its the executing assembly itself that is the issue (it's looking for itself?)
Is not related to code signing as it occurs with or without it
It is not related to strong name signing as we are not using that
Changing the project properties assembly output name but leaving the namespace as Installer works and presents no issues
Things I have observed:
This happens prior to OnStartup. I cannot debug into the application.
It appears to be PresentationCore.dll (from Fusion Log) originating this issue
Confirmed with ProcMon.exe that it is in fact trying to access one of the four logged fusion log paths (Installer.DLL, Installer/Installer.DLL, Installer.EXE, Installer.Installer.EXE)
This occurs on any environment and I can reproduce locally by building the solution then renaming, the output file and then running it
If I copy the executable and rename that and run it it works fine because Installer.exe is present in the same directory
I have never seen this problem before and researching it I have not been successful because I'm just finding all the basic stuff new developers typically run into with reference problems and the like.
My only assumption is that possibly its XAML related but I haven't found anything in XAML out of the ordinary and we don't have any pack:// usages or anything.
Only way we managed to solve this was by adding an AssemblyName attribute to the .csproj file with the name of the assembly after renaming. That way you wouldn't have to do the rename anymore yourself.

XAMARIN ANDROID app got System.ServiceModel.FaultException

I have an app running well, but after I upgrade visual studio 2019 OSX, I got the exception when running the app.
Target _ResolveSatellitePaths:
No way to resolve conflict between "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" arbitrarily.
Target _CopyConfigFiles:
[mono] Unhandled Exception:
[mono] System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Unable to load file or assembly 'DeepCloner, Version=0.10.0.0, Culture=neutral, PublicKeyToken=dc0b95cf99bf4e99' Or one of its dependencies。 The definition of the found component information list does not match the component reference。 (An exception occurred on HRESULT: 0x80131040)
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Unable to load file or assembly'DeepCloner, Version=0.10.0.0, Culture=neutral, PublicKeyToken=dc0b95cf99bf4e99' Or one of its dependencies。 The definition of the found component information list does not match the component reference。 (An exception occurred on HRESULT: 0x80131040)
I knew it's a dll conflict issue. but I can't find a way to fix it. thanks for any information.
Have a try with updating DeepCloner to the latest verson 0.10.2to check whether solve this .
In this release we're removed System.Reflection namespace from
cloning, because some classes in System.Reflection.Emit cause
unexpected crashes in runtime. Also, there are no significant reasons
to clone these classes.
Also, checking for empty constructors is improved to reduce number of
exceptions (internally catched, but can slow down copying).

Getting regular ConfigurationErrorsException

I am using Stackexchange redis for my MVC app and I am getting regular w3wp exception like below. It doesn't happen on production so I wonder if it is something to do with Visual studio?
Using:
Visual Studio Premium 2013
Version: 12.0.31101.00 Update 4
I am using the ASP.NET session state provider provided by Microsoft and in my web.config it is like so: (I am pointing to an azure redis)
<system.web>
<sessionState mode="Custom" customProvider="MySessionStateStore">
<providers>
<add name="MySessionStateStore"
type="Microsoft.Web.Redis.RedisSessionStateProvider"
host="***"
accessKey="***"
ssl="true" />
</providers>
</sessionState>
</system.web>
I get this exception every few minutes when I have the app open.
Application: w3wp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
Stack: at System.Diagnostics.TraceUtils.GetRuntimeObject(System.String, System.Type, System.String)
at System.Diagnostics.TypedElement.BaseGetRuntimeObject()
at System.Diagnostics.ListenerElement.GetRuntimeObject()
at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
at System.Diagnostics.TraceInternal.get_Listeners()
at System.Diagnostics.TraceInternal.WriteLine(System.Object)
at StackExchange.Redis.SocketManager.Read()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart(System.Object)
From exception it looks like it is not coming from session state provider. Problem is due to something else. If exception is due to RedisSessionStateProvider then you would see something like "at Microsoft.Web.Redis.RedisSessionStateProvider*****" in exception.
You can verify this by using different session state. Like try to use "InProc" session state provider and if it still reproduce then it means there is problem with something else and not RedisSessionStateProvider.
If you can provide sample test application that can reproduce this issue and send us on AzureCache#microsoft.com then I can debug it further.

FileLoadException: Could not load file or assembly 'Microsoft.Practices.ObjectBuilder, Version=1.0.51205.0, Culture=neutral, PublicKeyToken=null'

Im having a WebApplication that runs in vs2010 but the target framework is set as 2.0. The application uses enterprise library. when I execute the application in my local I getting an error
Required permissions cannot be acquired.
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.Security.Policy.PolicyException: Required
permissions cannot be acquired.
and in the stack trace I'm getting error
FileLoadException: Could not load file or assembly
'Microsoft.Practices.ObjectBuilder, Version=1.0.51205.0,
Culture=neutral, PublicKeyToken=null' or one of its dependencies.
If is set the target framework as 4.0 the code executes fine. I have tried the following steps :
set the 'Copy to output directory' in the objectbuilder dll property as
copy always (earlier it was do not copy)
tried adding the trust tag with
trust level="high" originUrl="" and also trust level="low"
originUrl=""
copied the below tags from the machine.config of 4.0 to machine.config of 2.0
section name="fullTrustAssemblies"
type="System.Web.Configuration.FullTrustAssembliesSection, System.Web,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
allowDefinition="MachineToApplication"/>
section name="partialTrustVisibleAssemblies"
type="System.Web.Configuration.PartialTrustVisibleAssembliesSection,
System.Web, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
allowDefinition="MachineToApplication"/>
but still getting the error. Please let me know what could be the issue..
prob solved.. i took the EL dlls seperately, gave it strong name & registered the assembly in GAC.. and now the code works fine..

Could not load file or assembly 'System.Web, Version=4.0.0.0, Culture=neutral' or one of its dependencies. Access is denied

Seems like there are already 100s of questions like these.. I have search through quite many of them and they don't seem to suggest anything that I haven't tried yet.
I get the error:
Could not load file or assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Access is denied.
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.IO.FileLoadException: Could not load file or assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Access is denied.
Source Error:
Line 44: private static IKernel CreateKernel()
Line 45: {
Line 46: var kernel = new StandardKernel();
Line 47: try
Line 48: {
The above code is present in my Ninject Initializer.
I have given "Everyone" Permissions on:
C:\Windows\Microsoft.NET\Framework\v4.0.30319
C:\Windows\Microsoft.NET\Framework64\v4.0.30319
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\ .NETFramework
FusionLog Details:
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///F:/TFS/MMSChange/Main/SMRH.MMSChange/SMRH.MMSChange.Web/
LOG: Initial PrivatePath = F:\TFS\MMSChange\Main\SMRH.MMSChange\SMRH.MMSChange.Web\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Found application configuration file (F:\TFS\MMSChange\Main\SMRH.MMSChange\SMRH.MMSChange.Web\web.config).
I have also given everyone permissions on my code's folders. I have tried running ProcMon (which has helped me with the similar problems for different solutions before); but it does not create a single entry for system.web.dll.
I have impersonation built in. I use separate accounts to access the database and that impersonation for the database account breaks this. Without impersonation the app runs fine. (But has no data because all the db calls fail)
Funny thing is though, if I deploy the same code to my local machine and run it via an IIS Web Site: it works. But if I just debug it from my IDE it fails.
I got another instance of ProcMon running and filtered results to show only IISExpress results (Since this is the one which is giving me error).
Then I compiled and launched my application from the IDE and waited for the error to come. After I got the error, I filtered the results by "Access Denied".
Usually I filtered the results to look for system.web.dll, but this time i decided to ignore it.
I found that the only file which was throwing access denied was: "C:\Users\1roj1\Documents\IISExpress\config\aspnet.config" (Where 1roj1 is my user account).
I gave my impersonate user full access to this file and voila everything worked!
I think you need to register .net Freamework 4.0 for IIS for the above issue.
Here are the steps..
Open Command prompt as Administrator
Navigate to "C:\Windows\Microsoft.NET\Framework\v4.0.30319" directory in command prompt
[use the command cd C:\Windows\Microsoft.NET\Framework\v4.0.30319]
Register .net FrameWork for IIS
[use command aspnet_regiis.exe -i]
Restart IIS [use command iisreset].
Done.

Categories

Resources