WCF 忽略数据协定不起作用

本文关键字:不起作用 数据 WCF | 更新日期: 2023-09-27 18:31:57

我有一个托管在 IIS 上的 WCF 服务。这是我的界面:

[ServiceContract]
[SilverlightFaultBehavior]
public interface IETC
{
   [OperationContract]
   [PrincipalPermission(SecurityAction.Demand, Role = "XYZ")]
   string GetStampXML();
   [OperationContract]
   [PrincipalPermission(SecurityAction.Demand, Role = "XYZ")]
   List<Stamp> GetStamps();
}

当我通过 Web 浏览器转到我的 WCF 服务时出现错误。错误如下:

Type 'System.Windows.Media.ImageSource' cannot be serialized. Consider marking it with the DataContractAttribute attribute.....

我的邮票类是:

[DataContract]
public class Stamp
{
    private string _Name;
    private string _SmallIcon = "";
    private string _MediumIcon = "";
    private string _LargeIcon = "";
    private BitmapImage _SmallImage;
    private BitmapImage _MediumImage;
    private BitmapImage _LargeImage;
    [DataMember]
    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }
    [DataMember]
    public string SmallIcon
    {
        get { return _SmallIcon; }
        set { _SmallIcon = value; }
    }
    [DataMember]
    public string MediumIcon
    {
        get { return _MediumIcon; }
        set { _MediumIcon = value; }
    }
    [DataMember]
    public string LargeIcon
    {
        get { return _LargeIcon; }
        set { _LargeIcon = value; }
    }
    [IgnoreDataMember]
    public BitmapImage SmallImage
    {
        get { return _SmallImage; }
        set { _SmallImage = value; }
    }
    [IgnoreDataMember]
    public BitmapImage MediumImage
    {
        get { return _MediumImage; }
        set { _MediumImage = value; }
    }
    [IgnoreDataMember]
    public BitmapImage LargeImage
    {
        get { return _LargeImage; }
        set { _LargeImage = value; }
    }
}

就像忽略数据成员没有被识别一样。我在没有忽略数据成员图的情况下尝试了它,它只会序列化数据成员,这也不起作用。任何想法为什么它似乎试图序列化位图图像?

WCF 忽略数据协定不起作用

您运行的是哪个版本的 .net? .NET 4 数据协定不要求您显式设置 Ignore 属性。可以通过使用 DataContractSerializer 并将内容写入文件来测试生成的内容。创建控制台应用程序并引用服务项目。

namespace SO_10281928
{
    class Program
    {
        static void Main(string[] args)
        {
            var instance = new Stamp
                               {
                                   Name = "Test",
                                   SmallIcon = "Small Icon",
                                   LargeIcon = "LargeIcon",
                                   MediumIcon = "MediumIcon"
                               };
            using (var stream = new FileStream(@"c:'temp'stamp.xml", FileMode.Create))
            {
                var ds = new DataContractSerializer(typeof (Stamp));
                ds.WriteObject(stream, instance);
            }
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
    [DataContract]
    public class Stamp
    {
        private string _Name;
        private string _SmallIcon = "";
        private string _MediumIcon = "";
        private string _LargeIcon = "";
        private BitmapImage _SmallImage;
        private BitmapImage _MediumImage;
        private BitmapImage _LargeImage;
        [DataMember]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        [DataMember]
        public string SmallIcon
        {
            get { return _SmallIcon; }
            set { _SmallIcon = value; }
        }
        [DataMember]
        public string MediumIcon
        {
            get { return _MediumIcon; }
            set { _MediumIcon = value; }
        }
        [DataMember]
        public string LargeIcon
        {
            get { return _LargeIcon; }
            set { _LargeIcon = value; }
        }
        public BitmapImage SmallImage
        {
            get { return _SmallImage; }
            set { _SmallImage = value; }
        }
        public BitmapImage MediumImage
        {
            get { return _MediumImage; }
            set { _MediumImage = value; }
        }
        public BitmapImage LargeImage
        {
            get { return _LargeImage; }
            set { _LargeImage = value; }
        }
    }
    public class BitmapImage
    {
    }
}

结果是:

<Stamp xmlns="http://schemas.datacontract.org/2004/07/SO_10281928" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <LargeIcon>LargeIcon</LargeIcon>
    <MediumIcon>MediumIcon</MediumIcon>
    <Name>Test</Name>
    <SmallIcon>Small Icon</SmallIcon>
</Stamp>