【问题标题】:Dynamically invoking WCF service动态调用 WCF 服务
【发布时间】:2012-09-20 02:48:13
【问题描述】:

我创建了 ASP.NET 应用程序并向其添加了简单的 WCF 服务。 ASP.NET 应用程序是 WCF 服务的宿主。服务正在运行。

服务如下:

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

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

在客户端是应动态调用 WCF 服务的控制台应用程序。我使用以下代码:

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();

但是,它在这一行崩溃了:

Message ret = irc.Request(m);

出现以下错误:

传出消息 (Soap12 (http://www.w3.org/2003/05/soap-envelope) AddressingNone (http://schemas.microsoft.com/ws/2005/05/addressing/none)) 的消息版本与编码器 (Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)) 的消息版本不匹配。确保绑定配置为与消息相同的版本。

有人知道我做错了什么吗?

提前谢谢你。

【问题讨论】:

    标签: wcf dynamic invoke


    【解决方案1】:
    Message.CreateMessage(MessageVersion.Soap12,  
    

    您需要指定 Soap12Addressing10 而不是 MessageVersion 枚举值 Soap12 以匹配您的绑定。

    【讨论】:

    • 这毫无意义。你确定你修改后重新编译了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多