Could not load file or assembly 'Microsoft.PowerShell.Security' or one of its dependencies. The system cannot find the file specified - powershell

I'm running PowerShell from a C# console app. My dev box has PowerShell 3.0 - the target machines will probably have v2.0. My project file has a reference to the assembly:
<Reference Include="System.Management.Automation" />
which seems to be the recommended way. I'm getting a System.IO.FileNotFoundException when I call runspace.Open():
using (var runspace = RunspaceFactory.CreateRunspace(initialSessionState))
{
runspace.AvailabilityChanged += runspace_AvailabilityChanged;
runspace.StateChanged += runspace_StateChanged;
runspace.Open();
...
}
The exception details are:
Message: Could not load file or assembly 'Microsoft.PowerShell.Security' or one of its dependencies. The system cannot find the file specified.
FusionLog:
=== Pre-bind state information ===
LOG: User = MYDOMAIN\MyUser
LOG: DisplayName = Microsoft.PowerShell.Security
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Microsoft.PowerShell.Security | 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/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\MyUser.MYDOMAIN\Projects\PoSh\PoShRunner1\bin\Debug\PoShRunner1.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\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/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security.EXE.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.EXE.
If I turn off Common Language Runtime Exceptions in VS 2012, I get
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.DllNotFoundException' occurred in System.Management.Automation.dll
A first chance exception of type 'System.Management.Automation.Host.HostException' occurred in System.Management.Automation.dll
in the output log.
I've had a look at the document referenced in the fusion log but most of it doesn't seem to apply to this case or help much with my understanding. Microsoft.PowerShell.Security is in the GAC so what's the problem?

The problem is that you compiled against the powershell 3.0 system.management.automation (s.m.a.) assembly - 3.0.0.0 - instead of the powershell 2.0 s.m.a which is 1.0.0.0.
Open your project, browse to the 1.0.0.0 version and recompile. PowerShell 3.0 installs both 1.0.0.0 and 3.0.0.0 s.m.a assemblies to the GAC. Then your program will work on systems with either 2.0 or 3.0 versions of powershell.

Related

How could I load MongoDB Client and start a connection inside F# Interactive?

I'm using netcore 3.1, on macOS Catalina 10.15.6, and JetBrains Rider 2020.2 .
I tried starting a MongoDB connection with the following:
#r "../../../.nuget/packages/dnsclient/1.3.1/lib/net471/DnsClient.dll"
#r "../../../.nuget/packages/mongodb.driver.core/2.11.0/lib/net452/MongoDB.Driver.Core.dll"
#r "../../../.nuget/packages/mongodb.bson/2.11.0/lib/net452/MongoDB.Bson.dll"
#r "../../../.nuget/packages/mongodb.driver/2.11.0/lib/net452/MongoDB.Driver.dll"
let connection = MongoDB.Driver.MongoClient "someConnectionString"
however, when executed, there's still a problem loading the DnsClient assembly, as in the error message below:
System.TypeInitializationException: The type initializer for 'MongoDB.Driver.Core.Misc.DnsClientWrapper' threw an exception.
---> System.IO.FileNotFoundException: Could not load file or assembly 'DnsClient, Version=1.3.1.0, Culture=neutral, PublicKeyToken=4574bb5573c51424'. The system cannot find the file specified.
File name: 'DnsClient, Version=1.3.1.0, Culture=neutral, PublicKeyToken=4574bb5573c51424'
at MongoDB.Driver.Core.Misc.DnsClientWrapper..ctor()
at MongoDB.Driver.Core.Misc.DnsClientWrapper..cctor()
--- End of inner exception stack trace ---
at MongoDB.Driver.Core.Misc.DnsClientWrapper.get_Instance()
at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString)
at MongoDB.Driver.MongoUrlBuilder.Parse(String url)
at MongoDB.Driver.MongoUrlBuilder..ctor(String url)
at MongoDB.Driver.MongoUrl..ctor(String url)
at MongoDB.Driver.MongoClientSettings.FromConnectionString(String connectionString)
at MongoDB.Driver.MongoClient..ctor(String connectionString)
at <StartupCode$FSI_0181>.$FSI_0181.main#()
Stopped due to error
How to make the MongoDB.Driver.Core refer to the right path for DnsClient?
Try restore/install all your drivers(mongoDB.Driver.Core and ones depending on),
I got the same error with the following situation :
I have a DAL class library with dependency packages AspNetCore.Identity.MongoDBCore(2.1.1) and MongoDB.Driver.Core(2.11.0), An API project which lacked the Identity.MongoDBCore(2.1.1) but had the MongoDB.Driver.Core(2.11.0),
The Dependency container thrawed the DNSClient exception,{"The type initializer for 'MongoDB.Driver.Core.Misc.DnsClientWrapper' threw an exception."}

Service running on one server but not on another ~ The type name or alias xxx could not be resolved

I have an issue with a service. I am installing the service via an Advanced Installer installation file, and after installing it, it runs just fine on the development server.
However, when trying to install the service using the same installation file on a different server, the service crashes during startup with the following error message:
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
at Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(System.String, Boolean)
at Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType()
at Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(Microsoft.Practices.Unity.IUnityContainer)
at Microsoft.Practices.Unity.Configuration.ContainerElement+<>c__DisplayClass1.<ConfigureContainer>b__0(Microsoft.Practices.Unity.Configuration.ContainerConfiguringElement)
at Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.Collections.Generic.IEnumerable`1<System.__Canon>, System.Action`1<System.__Canon>)
at Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(Microsoft.Practices.Unity.IUnityContainer)
at Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(Microsoft.Practices.Unity.IUnityContainer, System.String)
at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(Microsoft.Practices.Unity.IUnityContainer, Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, System.String)
at Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(Microsoft.Practices.Unity.IUnityContainer, Microsoft.Practices.Unity.Configuration.UnityConfigurationSection)
at NFCommonImplementation.Factory.NfUnityContainer..ctor()
at NFCommonImplementation.Factory.NfUnityContainer.get_Instance()
at NFObjectFactory.CommonObjectFactory..ctor()
at NFObjectFactory.CommonObjectFactory.<.cctor>b__0()
at System.Lazy`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].CreateValue()
at System.Lazy`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].LazyInitValue()
at System.Lazy`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].get_Value()
at NFDataImporter.ImportService.<Start>b__2()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
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()
Using the console to manually start the service file (which uses topshelf), specifies the error as follows:
Topshelf.Hosts.ConsoleRunHost Critical: 0 : The service threw an unhandled exception, System.InvalidOperationException: The type name or alias INetReportDao could not be resolved. Please check your configuration file and verify this type name.
I've already checked the unity configuration file, which reads:
[...]
<typeAlias alias="INetReportDao"
type="NFCommonInterfaces.Database.DAO.INetReportDao, NFCommonInterfaces, Version=6.0.0.1, Culture=neutral, PublicKeyToken=c98e951d4a9d1d0a" />
[...]
<typeAlias alias="NetReportDao"
type="NFCommonImplementation.Database.DAO.NetReportDao, NFCommonImplementation, Version=6.0.0.1, Culture=neutral, PublicKeyToken=c98e951d4a9d1d0a" />
[...]
<register type="INetReportDao" mapTo="NetReportDao">
</register>
[...]
I've also checked that the NFCommonImplementation.dll and NFCommonInterfaces.dll are located right next to the service executable.
Now I'm at my wit's end. Theoretically, it should work since the installer installs the exact same version of the service on both servers. However, for some reason it works on one server, and not on another.
Could it be that I'm missing dependencies? The installer already automatically recognizes a dependency on Visual C++ Redistributable for Visual Studio 2012 and installs it. Could there be other dependencies required for this that might be installed on the development server and not the test server which could cause this problem?
Okay, after some investigation, I figured out how to fix the issue, and after some more investigation, I found out what had caused it:
The Fix:
The missing .dlls have to be installed into the GAC. The easiest way to do so is using the followinh PowerShell script:
Set-location "[dll file location]"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("[dll file name]")
The Cause:
On the development server, the build process installed the .dlls in question into a custom assembly cache which the service was able to access if run on that server. However, it naturally was not able to find those files on any other server.
I hope this helps others who are experiencing similar difficulties.

Cannot [System.Type]::GetType of type from assembly System.Configuration

I'm trying to register soap extension programatically in powershell, like it is described here, but I have problem with this one particular method:
[System.Type]::GetType("System.Configuration.ConfigurationElementCollection, System.Configuration", $true)
Exception calling "GetType" with "2" argument(s): "Could not load file or assembly 'System.Configuration' or one of its dependencies. The system cannot find the file specified."
This is output from Fusion Log Viewer when running this line directly in powershell.exe:
*** Assembly Binder Log Entry (9/20/2013 # 10:42:57 AM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorwks.dll
Running under executable C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = xxx
LOG: DisplayName = System.Configuration
(Partial)
LOG: Appbase = file:///C:/WINDOWS/system32/WindowsPowerShell/v1.0/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: The same bind was seen before, and was failed with hr = 0x80070002.
ERR: Unrecoverable error occurred during pre-download check (hr = 0x80070002).
This is output from Fusion Log Viewer when running this PowerGUI Script Editor:
*** Assembly Binder Log Entry (9/20/2013 # 10:43:38 AM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\PowerGUI\ScriptEditor.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = xxx
LOG: DisplayName = System.Configuration
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Configuration | 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:/Program Files (x86)/PowerGUI/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = ScriptEditor.exe
Calling assembly : System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\PowerGUI\ScriptEditor.exe.Config
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:/Program Files (x86)/PowerGUI/System.Configuration.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/PowerGUI/System.Configuration/System.Configuration.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/PowerGUI/System.Configuration.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/PowerGUI/System.Configuration/System.Configuration.EXE.
LOG: All probing URLs attempted and failed.
*** Assembly Binder Log Entry (9/20/2013 # 10:43:38 AM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Program Files (x86)\PowerGUI\ScriptEditor.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = xxx
LOG: DisplayName = System.Configuration
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System.Configuration | 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:/Program Files (x86)/PowerGUI/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = ScriptEditor.exe
Calling assembly : System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Program Files (x86)\PowerGUI\ScriptEditor.exe.Config
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:/Program Files (x86)/PowerGUI/System.Configuration.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/PowerGUI/System.Configuration/System.Configuration.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/PowerGUI/System.Configuration.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/PowerGUI/System.Configuration/System.Configuration.EXE.
LOG: All probing URLs attempted and failed.
Of course assembly is present in GAC:
The Global Assembly Cache contains the following assemblies:
System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL
The same problem occurs if I use assembly full name in powershell line above.
You need to specify the assembly-qualified type name, unless the type is in the current assembly or mscorlib.dll. This should work:
[System.Type]::GetType("System.Configuration.ConfigurationElementCollection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", $true)

Server Error loading HttpModule

I am stumped and need some suggestions in resolving the following error
I am getting in my application that is configured to use
an HttpModule that I have created:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Could not load type 'namespaceResponseModifier.ResponseModifer'.
...
Exception Details: System.Web.HttpException: Could not load
type 'namespaceResponseModifier.ResponseModifer'.
...
Stack Trace:
[HttpException (0x80004005): Could not load type 'namespaceResponseModifier.ResponseModifer'.]
...
[ConfigurationErrorsException: Could not load type 'namespaceResponseModifier.ResponseModifer'.]
...
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
The web server is IIS Express 7.0.
In the web.config file, the code for the element is:
<system.webServer>
<modules>
<add name="ResponseModifier"
type="namespaceResponseModifier.ResponseModifer" />
</modules>
</system.webServer>
The assembly for the HttpModule resides in
C:\...\HttpModulePreSendRequestContentCS\Bin\ResponseModifier.dll
I have also tried placing the source for the HttpModule in the App_Code directory
but I am still getting Server Error on loading the HttpModule.
In this 2nd scenario, I am getting the following error:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Could not load type 'namespaceResponseModifier.ResponseModifer' from assembly 'App_Code'.
...
Exception Details: System.TypeLoadException: Could not load
type 'namespaceResponseModifier.ResponseModifer' from assembly 'App_Code'.
...
Stack Trace:
[TypeLoadException: Could not load type 'namespaceResponseModifier.ResponseModifer' from
assembly 'App_Code'.]
...
[ConfigurationErrorsException: Could not load type 'namespaceResponseModifier.ResponseModifer'
from assembly 'App_Code'.]
...
[HttpException (0x80004005): Could not load type 'namespaceResponseModifier.ResponseModifer'
from assembly 'App_Code'.]
...
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
I have also tried using the Visual Studio Development Web Server instead of the IIS Express Web Server.
I tried this server with both scenarios:
- with the assembly in the bin folder
- with the source in the App_Code folder.
These scenarios respectively produced the following Errors:
Server Error in '/HttpModulePreSendRequestContentCS' Application.
--------------------------------------------------------------------------------
Configuration Error
...
Parser Error Message: Could not load type 'ResponseModifer'.
(C:\...\HttpModulePreSendRequestContentCS\web.config line 8)
...
Source File: C:\...\HttpModulePreSendRequestContentCS\web.config Line: 8
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
Server Error in '/HttpModulePreSendRequestContentCS' Application.
--------------------------------------------------------------------------------
Configuration Error
...
Parser Error Message: Could not load type 'ResponseModifer' from assembly 'App_Code'.
(C:\...\HttpModulePreSendRequestContentCS\web.config line 8)
...
Source File: C:\...\HttpModulePreSendRequestContentCS\web.config Line: 8
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
Any suggestions as to what I can do to resolve the problem would be greatly appreciated.
Old question, but I believe your module add is incorrect.
<add name="NameOfModule" type="Namespace.NameOfModule, AssemblyName" />
Is the typical way I see it, so not knowing your namespace for this module, it'd look closer to:
<add name="ResponseModifier" type="ActualNameSpaceGoesHere.namespaceResponseModifier.ResponseModifer, ResponseModifier" />

Object.GetType throws NullReferenceExceptions

I'm trying to use Pex and Moles in my SharePoint project. However I'm facing problem when running Pex Explorations. I'm getting error:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Type System.Object.GetType()
Additionaly in "Pex Exporations result" window i'm seeing 2 Reflection issues:
System.TypeLoadException: Method 'get_TargetType' in type 'System.ComponentModel.Moles.SBaseNumberConverter' from assembly 'System.Moles, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0ae41878053f6703' does not have an implementation.
at an attribute that decorates
[module System.Moles.dll]
and
System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
at an attribute that decorates
[module System.Moles.dll]