Why am I getting this WCF Error Message?

后端 未结 4 1248
长发绾君心
长发绾君心 2021-01-12 04:54

I am getting the error below when I call my WCF service. What am I missing here?

\'System.String[]\' with data contract name
\'ArrayOfstring:http://schemas.         


        
相关标签:
4条回答
  • 2021-01-12 05:13

    In my case, after adding [Serializable] attribute to the MyEntity class. And then the issue came with serialization of the roles string array.

        [Serializable]
        [KnownType(typeof(string[]))]
        public class MyEntity
        {
           .........
           public string roles[]
           ......... 
        }
    

    [KnownType(typeof(string[]))] worked like magic!

    0 讨论(0)
  • 2021-01-12 05:24

    You haven't posted the code, so my answer is based on the assumption that you have a class myEntity which you are trying to serialize. Try using a KnownTypeAttribute for the class

    e.g.

    [KnownType(typeof(myEntity))]
    

    You can refer to the following MSDN link: KnownTypeAttribute

    0 讨论(0)
  • 2021-01-12 05:29

    From what I gather, you have a WCF function that has a parameter named 'myEntity'. I'm assuming that the type of myEntity is a user-defined class and is adorned with the DataContract attribute, as it should be. I'm also assuming that the type of myEntity has a member field that is a string array. Let's assume that all of this is true (again, it'd be really helpful if you could post your code).

    Ordinarily, string arrays, i.e., string[], will serialize just fine. But, in some cases (see here and here), you may have to add it to the list of known types in order for WCF to serialize everything correctly.

    To do this, add the following:

    [DataContract]
    [KnownType(typeof(string[]))]
    public class YourClassNameHere
    {
    }
    
    0 讨论(0)
  • 2021-01-12 05:31

    Yes. As explained in the previous post, The issue occurs if you pass an array of a Type(which is defined as a DataContract]). you will need to define the array of this class as a seperate type and mark it as data contract.

    Wont Work`

    [DataContract]
    Public class ABC{
    }
    
    ...
    
    SendData(ABC[])
    

    `

    What will work:

    Public class Data{ public ABC[] prop{get;set;}}
    ...
    SendData(Data);
    
    0 讨论(0)
提交回复
热议问题