简体   繁体   English

c#在WCF方法中传递结构

[英]c# Passing a struct in a WCF method

I want to pass a class to a client over a WCF service. 我想通过WCF服务将类传递给客户端。 In that class I use a struct. 在该课程中,我使用一个结构。 But the value I receive at client side is: "System.Data.DataSet" Must be something I don't understand. 但是我在客户端收到的值是:“ System.Data.DataSet”必须是我不理解的东西。 See my struct (it's just a string for now) 查看我的结构(目前只是一个字符串)

namespace spine.datatypes
{
[Serializable]
public struct Tanga : IXmlSerializable
{
    private string _value;


    public Tanga(string value)
    {
        this._value = value;
    }

    public static implicit operator Tanga(string value)
    {
        return new Tanga(value);
    }

    public override string ToString()
    {
        return this._value;
    }

    // implement IXmlSerializable
    public XmlSchema GetSchema() { return null; }
    public void ReadXml(XmlReader reader)
    {
        _value = reader.ReadContentAsString();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteValue(this._value.ToString());
    }
}

} }

This is my service: 这是我的服务:

namespace webapplication.WCFservice.Recorder
{

    [ServiceContract]
    [XmlSerializerFormat]
    public interface IWCFRecorder
    {
        [OperationContract]
        TvRecorder getDedicatedJob(String recordername, String recorderip);
    }
}

And this is the class I pass: 这是我通过的课程:

namespace spine.recorder.tv
{

[Serializable()]
[XmlRoot("Recorder")]
public class TvRecorder
{
    public int id { get; set; }
    public Tanga name { get; set; }
    public MyIpAddress ip { get; set; }
    public int channel { get; set; }
    public MyTimecode time_start { get; set; }
    public MyTimecode duration { get; set; }

    public TvRecorder() { }

    public TvRecorder(int _id, Tanga _name, MyIpAddress _ip, int _channel, MyTimecode _time_start, MyTimecode _duration)
    {
        this.id = _id;
        this.name = _name;
        this.ip = _ip;
        this.channel = _channel;
        this.time_start = _time_start;
        this.duration = _duration;
    }

}
}

There are unfortunately cases where svcutil generates a DataContract type and an XmlSerializer type for the same schema type. 不幸的是,svcutil会为同一模式类型生成DataContract类型和XmlSerializer类型。 I suggest you try using the additional “/serializer:XmlSerializer /useSerializerForFaults” switches to svcutil and see if that resolves your issue. 我建议您尝试使用附加的“ / serializer:XmlSerializer / useSerializerForFaults”开关切换到svcutil,看看是否可以解决您的问题。 It should ensure that Tanga gets generated. 它应该确保生成Tanga。

In general, for schema import to generate DataContract types, all of the types defined in the schemas must be contained in the subset of XSD that DCS supports, which you can find here: 通常,为了使架构导入生成DataContract类型,架构中定义的所有类型必须包含在DCS支持的XSD子集中,您可以在这里找到:

http://msdn.microsoft.com/en-us/library/ms733112.aspx http://msdn.microsoft.com/en-us/library/ms733112.aspx

If svcutil is failing to generate a proxy when you specify “/serializer:DataContractSerializer”, then the most likely explanation is that the schema isn't DC-conformant. 如果在指定“ / serializer:DataContractSerializer”时svcutil无法生成代理,则最可能的解释是该架构不符合DC标准。 Do you see any other errors or warnings when you use svcutil? 使用svcutil时,是否还看到其他错误或警告?

It's also generally bad practice to use DataSets (both typed and untyped) and IXmlSerializables in public web services. 在公共Web服务中使用数据集(类型化和非类型化)和IXmlSerializables通常也是一种不好的做法。 In this case, it seems there might be difficulties in importing these. 在这种情况下,似乎很难导入这些文件。 Here's a quick link for some other reasons it can be problematic: http://www.hanselman.com/blog/PermaLink,guid,d88f7539-10d8-4697-8c6e-1badb08bb3f5.aspx 这是一个由于其他原因而可能导致问题的快速链接: http : //www.hanselman.com/blog/PermaLink,guid,d88f7539-10d8-4697-8c6e-1badb08bb3f5.aspx

While DataContractSerializer can serialize IXmlSerializable types, there is no guarantee at all made that IXmlSerializable types can be imported as data contracts. 尽管DataContractSerializer可以序列化IXmlSerializable类型,但完全不能保证IXmlSerializable类型可以作为数据协定导入。 Those are two different concepts. 那是两个不同的概念。 IXmlSerializable types are free to provide their own schemas, so it's possible for them to provide schemas that are not datacontract-compliant and thus cause svcutil to fall back to XmlSerializer type generation. IXmlSerializable类型可以自由提供自己的模式,因此它们有可能提供不符合数据合同的模式,从而导致svcutil退回到XmlSerializer类型生成。

Hope this helps. 希望这可以帮助。

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

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