How to use a custom WCF proxy in a Powershell script with a config file? How to use a custom WCF proxy in a Powershell script with a config file? powershell powershell

How to use a custom WCF proxy in a Powershell script with a config file?


There seems to be a difference in the way that Powershell and Powershell ISE handle this.

With ISE (at least the version I'm using) you have to clear out the configuration to force it to reload. Though, I guess you could also put the contents of your .dll.config file into the powershell ISE config. However, that seems nasty. The code posted below works. I found parts of it googling this issue.

# $dllPath is the path to the dll we want to load# first point to the correct config file[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$dllPath.config")# PowerShell ISE is a PITA we have to override the configif ($psISE -ne $null) {    Add-Type -AssemblyName System.Configuration    [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null,0)    [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null,$null)    ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)}#Now load the DLL$null = [Reflection.Assembly]::LoadFrom($dllPath)# DLL and Config should be loaded - test


Are your assemblies built as 32 bits or 64 bits target?

I met the 32/64 problem in multiple cases.

Just be careful that if you are running on 64 bits OS, you've got two PowerShells a 64 bits (usual) and a 32 bits (interresting when you need 32 external assemblies).


It would be easier if you posted your complete config, but it sounds like you are missing the following section.

<system.serviceModel>    <bindings>      <basicHttpBinding>        <binding name="BasicHttpBinding">          <security mode="TransportCredentialOnly">            <transport clientCredentialType="Windows" />          </security>        </binding>      </basicHttpBinding>    </bindings>    <client>      <endpoint address="ServiceAddress"        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding"        contract="MyService.Contracts.IMyService" name="MyServiceHttpEndpoint" />    </client></system.serviceModel>

Although you sound pretty sure that is not the case.

So lets try and make sure that your powershell app is picking up the config correctly...

To be sure if the powershell app is picking up the config as expected add something like this to your powershell file:

Get-Content $LocalPath\MyService.Client.dll.config | foreach {Write-Output $_}

If it is then its not a config issue, I think we we can agree on that.

So can the dll see the settings in the config? By now we know that powershell can see the config and can call your dll.

Is the config in the same location as the dll?

Does add-type do something we dont expect? Looking at the msdn docs it looks like add-type

Adds a Microsoft .NET Framework type (a class) to a Windows PowerShell session.

If the class is now in a powershell session does it have access to the config like it normally would? I dont know.

Maybe try [Reflection.Assembly]::LoadFrom rather and add-type to see if that makes any difference?

I don't have an exact answer I'm afraid but I hope my ramblings are somewhat helpful.