通过WCF服务发送带有自定义属性的对象 [英] Send objects with custom attributes via a wcf service

查看:142
本文介绍了通过WCF服务发送带有自定义属性的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习WCF,因为我需要它的一所学校分配。但我有,当我试图发送一个对象,一些自定义属性的问题。
对象是:

I just began studying WCF because i need it for a school assignment. But i have a problem when i am trying to send an object with some custom attributes. The object is:

[DataContract]
public class Person
{
    [DataMember]
    [Searchable("ID")]
    public virtual String ID
    {
        get;
        set;
    }

    [DataMember]
    [Searchable("LastName")]
    public virtual String LastName
    {
        get;
        set;
    }

    [DataMember]
    [Searchable("FirstName")]
    public virtual String FirstName
    {
        get;
        set;
    }
}

自定义属性是:

[DataContract]
[AttributeUsage(AttributeTargets.Property)]
public class Searchable:Attribute
{
    public Searchable(String PropertyName)
    {
        this.PropertyName = PropertyName;
    }

    [DataMember]
    public virtual String PropertyName
    {
        get;
        set;
    }
}

我使用SvcUtil工具生成配置文件和客户端。客户端和服务之间的通信正在进行细。但是,当我收到类型的对象的的并尝试搜索类型的属性的检索的我无法找到任何。

I use svcutil to generate the configuration file and the client. The communication between the client and the service is going on fine. But when I receive an object of type Person and try to search for attribute of type Searchable i can't find any.

这可能吗?如果是你能提供关于如何实现这种行为的任何提示?

Is this possible? If yes could you provide any hints on how to achieve this kind of behavior?

感谢。
丹尼斯。

Thanks. Denis.

推荐答案

WCF是一种基于消息的服务 - 你序列化的一面你的消息,整个发送,接收它的另一面。你不是的发送对象的如你所说的那样 - 你只发送序列化的消息。这是相当的重要的区别!

WCF is a message based service - you serialize your message on one side, send it across, and receive it on the other side. You're not sending objects as you put it - you only send serialized messages. That's quite an important difference!

您的服务器和客户端是完全独立的 - 它们不共享任何东西,但服务描述(服务方法列表),并在XML架构的表单中的数据合约

Your server and client are totally separate - they don't share anything but the service description (the list of service methods), and the data contracts in the form of a XML schema.

当你创建一个客户是服务方式的代理,数据契约的复制(看起来是一样的,但不同的命名空间,通常情况下),你得到什么具有相同的签名的序列化格式。这是所有有。你在客户端上一个新的,独立的类,这将序列化为相同的格式在服务器端你原来的班。

What you get when you create a client is a proxy for the service methods, and a copy of the data contract (looks the same, but different namespace, typically) that has the same signature in the serialized format. That's all there is. You get a new, separate class on the client side, that will serialize into the same format as your original class on the server side.

这意味着:你会得到相同的字段和属性 - 但仅此而已。别的(如被实现的接口,.NET属性和更多)是的不可以复制。它们不能是 - 毕竟,WCF是可互操作 - 客户端可能是一个PHP应用程序或Ruby程序。他们将如何处理.NET自定义属性?

This means: you'll get the same fields and properties - but that's it. Anything else (like interfaces that are implemented, .NET attributes and more) are not replicated. They cannot be - after all, WCF is interoperable - the client could be a PHP app or a Ruby program. How are they going to handle .NET custom attributes?

因此​​,在短暂的:什么是.NET具体和超越了简单的基于XML的模式的数据再presentation不能跨WCF服务使用

So in brief: anything that is .NET specific and goes beyond the simple XML-schema-based data representation cannot be used across a WCF service.

有一个循环孔 - 如果你控制通信的两端 - 服务器和客户端 - 无一不是.NET,那么你可以:

There is a loop hole - if you control both ends of the communication - both server and client - and both are .NET, then you could:


  • 把你所有的服务和数据的合同到一个单独的,共享程序集

  • 引用的装配服务实现服务器端

  • 引用该程序集在您的客户端代理

利用这一点,你就会有一个程序集数据的服务器都和客户端的类型 - 而这个漏洞,您可以preserve .NET等具体像服务器和客户端之间的属性。

Using this, you'll have one assembly with the same data type on both the server and the client side - and with this "loophole", you can preserve such .NET specifics like attributes between server and client.

这篇关于通过WCF服务发送带有自定义属性的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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