Calling a SOAP service via a ChannelFactory without using the WSDL
I am calling a SOAP service via a ChannelFactory without using the WSDL. I will be consuming multiple versions of this service and i'm trying to avoid having multiple versions of the WSDL in my project/config files.
The code is as follows:
[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");
}
Using Fiddler I see the SOAP request and response come back just fine. But there is a namespace issue preventing the response message from deserializing.
Error Message: Error in deserializing body of reply message for operation 'login'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'loginResponse' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'ns1:loginResponse' and namespace 'urn:Foo'
If I update my ServiceContract to include the namespace like so:
[ServiceContract(Namespace="urn:Foo")]
public interface IServiceContract
{
[OperationContract(Name = "login")]
string login(string username);
}
The error message goes away but the method now returns null instead of the actual value. Once again I can see the XML in Fiddler and the value I am looking for is in the response but it appears it still can't find the element ns1:loginResponse.
开发者_运维技巧The question is how do I configure the ChannelFactory to know that all elements with a given Namespace will be prefixed with ns1:?
I don't think there is a pure configuration option to achieve what you want... but there is an option - you can use IClientMessageInspector
:
this offers you to change outgoing and incoming messages so you can for example change/check whatever needed...
http://msdn.microsoft.com/en-us/library/aa717047.aspx
http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iclientmessageinspector.aspx
http://social.technet.microsoft.com/wiki/contents/articles/how-to-inspect-wcf-message-headers-using-iclientmessageinspector.aspx
http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx
http://www.codeproject.com/KB/WCF/ExtendingWCF_PartI.aspx
精彩评论