Reputation: 5281
Is possible returning 2 different types addicted on condition?
for example:
[OperationContract]
[WebInvoke(
Method = "GET",
UriTemplate = "/GetMyResult",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)]
object GetMyResult(int x);
and function declaration:
public object GetMyResult(string x)
{
if (x)
return new UserEntity () {...} // some object with properties
else
return new MessageErr() {...} // object with 2 properties Text and error code
}
This code do not work for me. Is some good practice how can I do that ? Thanks
Upvotes: 0
Views: 63
Reputation: 14624
No you can't return object
in WCF
because service
is meant to clearly describe what it needs
and what it will return
.
But there is another way by which you can do this which i have implemented in my project which is similar as your needs.
You can make a class
with a property
where you will send the object
.
public class CustomResponse
{
public CustomData Data { get; set; }
}
[KnownType(typeof(UserEntity))]
[KnownType(typeof(MessageErr))]
[DataContract]
public class CustomData
{
[DataMember]
public object Data { get; set; }
public CustomData(object obj)
{
this.Data = obj;
}
}
And here is how you will call it
CustomResponse Response = new CustomResponse();
UserEntity obj = new UserEntity();
Response.Data = new CustomData(obj);
Upvotes: 1