A PowerShell Script to Assist WCF Service Hosting / Testing

When developing for WCF, I find situations in which I need to manually host the WCF services.  (“Manually” == “not from within Visual Studio”).  Sometimes I have to go back and forth between different services, etc.  The command I really wanted was to be able to “just host from here.”  So, I created a simple PowerShell script that does just that.

WCF-HostMe starts in the current directory and looks for a service to host.  It simply looks for a config file (matching *.[exe|dll].config) and assumes that the config file’s name-matching exe or dll is the service.  After formatting and building the parameter values, it launches the service using WcfSvcHost.exe.

Again, this is a simplistic approach and really only works well for development and testing purposes.  Obviously this is not a good approach for production environments.

 

###### WCF-HostMe.ps1
#    Hosts a WCF service using WCF Service Host (WcfSvcHost.exe) for testing purposes.
#    How it works:
#        Beginning in current dir, recursively searches for *.exe.config or *.dll.config
#        Assuming the .config file is asso'd with the WCF service, launches WCFSvcHost
#            using the service's and config's paths
#
#    History:
#        3/16/2012, J Burnett    Created
#####

# Find .config file
# TODO: handle multiple search results
# TODO: detect assembly is not hostable? (not WCF, WF)
$configPath = (gci . -include *.exe.config, *.dll.config -recurse).VersionInfo.FileName

# Build WCFSvcHost param inputs - the full paths of service & config
$serviceArg = (" /service:""" + $configPath -replace '.config','') + """ "
$configArg = " /config:""$configPath"" "

# Launch WCFSvcHost to host the service
echo ''
echo "Attempting to host $serviceArg as WCF Service..."
start-process WcfSvcHost.exe -ArgumentList ($serviceArg + " /config:""$configPath"" ")
echo ''


### Copyright & Disclaimer
#####
# This software is provided "as is"; there are no warranties of any kind.  This software 
# may not work correctly and/or reliably in some environments. In no event shall
# AltaModa Technologies, LLC, its personnel, associates or contibutors be liable for 
# ANY damages resulting from the use of this software.
#####

Good Explanation of Publishing Metadata for WCF

The blog post, Quick WCF Metadata Publication Walkthrough, is not new, but it give a good explanation of how metadata publication works with WCF.  In particular, it provides good understanding of interaction / dependencies of <baseAddresses />, <endpoint /> and <behavior />.  If you use it’s guidance and tinker around a bit, you’ll also get a good grasp on how you can do IMetadataExchange though HTTP, named pipes or TCP.