I was working with a co-worker and getting a PowerShell script to call into a WCF service. We had to use svcutil.exe to create the proxy then compile it via csc.exe – he wanted to do everything from a PowerShell script.
When it came time to run the script which would call a WCF service we kept getting errors. One of the features of a .Net executable is that if you have configuration settings in the app.config, they get loaded up for you at run time. If you’ve worked at all with WCF then you know it is configuration heavy!
We needed to get our app.config settings loaded into PowerShell so we wouldn’t have to programmatically configure the bindings, etc. I’m new to PowerShell so we did some digging around and found that you could use the AppDomain.SetData method. Here’s how…
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "path to config file");
If we call that before we load our assembly into PowerShell, our configuration for the WCF service is loaded properly and we can call the service without any errors.
Enjoy!