none
Return a custom class from WCF RRS feed

  • Question

  • Hi,

    I have done some programming during the years, but no object programming and not used WCF.

    So my first tests was (unfortunately) to combine these. How to get this to work?
    The gpheader gets null at when I create the popup message.

    My IMixTest file:

        [ServiceContract]
        public interface IMixTest
        {
            [OperationContract]
            GamePiece gp();
        }    

    My MixTest file:

        public class MixTest : IMixTest
        {
            public GamePiece gp()
            {
                GamePiece gp = new GamePiece();
                gp.gpHeader = "codedheader";
                return gp;
            }
        }
        public class GamePiece
        {
            public string gpHeader = "staticheader";
        }

    My client:

            public pGamePiece()
            {
                this.InitializeComponent();
                GetGamePiece();
            }
            private async void GetGamePiece()
            {
                m.GamePiece gp = new m.GamePiece();
                Windows.UI.Popups.MessageDialog mg = new Windows.UI.Popups.MessageDialog(gp.gpHeader);
                await mg.ShowAsync();
            }
    Please help /Magnus

    Magnus Burk

    Friday, June 7, 2013 12:57 PM

Answers

  • Did you apply the DataContract attribute to your GamePiece class and the DataMember attribute to its gpHeader property?

    [DataContract]
    public GamePiece
    {
    [DataMember]
    public string gpHeader {get;set;}
    }

    The DataMemberAttribute is applied to a member of a type to specify that the member is part of a data contract and is serializable by the DataContractSerializer.

    Monday, June 10, 2013 8:24 AM
  • On 6/9/2013 5:35 PM, Magnus Burk wrote:

    No ideas from anyone? /Magnus

    Well, the first thing is the custom object must look like below in order to pass the custom object serialized object  through WCF. If it is not serialized, WCF wants serializable objects, then object is going to be a null value.

    using System;
    using System.Runtime.Serialization;

    namespace BLL.DTO
    {
        [Serializable]
        public class DTOArticle
        {
            [DataMember]
            public int ArticleID { get; set; }
             [DataMember]
            public string Title { get; set; }
             [DataMember]
            public string Body { get; set; }
             [DataMember]
            public int? AuthorID { get; set; }
             [DataMember]
            public Boolean IsUpdate { get; set; }
        }
    }
     The second this is the call form the client to the service must look the the try/catch example in the link for a type-client, which is what you are doing - a call to a typed-client.

    http://msdn.microsoft.com/en-us/library/aa355056(v=VS.85).aspx

    Monday, June 10, 2013 9:57 AM
  • Your interface must have the ServiceContractAttribute and the methods must have the OperationContractsAttribute:

    [ServiceContract]
        public interface IMixTest
        {
            [OperationContract]
            GamePiece gp();
        }

    The returned type should have the DataContractAttribute and the properties should be decorated with the DataMemberAttribute:

    [DataContract]
    public GamePiece
    {
    public GamePiece()
    {
    this.gpHeader = "staticheader";
    }
    [DataMember]
    public string gpHeader {get;set;}
    }


    Then, in your client application that is consuming the service, add a service reference to the service to create a proxy class to be able to access it or create a proxy class yourself by using the svcutil.exe tool: http://msdn.microsoft.com/en-us/library/ms733133.aspx

    Monday, June 10, 2013 10:57 AM
  • Hi,

    Thanks for your help. After browsing in the client docs you provided, I finally found out it was about how the client called the service. Now it works:-)

    BR /Magnus


    Magnus Burk

    Wednesday, June 12, 2013 2:08 PM

All replies

  • What is the variable "m'?  Is it a namespace or a class?  if it is a class you need to construct the class calling the class with new() method.


    jdweng

    Friday, June 7, 2013 4:29 PM
  • Hi,

    M is

    using m = Mix.myclientgp;

    And this is the name of the service reference. Mix.myclientgp is a namespace what I can see.

    BR /Magnus


    Magnus Burk

    Friday, June 7, 2013 7:38 PM
  • No ideas from anyone? /Magnus

    Magnus Burk

    Sunday, June 9, 2013 9:35 PM
  • Did you apply the DataContract attribute to your GamePiece class and the DataMember attribute to its gpHeader property?

    [DataContract]
    public GamePiece
    {
    [DataMember]
    public string gpHeader {get;set;}
    }

    The DataMemberAttribute is applied to a member of a type to specify that the member is part of a data contract and is serializable by the DataContractSerializer.

    Monday, June 10, 2013 8:24 AM
  • On 6/9/2013 5:35 PM, Magnus Burk wrote:

    No ideas from anyone? /Magnus

    Well, the first thing is the custom object must look like below in order to pass the custom object serialized object  through WCF. If it is not serialized, WCF wants serializable objects, then object is going to be a null value.

    using System;
    using System.Runtime.Serialization;

    namespace BLL.DTO
    {
        [Serializable]
        public class DTOArticle
        {
            [DataMember]
            public int ArticleID { get; set; }
             [DataMember]
            public string Title { get; set; }
             [DataMember]
            public string Body { get; set; }
             [DataMember]
            public int? AuthorID { get; set; }
             [DataMember]
            public Boolean IsUpdate { get; set; }
        }
    }
     The second this is the call form the client to the service must look the the try/catch example in the link for a type-client, which is what you are doing - a call to a typed-client.

    http://msdn.microsoft.com/en-us/library/aa355056(v=VS.85).aspx

    Monday, June 10, 2013 9:57 AM
  • Hi,

    Thank you both. I'm closer to it now. Seems that [datacontract] do the same as serializable. I ended up with having the class built in the I.. file, under a [datacontract] tag. But this Example says I have to have a [operationcontract] tag in the interface to. How to write the getsomething part in the example?

    BR /Magnus


    Magnus Burk

    Monday, June 10, 2013 10:39 AM
  • Your interface must have the ServiceContractAttribute and the methods must have the OperationContractsAttribute:

    [ServiceContract]
        public interface IMixTest
        {
            [OperationContract]
            GamePiece gp();
        }

    The returned type should have the DataContractAttribute and the properties should be decorated with the DataMemberAttribute:

    [DataContract]
    public GamePiece
    {
    public GamePiece()
    {
    this.gpHeader = "staticheader";
    }
    [DataMember]
    public string gpHeader {get;set;}
    }


    Then, in your client application that is consuming the service, add a service reference to the service to create a proxy class to be able to access it or create a proxy class yourself by using the svcutil.exe tool: http://msdn.microsoft.com/en-us/library/ms733133.aspx

    Monday, June 10, 2013 10:57 AM
  • Hi,

    Thanks for your help. After browsing in the client docs you provided, I finally found out it was about how the client called the service. Now it works:-)

    BR /Magnus


    Magnus Burk

    Wednesday, June 12, 2013 2:08 PM