【发布时间】:2014-04-24 02:54:08
【问题描述】:
我有一个 Windows 服务和 winform 前端应用程序。我需要能够调用并将数据往返传递给服务中的方法。我通过定义一个接口实现了这一点,并且可以毫无问题地传递字符串。
[ServiceContract]
public interface IStringReverser
{
[DataContractFormat]
[OperationContract]
string ReverseString(string value);
}
我现在正试图返回一个复杂的数据类型,并已更改代码:
[ServiceContract]
public interface IStringReverser
{
[DataContractFormat]
[OperationContract]
DTO ReverseString(string value);
}
[Serializable]
public class DTO
{
public int Id { get; set; }
public string Name { get; set; }
}
我的实现是这样的:
public class StringReverser : IStringReverser
{
public DTO ReverseString(string value)
{
char[] retVal = value.ToCharArray();
int idx = 0;
for (int i = value.Length - 1; i >= 0; i--)
retVal[idx++] = value[i];
var dto = new DTO();
dto.Name = retVal.ToString();
dto.Id = 122;
return dto;
}
}
我并不特别关注数据是如何传输的。我收到一个错误:
格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数http://tempuri.org/:ReverseStringResult 时出错。不应出现来自命名空间“http://tempuri.org/”的 InnerException 消息是“EndElement”“ReverseStringResult”。期待元素'_x003C_Id_x003E_k__BackingField'。'。有关详细信息,请参阅 InnerException。
在 winform 中,我正在创建与此的连接:
private IStringReverser pipeProxy;
public Form1()
{
InitializeComponent();
ChannelFactory<IStringReverser> httpFactory =
new ChannelFactory<IStringReverser>(
new BasicHttpBinding(),
new EndpointAddress(
"http://localhost:8000/Reverse"));
ChannelFactory<IStringReverser> pipeFactory =
new ChannelFactory<IStringReverser>(
new NetNamedPipeBinding(),
new EndpointAddress(
"net.pipe://localhost/PipeReverse"));
//IStringReverser httpProxy = httpFactory.CreateChannel();
pipeProxy = pipeFactory.CreateChannel();
}
我不明白错误消息或如何更正它。我如何定义它是如何反序列化的?序列化可以吗?
【问题讨论】: