如何为启用 Silverlight 的 WCF 服务配置仅获取属性 [英] How do you configure a get-only property for a Silverlight-enabled WCF service

查看:35
本文介绍了如何为启用 Silverlight 的 WCF 服务配置仅获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定错误是从哪里产生的(来自 Silverlight、来自 wcf、其他东西......)但是,我有一个从 Silverlight 调用的 WCF 服务.该方法返回一个类,其属性没有设置器.这会引发错误.如果我向属性添加一个 setter,那么它不会给出错误.

I'm not certain where the error is resulting (from silverlight, from wcf, something else...) but, I have a WCF service being called from Silverlight. The method returns a class, with a property that does not have a setter. This throws an error. If I add a setter to the property then it does not give an error.

该错误是通常难以理解且无益的 Silverlight 错误消息,但是...

The error is the usual impenetrable and unhelpful Silverlight error message, but...

[Serializable]  
[DataContract]  
public SomeClass {  
    DataMember]  
    public string PropertyA { get; set; }  
    public string PropertyB { get { return "Hi There"; } }  
}  

抛出错误...

但改为:

[Serializable]  
[DataContract]  
public SomeClass {  
     [DataMember]  
     public string PropertyA { get; set; }  
     public string PropertyB { get { return "Hi There"; } set {} }  
}  

没有错误.

包括通常的 ISomeService.svc &SomeService.svc 类、Silverlight 中更新的引用调用客户端异步等

Includes the usual ISomeService.svc & SomeService.svc class, references updated in Silverlight calling the client async, etc., etc.

配置属性(除DataMember"之外的某些属性以允许仅获取或私有设置属性)通过网络传递它的正确方法是什么?

What is the correct way to configure the property (some attribute other than "DataMember" to allow a get-only, or a private-set property) to pass it over the wire?

推荐答案

在您的示例中,PropertyB 没有用 DataMember 属性标记,这意味着它不会在 WSDL 中公开并被序列化程序忽略.但是如果你用 DataMember 标记 PropertyB,那么你必须有一个 setter(私有的、受保护的或公共的)才能正确地序列化它,否则你可能会得到一个异常.我可以想到通过网络序列化只读属性的两种方法:

In your example PropertyB is not marked with a DataMember attribute meaning that it won't be exposed in the WSDL and ignored by the serializer. But if you mark PropertyB with a DataMember then you must have a setter (private, proptected or public) in order to serialize it correctly otherwise you may get an exception. There are two ways I can think of having read-only properties serialized over the wire:

[DataContract]
public class SomeClass
{ 
    public SomeClass()
    {
        _propertyB = "Hi there";
    }

    [DataMember(Name="PropertyB")]
    private readonly string _propertyB;

    public string PropertyB
    {
        get { return _propertyB; }
    }
}

或者这个:

[DataContract]
public class SomeClass
{ 
    public SomeClass()
    {
        PorpertyB = "Hi there";
    }

    [DataMember]
    public string PropertyB
    {
        get;
        private set;
    }
}

请注意,如果您使用 svcutil.exe,生成的代理类将具有该属性的公共 getter 和 setter,这可能不是您正在寻找的.在这种情况下,您可以在客户端使用服务合同和数据合同程序集.

Please note that if you use svcutil.exe, the generated proxy class will have both public getter and setter for the property which might not be what you are looking for. In this case you can use the service contract and data contract assemblies on the client side.

这篇关于如何为启用 Silverlight 的 WCF 服务配置仅获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆