简体   繁体   中英

c# Passing a struct in a WCF method

I want to pass a class to a client over a WCF service. 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. 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. I suggest you try using the additional “/serializer:XmlSerializer /useSerializerForFaults” switches to svcutil and see if that resolves your issue. It should ensure that Tanga gets generated.

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:

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. Do you see any other errors or warnings when you use svcutil?

It's also generally bad practice to use DataSets (both typed and untyped) and IXmlSerializables in public web services. 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

While DataContractSerializer can serialize IXmlSerializable types, there is no guarantee at all made that IXmlSerializable types can be imported as data contracts. 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.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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