Create a WCF object on client side

Posted on 2009-12-25
Last Modified: 2012-08-13
Could someone please let me know what is needed in order to be able to create objects which were defined on the server side of WCF, in the client code.

So, basically, if I have an object "Reservation", in WCF (let's say it is hosted on a website's App_Code folder), I would want the calling code (let's say an ASPX page which is NOT in the same website) to be able to do:

Dim reservation as new RestaurantService.Reservation() ' This object is defined on the server, but I would like it to be available to me for declaration on the client side, just as methods of the service contract are.

reservation.FirstName = "John"
reservation.LastName = "Smith"

This would be so much nicer than having to do:

RestaurantService.CreateNewReservation("john", "smith",... )

Question by:luminas
    LVL 11

    Expert Comment

    actually, the method RestaurantService.CreateNewReservation("john", "smith",... ) follows SOA guidelines and is a much better design choice in my ownest opinion.

    Author Comment

    I don't like it. It means duplication of method signatures since inside RestaurantService.CreateNewReservation() there will need to be another reference to a business object's respective method, for example BusinessObjects.Reservation.Create("john", "smith"...), instead of taking that Reservation object in the server side and writing Reservation.Create().

    It's much more maintainable. Not to mention, you have the ability on the client side to "play" with the Reservation object (receive it from server, manipulate, and send back to server).
    LVL 11

    Accepted Solution

    WCF is designed for SOA so I am not even sure that you can do what you are asking for. And again, I really recommend you to use a service based approach and not an OO one. SOA is much easier to maintain actually and allows for a separation of logic between server and client. You'll probably need to rethink your project a bit but I think it is worth it. I am sure a lot of experts here will tell you the same thing, SOA is the best solution for vertical and horizontal scalability.

    Also, what you can do is the following for instance:
    RestaurantService.AddReservation(ReservationInfo reservation)
    where reservation info is a container object with the information required to make a reservation.

    To mark an object to be exposed use "DataContract" and "DataMember" in your attributes:
        public class ReservationInfo
            private int reservationId;
            private string firstName;

            public int FirstName
    LVL 11

    Expert Comment

    by the way, if you don't put "DataMember" on a property than it won't be exposed. In my above example it helps you to have private property (reservationId) that your client side needs to use without exposing them to the client.

    Author Comment

    Actually that's what I was doing - putting "DataContract" attribute on the class - but forgot to update the web service reference in VS... After doing that, I got my object on the client side, and could do exactly what I wanted.

    Even if this somehow conflicts SOA guidelines (it's still hard for me to understand why, this is not about a specific architecture - it's general programming concepts) I still prefer this. It means if many clients are consuming the service, any change in the reservation structure would not break the service for them. And the method signature stays the same.

    Author Closing Comment

    I prefer exposing the object and working with it on client side than just exposing methods with many parameters.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now