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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With