通过ChannelFactory调用SOAP服务而不使用WSDL

时间:2011-07-20 15:02:54

标签: .net magento soap channelfactory

我在不使用WSDL的情况下通过ChannelFactory调用SOAP服务。我将使用此服务的多个版本,并且我试图避免在我的项目/配置文件中使用多个版本的WSDL。

代码如下:

[ServiceContract()]
public interface IServiceContract
{
    [OperationContract(Name = "login")]
    string login(string username);
}

public void UserLogin()
{
    IServiceContract service = new ChannelFactory<IServiceContract>(
        new BasicHttpBinding(),
        "http://myurl.com/index.php/api/v2_soap/index/")
        .CreateChannel();

    var sessionId = service.login("username");
}

使用Fiddler我看到SOAP请求和响应回来了。但是存在一个名称空间问题,导致响应消息无法反序列化。

错误消息: 对操作'login'反序列化回复消息体时出错。 OperationFormatter遇到无效的Message正文。预计会找到名为“loginResponse”的节点类型“Element”和名称空间“http://tempuri.org/”。找到名为'ns1:loginResponse'的节点类型'Element'和名称空间'urn:Foo'

如果我更新我的ServiceContract以包含如下所示的命名空间:

[ServiceContract(Namespace="urn:Foo")]
public interface IServiceContract
{
    [OperationContract(Name = "login")]
    string login(string username);
}

错误消息消失但该方法现在返回null而不是实际值。再一次,我可以看到Fiddler中的XML,我正在寻找的值是在响应中,但看起来它仍然无法找到元素ns1:loginResponse。

问题是我如何配置ChannelFactory以知道具有给定命名空间的所有元素都将以ns1为前缀:?

相关问题