<p>I have a service contract that defines a method with a parameter of type System.Object (xs:anyType in the WSDL).  I want to be able to pass simple types as well as complex types in this parameter.  Simple types work fine, but when I try to pass a complex type that is defined in my WSDL, I get this error:</p>

<p>Element '<a href="http://tempuri.org/:value" rel="nofollow noreferrer">http://tempuri.org/:value</a>' contains data of the '<a href="http://schemas.datacontract.org/2004/07/MyNamespace:MyClass" rel="nofollow noreferrer">http://schemas.datacontract.org/2004/07/MyNamespace:MyClass</a>' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'MyClass' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.</p>

<p>Adding it as a known type doesn't help because it's already in my WSDL.  How can I pass an object of a complex type via an "xs:anyType" parameter?</p>

<p>More info:</p>

<p>I believe this works when using NetDataContract, but I can't use that because my client is Silverlight.</p>

<p>I have seen references to complex types explicitly extending xs:anyType, but I have no idea how to make WCF generate a WSDL that does that, and I have no idea whether or not it would even help.</p>

<p>Thanks</p>

[{'answer_id': 70080, 'author': 'Steve Morgan', 'author_id': 5806, 'author_profile': 'https://Stackoverflow.com/users/5806', 'pm_score': 2, 'selected': False, 'text': '<p>The NetDataContract works because the NetDataContractSerializer includes type information.</p>\n\n<p>The KnownType attribute instructs the DataContractSerializer how to deserialize the message. Being implementation specific, this is information over-and-above that defined by the public contract and doesn\'t belong in the WSDL.</p>\n\n<p>You\'re never going to be able to pass any-old data type because the deserializer needs to identify the appropriate type and create an instance.</p>\n\n<p>You may be able to derive your known types at runtime rather than having them hard-coded in the DataContract. Take a look <a href="http://electroholic.com/?p=5" rel="nofollow noreferrer">here</a> for a sample.</p>\n'}, {'answer_id': 70158, 'author': 'Ray Lu', 'author_id': 11413, 'author_profile': 'https://Stackoverflow.com/users/11413', 'pm_score': 0, 'selected': False, 'text': '<p>Try use data contract Surrogates to map unsupported object that is dot net specific or not interoperable types. See <a href="http://msdn.microsoft.com/en-us/library/ms733064.aspx" rel="nofollow noreferrer">MSDN</a></p>\n'}, {'answer_id': 73640, 'author': 'dcstraw', 'author_id': 10391, 'author_profile': 'https://Stackoverflow.com/users/10391', 'pm_score': 0, 'selected': False, 'text': "<p><strike>I have tried adding the ServiceKnownType attribute, specifying the type that I am trying to pass, but I still get the same error.</strike> I have also tried adding the KnownType attribute to my data contract (which seemed silly because it was the same type as the data contract).  I would guess that adding them at runtime won't help if adding them at compile time doesn't help.</p>\n\n<p>If I were extending another complex type, it seems to me that I would want to add the KnownType attribute to that base type.  But since my base type is Object, I don't see any way to do this.</p>\n\n<p>As for Surrogates, it seems to me that these are used for wrapping types that don't have a contract defined.  In my case however, I do have the contract defined.</p>\n"}, {'answer_id': 81286, 'author': 'sajidnizami', 'author_id': 9498, 'author_profile': 'https://Stackoverflow.com/users/9498', 'pm_score': 1, 'selected': False, 'text': '<p>I hope this would help. I saw a colleague of mine using this code to send complicated data types and to me this is pretty simple. This was used with basicHttpBinding and it works pretty well with MOSS BDC as well as other applications which use the basic binding. </p>\n\n<ol>\n<li>Create a data contract based on a generic class</li>\n<li><p>Use the data contract when the information needs to be sent</p>\n\n<p>[DataContract(Namespace = "<a href="http://Service.DataContracts" rel="nofollow noreferrer">http://Service.DataContracts</a>", Name = "ServiceDataContractBase")]\npublic class ServiceDataContract\n{</p>\n\n<pre><code>public ServiceDataContract() { }\n\npublic ServiceDataContract(TValueType Value)\n{\n    this.m_objValue = Value;\n}\n\nprivate TValueType m_objValue;\n\n[DataMember(IsRequired = true, Name = "Value", Order = 1)]\npublic TValueType Value\n{\n    get { return m_objValue; }\n    set { m_objValue = value; }\n}\n</code></pre>\n\n<p>}</p></li>\n</ol>\n\n<p>Use this data contract where ever it is needed in the WCF functions that return the complicated data type. For example: </p>\n\n<pre><code>public ServiceDataContract&lt;string[]&gt; GetStrings()\n{\n    string[] temp = new string[10];\n    return new ServiceDataContract&lt;string[]&gt;(temp);\n}\n</code></pre>\n\n<p>Update: ServiceDataContract is generic class is using TValueType. It is not appearing because of something wrong with the rendering of the HTML.</p>\n'}, {'answer_id': 85469, 'author': 'dcstraw', 'author_id': 10391, 'author_profile': 'https://Stackoverflow.com/users/10391', 'pm_score': 0, 'selected': False, 'text': "<p>For now I have worked around this by creating a new data contract type that can wrap either another data contract type or a simple type.  Instead of passing type Object, now I pass this wrapper class.  This works OK, but I'd still like to know if there is a solution to the original issue.</p>\n"}, {'answer_id': 370117, 'author': 'dcstraw', 'author_id': 10391, 'author_profile': 'https://Stackoverflow.com/users/10391', 'pm_score': 2, 'selected': True, 'text': "<p>I have solved this problem by using the ServiceKnownType attribute.  I simply add my complex type as a service known type on my service contract, and the error goes away.  I'm not sure why this didn't work last time I tried it.</p>\n\n<p>It doesn't appear to affect the WSDL in any way, so I suspect that the serialized stream must have some difference that informs the deserializer that the object can be deserialized using my type.</p>\n"}]
['https://Stackoverflow.com/questions/67736', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10391/']