1

I have this Dto model class:

[System.Runtime.Serialization.DataContract, System.Serializable]
public class DataObj
{
    [System.Runtime.Serialization.DataMember]
    public object Value { get; set; }

    public static DataObj CreateDataObj(object obj)
    {
        return new DataObj { Value = obj };
    }
}

static void main()
{
      var obj1 = DataObj.CreateDataObj(10);
      wcfService.Send(obj1);
      var obj2 = DataObj.CreateDataObj("hello");
      wcfService.Send(obj2);
      var obj3 = DataObj.CreateDataObj(new int[]{10, 20,30});
      wcfService.Send(obj3);
      var obj4 = DataObj.CreateDataObj(new string[]{"ht", "fd","xs"});
      wcfService.Send(obj4);
 }

In the main method, WCF calls for obj 1 and 2 are ok but for obj 3 and 4 aren't ok and throw serialization exception.

How can I transfer obj 3 and 4 in service calls?

4
  • 2
    BTW, creating a contract like that that allows any old object to be passed is kind of an anti-pattern. You never know what is allowed to be sent and the service can never predict what it will receive. The net result is that you don't know what operations the service can perform Commented Sep 14, 2019 at 7:20
  • oh, this code implemented and used in application now, it is not possible to re implement, is there any idea to solve this problem?@MickyD Commented Sep 14, 2019 at 7:32
  • 1
    [Serializable] and [DataMember] don't belong together. Two different serializers. Commented Sep 14, 2019 at 7:32
  • 2
    ”used in application now, it is not possible to re implement” - considering you are getting serialisation exceptions it’s always time to ”re-implement” Commented Sep 14, 2019 at 21:24

1 Answer 1

1

In WCF communication, we must explicitly specify the type of serialization. This ensures that both the client and server recognize the custom type during communication. Marking data members as objects violates WCF design pattern. Based on your scenario, the Datatable type can be used, but this is deprecated.
https://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-return-a-datatable-from-wcf-service/
It is recommended that you specify the type of data that needs to be serialized when designing a data contract. For example, we can consider using List[T].
Feel free to let me know if there is anything I can help with.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.