Share on Facebook
Welcome to MSDN Blogs Sign in | Join | Help

Dynamically Invoking Web Services... With WCF This Time

Awhile back, I posted on Dynamically Invoking Web Services using ASMX.  To do this, we used a little nasty bit of CodeDOM trickery and reflection, something that just isn't needed with WCF.  Take a look at how much easier this is using WCF.

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
factory.Open();

EndpointAddress address = 
    new EndpointAddress("http://localhost:43288/WCFService1/Service.svc");
IRequestChannel irc = factory.CreateChannel(address);
using (irc as IDisposable)
{
    irc.Open();
    
    XmlReader reader = XmlReader.Create(new StringReader(
@"<GetDataUsingDataContract xmlns='http://tempuri.org/'>
<composite xmlns:a='http://schemas.datacontract.org/2004/07/' 
xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<a:BoolValue>true</a:BoolValue>
<a:StringValue>successfultest</a:StringValue>
</composite>
</GetDataUsingDataContract>"));
    Message m = Message.CreateMessage(MessageVersion.Soap11,
        "http://tempuri.org/IService/GetDataUsingDataContract", reader);
    
    Message ret = irc.Request(m);
    reader.Close();
    
    Console.WriteLine(ret);    
}

//close the factory
factory.Close();

To test this, I simply used Visual Studio 2008 to create a new WCF Service.  I changed the binding from wsHttpBinding to basicHttpBinding, and the rest stays the same.

One of the neat things about the original post was that you could download the definition and compile it.  You could still do the same operations here and use that to serialize into an object used in the CreateMessage method.

Published Tuesday, January 20, 2009 4:11 PM by kaevans
Filed under: ,

Comments

# infoblog &raquo; Dynamically Invoking Web Services&#8230; With WCF This Time

# re: Dynamically Invoking Web Services... With WCF This Time

Wednesday, January 21, 2009 12:19 AM by Daniel

Is there an advantage to dynamically constructing the service operation over binding with service references directly?

# re: Dynamically Invoking Web Services... With WCF This Time

Wednesday, January 21, 2009 8:41 AM by kaevans

In my opinion, not for the majority of cases.  However, there have been several customers who came up with cases where they wanted their own "Add Service Reference" type of functionality at runtime.

I have gotten quite a few emails on the previous post, thought it was time to write up the WCF version as well.

When I work with a lot of folks on services, I find that the Managed Services Engine (http://www.codeplex.com/servicesengine) actually fits their problems more cleanly, providing virtual services instead of pushing the brunt of the work into the client.  

# Dynamically Invoking Web Services... With WCF This Time

Wednesday, January 21, 2009 6:24 PM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# Links of the week #69

Sunday, January 25, 2009 6:50 PM by Zunanji viri

Development Merlin Wizard Framework - A framework for easily making wizards in .NET. LINQ to SQL templates

New Comments to this post are disabled
 
Page view tracker