问题描述
我定义了一个简单的服务及其实现.
I have a simple service defined and its implementation.
[ServiceContract(Namespace = "http://adventure-works.com/2006/09/30")]
public interface ISimpleProductsService
{
[OperationContract(Action = "http://adventure-works.com/2006/09/30/TestReturnVaue")]
string TestReturnVaue();
}
public class ProductsServiceImpl : ISimpleProductsService
{
public string TestReturnVaue()
{
return "Hello World";
}
}
我已经在Windows窗体中托管了该服务.现在,我想使用此服务而不创建代理.我使用下面的代码来使用该服务.
I have hosted the service in my windows forms. Now i want to consume this service without creating the proxy. I use the below code to consume the service.
class WCFTest
{
public void Test()
{
BasicHttpBinding httpBinding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress(
"http://localhost:8040/SimpleProductsService/SimpleProductsService.svc");
ChannelFactory<IRequestChannel> f1 = new ChannelFactory<IRequestChannel>(httpBinding, address);
f1.Open();
IRequestChannel channel = f1.CreateChannel();
channel.Open();
StringBuilder inputXml = new StringBuilder();
inputXml.Append("<TestReturnVaue xmlns=\"http://adventure-works.com/2006/09/30\" />");
XmlReader readerOut = XmlReader.Create(new MemoryStream(Encoding.Default.GetBytes(inputXml.ToString())));
readerOut.Read();
Message request = Message.CreateMessage(httpBinding.MessageVersion,
"http://adventure-works.com/2006/09/30/TestReturnVaue", readerOut);
Message reply = channel.Request(request);
if (!reply.IsFault)
{
string result = reply.GetBody<string>(); // Here it fails to deserialize.
}
}
}
但是当我尝试获取操作结果时,出现以下错误.请帮助我解决这个问题.
But when i try to get the result of my operation, then i get the below error. Please help me in resolving this.
“第1行位置348中的错误.应从命名空间'http://schemas.microsoft.com/2003/10/Serialization/'中获取元素'string'..遇到'Element' ,名称为"TestReturnVaueResponse",名称空间为"http://adventure-works.com/2006/09/30".
“Error in line 1 position 348. Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'TestReturnVaueResponse', namespace 'http://adventure-works.com/2006/09/30'.”
谢谢
Mallikarjun
Mallikarjun
推荐答案
响应SOAP消息并不完全由您的返回类型确定.这会影响您的部分响应,但实际的消息正文将包裹在带有消息名称的元素中(TestReturnValueResponse,因为它是对TestReturnValue的响应消息)
The response SOAP message is not completely determined by your return type. That influences part of your response but the actual message body is wrapped in an element with the message name (TestReturnValueResponse as it is the response message to TestReturnValue)
使用GetReaderAtBody读取消息的实际数据内容-如果要将其与Linq转换为XML,则请记住先将读取器调用ReadSubtree,然后再将其传递给XElement.Load
Use GetReaderAtBody to read to the actual data content of the message - if you want to combine it with Linq to XML then remember to call ReadSubtree on the reader before passing it to XElement.Load
这篇关于反序列化WCF回复消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!