none
how to create a hierarchized WCF contract?

    Question

  • Hello,

    I intend to make a device and a computer communicate using WCF.  (-> one to one communication, intranet, no interoperability issues)

    Ideally, I would like to have a multi-layered interface
    That is, a IDevice, that will contain methods and a collection of IMotor, which will contain methods and several IAnalogOutput and IDigitalInput, ... which will contain... and so on and so on.

    From my little experience of WCF, it seems to me that it promotes somewhat "flat" service contract, and such a Russian dolls scheme, if not impossible, may prove a lot of work.

    Am I misunderstanding WCF, or should I use remoting instead?

    Thx
    Thursday, February 25, 2010 12:48 PM

Answers

  • It is possible to build a DataContract that contains other DataContracts. As long as every class you wish to use is marked with the [DataContract] attribute and any properties you wish to include as [DataMember]

    Friday, February 26, 2010 2:09 PM
  • First of all, don't use remoting because it's an obsolete technology. As Allan mentioned, you can have 'multi-layered' data contract as long as you follow some rules. Here's an example. Let's say you have two classes, Body and Hand, and you want to include the Hand class into the Body class - this is what you should do:

    1) Mark both classes with the DataContract attribute
    2) Create a property typeof(Hand) inside the Body class 
    3) Mark the property with the DataMember attribute

    You can use these steps to create multiple layers (e.g. you could attach a class called Fist to the Hand class by using the same three steps as above).

    Hope this helped!

    Regards,
    Stipe Ivan




    Friday, February 26, 2010 2:42 PM
  • Hi Gille,

    From your post I see you are speaking about interfaces like IAnalogInput, IDigitalInput etc. But I'm not sure
    whether you can include Interfaces in DataContracts. We can't able to mark interfaces as DataContracts at least till .NET 3.5 SP1 I don't know about future versions.

    You can create a base class and by the help of KnownType attributes derived types can be passed in communication. But I'm not sure about interfaces. I tried with a sample service contract that passes an interface like below

        [ServiceContract]
        public interface IService1 : IService
        {
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            IDataContract ServiceMethod2();
        }
    I created a proxy using SvcUtil.exe where the IDataContract is converted into an object in the client side. I don't know whether there any swithches in the utility tool that can do this.

    Have a look at this thread 

    Regards
    Dnana




    Saturday, February 27, 2010 6:12 AM

All replies

  • It is possible to build a DataContract that contains other DataContracts. As long as every class you wish to use is marked with the [DataContract] attribute and any properties you wish to include as [DataMember]

    Friday, February 26, 2010 2:09 PM
  • First of all, don't use remoting because it's an obsolete technology. As Allan mentioned, you can have 'multi-layered' data contract as long as you follow some rules. Here's an example. Let's say you have two classes, Body and Hand, and you want to include the Hand class into the Body class - this is what you should do:

    1) Mark both classes with the DataContract attribute
    2) Create a property typeof(Hand) inside the Body class 
    3) Mark the property with the DataMember attribute

    You can use these steps to create multiple layers (e.g. you could attach a class called Fist to the Hand class by using the same three steps as above).

    Hope this helped!

    Regards,
    Stipe Ivan




    Friday, February 26, 2010 2:42 PM
  • Hi Gille,

    From your post I see you are speaking about interfaces like IAnalogInput, IDigitalInput etc. But I'm not sure
    whether you can include Interfaces in DataContracts. We can't able to mark interfaces as DataContracts at least till .NET 3.5 SP1 I don't know about future versions.

    You can create a base class and by the help of KnownType attributes derived types can be passed in communication. But I'm not sure about interfaces. I tried with a sample service contract that passes an interface like below

        [ServiceContract]
        public interface IService1 : IService
        {
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            IDataContract ServiceMethod2();
        }
    I created a proxy using SvcUtil.exe where the IDataContract is converted into an object in the client side. I don't know whether there any swithches in the utility tool that can do this.

    Have a look at this thread 

    Regards
    Dnana




    Saturday, February 27, 2010 6:12 AM