tesicg
tesicg

Reputation: 4053

Dynamically invoking WCF service

I've created ASP.NET application and added simple WCF service to it. The ASP.NET application is host for WCF service. The service is running.

The service looks as follows:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string DoWork(string text);
}

public class Service1 : IService1
{
    public string DoWork(string text)
    {
        return text.ToUpper();
    }
}

On the client side is console application that should invoke WCF service dynamically. I use following code:

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

EndpointAddress address = new EndpointAddress("http://localhost:3929/Service1.svc");
IRequestChannel irc = factory.CreateChannel(address);
using (irc as IDisposable)
{
    irc.Open();

    XmlReader reader = XmlReader.Create(new StringReader(
        @"<DoWork xmlns='http://tempuri.org/'>
        <composite xmlns:a='http://www.w3.org/2005/08/addressing' 
        xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
        <a:StringValue>aaaa</a:StringValue>
        </composite>
        </DoWork>"));

    Message m = Message.CreateMessage(MessageVersion.Soap12,  
                "http://tempuri.org/IService1/DoWork", reader);

    Message ret = irc.Request(m);
    reader.Close();

    Console.WriteLine(ret);
}

//close the factory
factory.Close();

But, it crashes at this line:

Message ret = irc.Request(m);

with following error:

The message version of the outgoing message (Soap12 (http://www.w3.org/2003/05/soap-envelope) AddressingNone (http://schemas.microsoft.com/ws/2005/05/addressing/none)) does not match that of the encoder (Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)). Make sure the binding is configured with the same version as the message.

Does anybody know what I'm doing wrong?

Thank you in advance.

Upvotes: 2

Views: 1455

Answers (1)

Chris Dickson
Chris Dickson

Reputation: 12135

Message.CreateMessage(MessageVersion.Soap12,  

Instead of the MessageVersion enum value Soap12, you need to specify Soap12Addressing10 to match your binding.

Upvotes: 1

Related Questions