Send objects using custom attributes through wcf service

I was just starting to learn WCF, because I need it for a school assignment. But I have a problem when I try to send an object with some custom attributes. An object:

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

User attribute:

 [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 create a configuration file and client. The connection between the customer and the service is going well. But when I get an object of type Person and try to find an attribute of type Searchable, I cannot find it.

Is it possible? If so, can you give any clues on how to achieve this behavior?

Thanks. Denis

+7
c # attributes wcf
source share
2 answers

WCF is a message-based service β€” you serialize your message on one side, send it and receive on the other. You do not send objects, as you put it, you send only serialized messages. This is a very important difference!

Your server and client are completely separate - they do not share anything except the description of the service (a list of service methods), and the data is in the form of an XML schema.

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

This means: you get the same fields and properties - but that. Everything else (for example, implemented interfaces, .NET attributes, etc.) is not replicated. They cannot be - after all, WCF interacts - the client can be a PHP application or a Ruby program. How will they handle custom .NET attributes?

So, in short: everything that is specific to .NET and beyond the scope of simply presenting data based on an XML schema cannot be used in the WCF service.

There is a loopback hole - if you control both ends of the connection - both the server and the client - and both are .NET, then you can:

  • put all your service contracts and data in a separate general assembly
  • refers to this assembly in your server-side implementation of your service
  • refer to this assembly in your side proxy

Using this, you will have one assembly with the same type on the server and on the client side - and with this "loophole" you can save .NET features such as attributes between the server and the client.

+15
source share

Today I got into the same problem (after 6 years, I know). Anyway, I ran into a problem when I sent the ExtensionData property to a saved proc on the server side, which I did not want to send. So my problem was really completely server side. All I have to do is declare the attributes of my wcf service, and when I create my parameters, just get the ones that are decorated with the correct attribute, so I do not send this property along with my other parameters / parameters.

0
source share

All Articles