Reputation: 12034
I'm trying return an object
from my WCF function call, and if I specify the type in my ServiceModel
, the result is sent successfully.
But if I send an object displayme a message like:
This operation is not supported in the wcf test client because it uses type
Code:
[DataContract]
public class ServiceModel
{
[DataMember]
public int status { get; set; }
[DataMember]
public string message { get; set; }
[DataMember]
public requestdVM result { get; set; }
//public object result { get; set; }
}
The problem is similar to: WCF service: Returning custom objects but as solution they change the type
Upvotes: 0
Views: 4686
Reputation: 9119
In common there is no such possibility as using base common objects in WCF services.
Here is the example of WSDL type declaration:
<complexType name="TimePeriod">
<all>
<element name="StartTime" type="xsd:timeInstant"/>
<element name="EndTime" type="xsd:timeInstant"/>
</all>
</complexType>
This schema is visible for the clients and then .NET can understand that it can use his own object and transfer it. In .NET this will be similar to
[DataContract]
public class TimePeriod
{
[DataMember]
public DateTime StartTime {get;set;}
[DataMember]
public DateTime EndTime {get;set;}
}
Java can understand WSDL and use own class and objects; Python can do it, PHP can do it, hope you got the SOA idea.
Now you want to send object that can be anything. It is not possible to do with contracts.
However if you can promise that only .NET is using in your project then there is a solution.
The idea is to serialize .NET object in binary format and give a type name of the object. Then on the client side deserialize it. Of course you cannot simply do it with sending object from .NET to, say, Python.
1) Small object sending in string XML format
[Serializable]
public class Data
{
public string TypeName {get;set;} // typeof(string), typeof(int), etc.
public string Xml {get;set;} // serialized object via XmlSerializer
}
Then after serialization and receiving object in the client it can be deserialized
Data objectData;// we got it from service
var serializer = new XmlSerializer(typeof(objectData.TypeName));
object result;
using (TextReader reader = new StringReader(objectData.Xml))
{
result = serializer.Deserialize(reader);
}
Also binary serialization can be used instead of XML, then there will be byte[] property.
2) Second solution will be using binary stream and write type name in the header of message, the approach is similar to the first point.
All depends on situation, but I recommend it for big files (> 1 MB).
Upvotes: 1