TerrorAustralis
TerrorAustralis

Reputation: 2923

Passing classes to a WCF service

I have a problem but first i want to know if im working on the best solution.

I am making an application that will sit on hundreds of client machines and send data to/retreive data from a server. The server will get that data and store/process it.

I want the client apps to be as lightweight as possible so i want the server to pass/receive the data in the form of classes. For example

Client requests User by userID Server responds with class UserDetails which has user name, id, and personal info.

Another example would be

client requests a project server responds with ProjectDetails class which has project id, name, description, details AND a list of Activities (this is another class, so should be implemented as an ActivitiesCollection)

I am just starting out on this and found a lot of people saying WCF services are the way to go but i have never written one before. Is this true? and if so, how do i pass complex classes to/from the WCF service?

Thanks all :)

~Dave

Upvotes: 3

Views: 8881

Answers (3)

Kirk Broadhurst
Kirk Broadhurst

Reputation: 28738

WCF does this for you. It's just like consuming a web service in pre-WCF .NET - the 'structs' are created for you and you just populate their properties.

  1. Create the business objects in the service
  2. Write a service which publishes those objects as method parameters
  3. Generate a service proxy with which you will consume the service.

The auto-generated service proxy will contain classes for each of the business objects you require. It is that easy.

You mention in a comment that you

solved it by adding a project refference to my service so i can use the same classes in by service and my forms app (sic).

That is a legitimate way to do this, but you don't need to share the objects in that manner. If you generate the service proxy on your client then these same classes will be created. You may even save some weight in your client application, because only the required classes will be created.

Upvotes: 2

marc_s
marc_s

Reputation: 755361

A WCF service consists of contracts - the service contract defines the behavior of the service (e.g. your service methods that can be called), and then there's the data contract which defines what data gets passed around.

Those are simply attributes on .NET classes, that expose them to the WCF runtime.

In your case, you'd probably have something like:

[ServiceContract]
interface MyUserService
{
   [OperationContract]
   UserDetails GetUserDetails(int userId);
}

and then for the data:

[DataContract]
class UserDetails
{
   [DataMember]
   int userId { get; set; }

   [DataMember]
   string userName { get; set; }
}

WCF is very flexible and gives you a lot of control over how and what to send around, and it handles all the serialization and other issues for you - automagically. No need to mess around with angle bracket soup yourself!

Check out the WCF Developer Center for lots of good content, how-to videos, articles and more - should help you get started quickly!

Marc

Upvotes: 11

Wayne Hartman
Wayne Hartman

Reputation: 18497

Why not XML serialize the data? The power in not doing it as 'classes' per se is that you can have a number of different clients that can code against a standard platform agnostic API. That's what a service ultimately is used for, allowing different applications and language to talk to single endpoint.

Upvotes: 0

Related Questions