Skip to content

Instantly share code, notes, and snippets.

@xcud
Created February 22, 2013 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcud/5014504 to your computer and use it in GitHub Desktop.
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 …
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);
}
}
}
@xcud
Copy link
Author

xcud commented Feb 22, 2013

See what I did there? I got in the first heckle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment