tags:

views:

267

answers:

3

Hey all,

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

A: 

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.

Wayne Hartman
Thanks for your reply. Like i said, i am new at WCF services, just acting on a recomendation here. The program would only be interacted with from Windows platforms, so is WCF even the way to go with this?
TerrorAustralis
Thanks again, i think i have solved it by adding a project refference to my service so i can use the same classes in by service and my forms app.
TerrorAustralis
The service reference is simply generating a client proxy. This makes it easier for you as the developer so you don't have to manually create the requests and handle the responses.
Wayne Hartman
The data will still be XML Serialized using WCF (if you're using a http protocol), but the benefit is that if you are using WCF client-side then you get receive objects rather than XML. It's much cleaner to code.
Kirk Broadhurst
+3  A: 

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

marc_s
Champion stuff mate, i looked at the data contract stuff but it made little to no sense to me. This is the clearest explination i have seen yet
TerrorAustralis
+1  A: 

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.

Kirk Broadhurst
Thanks a lot for the comment. You and marc_s really helped me out, pitty i cant give two 'Answers' for a single question :)
TerrorAustralis
If you like this answer, you can mark it up! Click the 'up' arrow neat the big zero :-)
Kirk Broadhurst