Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send objects with custom attributes via a wcf service

Tags:

c#

attributes

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

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.

like image 687
Denis Rosca Avatar asked May 13 '10 20:05

Denis Rosca


1 Answers

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.

like image 126
marc_s Avatar answered Sep 23 '22 08:09

marc_s