Created
February 22, 2013 16:10
-
-
Save xcud/5014504 to your computer and use it in GitHub Desktop.
I'm spoiled by PowerShell's New-WebServiceProxy. I want to poke around at a WCF but I don't want to precompile a WSDL, or add a service reference, or include a contract interface that'll break on compile when it changes. Let me just interact with the web services reflectively like I do in PowerShell. The performance profile is terrible. It does …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Management.Automation; | |
using System.Management.Automation.Runspaces; | |
using System.Reflection; | |
namespace Xcud.Net | |
{ | |
/// <example> | |
/// DynamicWebServiceProxy proxy = new DynamicWebServiceProxy(uri); | |
/// string[] cachePaths = proxy.InvokeMethod("GetCachePaths", null) as string[]; | |
/// </example> | |
public class DynamicWebServiceProxy | |
{ | |
object Proxy = null; | |
public DynamicWebServiceProxy(string uri) | |
{ | |
Pipeline pipeline = PowerShell.Create().Runspace.CreatePipeline(); | |
Command command = new Command("New-WebServiceProxy"); | |
command.Parameters.Add(new CommandParameter("Uri", uri)); | |
pipeline.Commands.Add(command); | |
PSObject proxyResult = pipeline.Invoke().FirstOrDefault(); | |
this.Proxy = proxyResult.BaseObject; | |
} | |
public object InvokeMethod(string methodName, object[] parameters) | |
{ | |
MethodInfo method = this.Proxy.GetType().GetMethod(methodName); | |
if (method == null) | |
return null; | |
return method.Invoke(this.Proxy, parameters); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See what I did there? I got in the first heckle.