locked
Another DataMemberConstructor problem RRS feed

  • Question

  • I am trying to use [DataMemberConstructor] because it makes the code a lot cleaner. For example:

    _mainPort.MotionCommand(50, 20);

     

    However, there is a problem if you try to do this from a WinForm in the same project as the service itself. It is common practice to pass messages back to the service from a Form via the service's main port.

     

    The automatically generated constructors only appear in the Proxy DLL. If you add a reference to the Proxy DLL into the project and put a using statement in your WinForm code so that your WinForm can use the correct version of the Port handle, then you get an error during the build saying that the Proxy DLL cannot be copied to the destination because it is in use.

     

    In a strict sense, the WinForm is a consumer of the service and therefore it should use the Proxy interface, not the direct interface defined in the ServiceTypes.cs. However, this appears to be impossible.

     

    I have tried adding the constructors myself manually, but that does not seem to work as far as giving you the simplified syntax noted above. See the code below:

     

    Code Block

    public class MotionCommand : Update<MotionRequest, PortSet<DefaultUpdateResponseType, Fault>>

    {

      public MotionCommand()

        : base(new MotionRequest())

      {

      }

      public MotionCommand(int left, int right)

        : base(new MotionRequest(left, right))

      {

      }

    }

     

    [DataContract]

    [DataMemberConstructor]

    public class MotionRequest

    {

      private int _leftPower;

      private int _rightPower;

      [DataMember, DataMemberConstructor(Order=1)]

      [Description("Left Motor Power")]

      public int LeftPower

      {

        get { return this._leftPower; }

        set { this._leftPower = value; }

      }

      [DataMember, DataMemberConstructor(Order=2)]

      [Description("Right Motor Power")]

      public int RightPower

      {

        get { return this._rightPower; }

        set { this._rightPower = value; }

      }

      public MotionRequest()

      {

      }

      public MotionRequest(int left, int right)

      {

        this._leftPower = left;

        this._rightPower = right;

      }

    }

     

     

     

    Can anyone clarify this for me? Am I doing something wrong? Do I have the wrong idea about [DataMemberConstructor]?

     

    Trevor

     

    P.S. The additional constructors are being created. That is not the problem.

    Saturday, December 29, 2007 5:58 AM

Answers

  • Trevor, you are right you cant use the proxy since you have a chicken and egg compilation problem. However you *can* add the helper methods on your  operations port! You dont need to add them on the operation itself.

     

    thanx

    g

     

    Wednesday, January 2, 2008 5:12 AM

All replies

  • Trevor, you are right you cant use the proxy since you have a chicken and egg compilation problem. However you *can* add the helper methods on your  operations port! You dont need to add them on the operation itself.

     

    thanx

    g

     

    Wednesday, January 2, 2008 5:12 AM
  • Thanks for clarifying that. I was beginning to get really confused.

     

    I see what you mean about the operations port. I can change the constructor to:

     

    Code Block

    public class MotionCommand : Update<MotionRequest, PortSet<DefaultUpdateResponseType, Fault>>

    {

        public MotionCommand()

            : base(new MotionRequest())

        {

        }

        public MotionCommand(MotionRequest body)

            : base()

        {

            this.Body = body;

        }

        public MotionCommand(int left, int right)

            : base()

        {

            this.Body.LeftPower = left;

            this.Body.RightPower = right;

        }

    }

     

     

    I think I will just add the constructors where I use an operation a lot. Otherwise it is a lot of overhead to go through and create them just to simplify the syntax.

     

    Trevor

    Wednesday, January 2, 2008 8:58 AM
  • I have encountered this situation in situation you mention above, in addition to a few other situations. For this reason, I now strictly use generic contracts and have completely different projects for the service implementations.

     

    Compile the contract first, and then you can use the definitions in the WinForm, right?

     

    -Don

    Monday, January 7, 2008 3:11 PM
  • Thanks for the suggestion Don. I see how this could work. However, I am not sure that I want to create a generic contract for every service I write that uses a WinForm :-(  After all, it's just some syntactic sugar. Still, it would be nice if you could used the proxy versions within the original project.

     

    Trevor

     

     

    Tuesday, January 8, 2008 12:44 AM