简体   繁体   English

使用WCF将复杂类型传递给Windows服务

[英]Passing complex type to windows service with WCF

I have a windows service and winform front end app. 我有Windows服务和Winform前端应用程序。 I need to be able to call and pass data round trip to methods in the service. 我需要能够调用数据往返并将其传递到服务中的方法。 I have accomplished this by defining an interface and can pass strings without issue. 我通过定义一个接口来完成此任务,并且可以毫无问题地传递字符串。

[ServiceContract]
public interface IStringReverser
{
    [DataContractFormat]
    [OperationContract]
    string ReverseString(string value);
}

I am now trying to return a complex data type and have altered the code as such: 我现在试图返回一个复杂的数据类型,并更改了代码,例如:

[ServiceContract]
public interface IStringReverser
{
    [DataContractFormat]
    [OperationContract]
    DTO ReverseString(string value);
}

[Serializable]
public class DTO
{
    public int Id { get; set; }
    public string Name { get; set; }
}

My implementation is as such: 我的实现是这样的:

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;
    }
}

I am not particular how the data gets transferred. 我不是很特别如何传输数据。 I am getting an error: 我收到一个错误:

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:ReverseStringResult . 格式化程序尝试反序列化消息时引发异常:尝试反序列化参数http://tempuri.org/:ReverseStringResult时发生错误。 The InnerException message was ''EndElement' 'ReverseStringResult' from namespace ' http://tempuri.org/ ' is not expected. InnerException消息是名称空间“ http://tempuri.org/ ”中的“ EndElement”,“ ReverseStringResult”。 Expecting element '_x003C_Id_x003E_k__BackingField'.'. 期望元素为“ _x003C_Id_x003E_k__BackingField”。 Please see InnerException for more details. 有关更多详细信息,请参见InnerException。

In the winform I am creating the connection with this: 在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();
    }

I don't understand the error message or how to correct it. 我不理解错误消息或如何纠正它。 How do I define how it gets deserialized? 如何定义反序列化的方式? Is the serialization ok? 可以序列化吗?

Use Data Contracts: 使用数据合同:

[DataContract]
public class DTO
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Name { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM