tags:

views:

243

answers:

1

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;
    }
}

The custom attribute is:

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

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

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.

+1  A: 

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!

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.

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?

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.

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

  • put all your service and data contracts into a separate, shared assembly
  • reference that assembly in your service implementation on the server side
  • reference that assembly in your client side proxy

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.

marc_s
Thank you Marc for the quick response.I kind of figured that one out after i already posted the question.I do have control of both the client and the server, however I don't think that creating this reference to a common library will be good in the long run.Any ideas on how i could make the attribute accessible by other means?
Denis Rosca
@Denis Rosca: there are no other means, sorry. If you use the standard way of adding a WCF reference, as I said, you **only** get what is serialized into the XML message (and deserialized back) and that does not include .NET custom attributes - sorry.
marc_s