WCF服务和对象构造函数 [英] WCF Services and Object Constructors

查看:162
本文介绍了WCF服务和对象构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学WCF服务。我有一个返回我创建了名为GPSPosition对象的方法。 GPS位置对象有一个纬度和经度。对象不应该真的存在没有这些特性。所以,我想有构造函数接受纬度和长彩车。所以创建对象时,该值设置,并且可以永远不会失效(未设置)。

I am just learning WCF services. I have a method that returns an object I created called GPSPosition. A GPS Position object has a Latitude and a Longitude. The object should never really exist without these properties.. So, I want to have the constructor accept the Lat and Long floats. So when the object is created, the values are set, and can never be invalid (Unset).

不过,这是我的WCF服务的对象。可以作为WCF服务对象,我调用应用程序使用,有一个构造函数?如果我 VAR位置=新GpsPosition {纬度= 1,LON = 1}; 在我的调用代码,将构造的工作?你可以在WCF类的构造函数?

But, it's an object of my WCF service. Can as WCF service object, that my calling application uses, have a constructor? If I var position = new GpsPosition{ lat=1, lon=1 }; in my calling code, will the constructor work? Can you have constructors in WCF classes?

我还以为他们是不同的语言之间共享 - 因此,如果服务由Java使用的,例如,我不能确定的构造将如何触发。对象的结构共享给调用应用程序,不是吗? ?您可以触发构造.NET代码(getter和setter方法,这种情况下)

I thought they are shared among different languages - so if the service is used by Java, for example, I'm unsure how the constructor will fire. The object structure is shared to the calling app, no? Can you fire .net code in the constructor (And getter and setter methods, for that case)?

和如果构造是不能使用 - 是有办法确保我的目标从来都不是无效的? (必须有-180正负180之间的有效纬度/长值)?

And if the constructor isn't usable - is there a way to ensure my object is never invalid? (Must have valid lat/long values between -180 and +180)?

推荐答案

答案将取决于系列化型号搜索键入 GpsPosition 使用。在WCF中使用的两种最常见的是POCO(普通老式CLR对象)和 [DataContract] 。在前者,对象必须有一个无参数的构造函数,它不利于您的要求(该值需要设置一次)。在后者中,所述对象的构造的不调用 - 代替的类型的一个未初始化的实例被创建,其成员是通过反序列化设置

The answer will depend on which serialization model your type GpsPosition uses. The two most common ones used in WCF are POCO (plain-old CLR object) and [DataContract]. In the former, the object must have a parameter-less constructor, which goes against your requirement (that the values need to be set once). In the latter, the object constructor is not invoked - instead an uninitialized instance of the type is created, and its members are set via deserialization.

所以构造是不验证从线来的对象的替代方法。你需要验证,而不是构造函数的对象是什么,是的反序列化完成时,它调用序列化回调的。 WCF的序列化器将打电话给他们时,反序列化完成后,在那里你可以检查对象是否被正确初始化,否则抛出异常。这博客帖子对序列化的更多细节回调,和下面的代码显示了您的方案一种可能的实现。

So constructors are not an alternative to validate the objects coming from the wire. What you need to validate the object, instead of the constructor, is a serialization callback, which is invoked when the deserialization is complete. The WCF serializer will call them when the deserialization is done, and there you can check whether the object was properly initialized, and throw an exception otherwise. This blog post has more details on serialization callbacks, and the code below shows one possible implementation for your scenario.

[DataContract]
public class GpsPosition
{
    private float _lat;
    private float _lon;
    private bool _latWasSet;
    private bool _lonWasSet;

    public GpsPosition(float lat, float lon)
    {
        _lat = lat;
        _lon = lon;
    }

    [DataMember]
    public float lat
    {
        get { return _lat; }
        private set
        {
            _lat = value;
            _latWasSet = true;
        }
    }

    [DataMember]
    public float lon
    {
        get { return _lon; }
        private set
        {
            _lon = value;
            _lonWasSet = true;
        }
    }

    [OnDeserialized]
    void OnDeserialized(StreamingContext ctx)
    {
        if (!_latWasSet || _!lonWasSet ||
            _lat < -90 || _lat > 90 ||
            _lon < -180 || _lon > 180)
        {
            throw new InvalidOperationException("Required property is missing");
        }
    }
}

这篇关于WCF服务和对象构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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