How can I send multiple return values in one WCF request?
This is what a WCF service is: I want to get 5 values from users at the same time and send them a response of adding the first two values as AddedResult, subtracting the added result and third value as SubtractedResult, multiplying SubtractedResult and fourth value as MultipliedResult and dividing multipiedResult and 5th value as DividedResult.
I know this looks unwise, but I am trying to do some preliminary tests with them, but I am new to WCF who can help, would really be appreciated.
public interface IService1
{
[OperationContract]
string GetValuesForCalculation(int value1, int value2, int value3, int value4, int value5);
// TODO: Add your service operations here
}
public class Service1 : IService1
{
public int GetValuesForCalculation(int value1, int value2, int value3, int value4, int value5)
{
int AddedResult;
int SubtractedResult;
int MultipliedResult;
int DividedResult;
AddedResult = (value1 + value2);
SubtractedResult = (AddedResult - value3);
MultipliedResult = (SubtractedResult - value4);
DividedResult =(MultipliedResult/value5);
return AddedResult;
return SubtractedResult;
return MultipliedResult;
return DividedResult;
}
}
How do I return 4 values at the same time?
source to share
You need to make DataContract
and return this object.
[ServiceContract()]
interface IService1
{
[OperationContract]
ReturnValue GetValuesForCalculation(int value1, int value2, int value3, int value4, int value5);
}
public class Service1 : IService1
{
public ReturnValue GetValuesForCalculation(int value1, int value2, int value3, int value4, int value5)
{
int AddedResult;
int SubtractedResult;
int MultipliedResult;
int DividedResult;
AddedResult = (value1 + value2);
SubtractedResult = (AddedResult - value3);
MultipliedResult = (SubtractedResult - value4);
DividedResult =(MultipliedResult/value5);
var returnValue = new ReturnValue(AddedResult, SubtractedResult, MultipliedResult,DividedResult);
return returnValue ;
}
}
[DataContract]
public class ReturnValue
{
public ReturnValue(int addedResult, int subtractedResult, int multipliedResult, int dividedResult)
{
AddedResult = addedResult;
SubtractedResult = subtractedResult;
MultipliedResult = multipliedResult;
DividedResult = dividedResult;
}
[DataMember]
public int AddedResult {get; set;}
[DataMember]
public int SubtractedResult {get; set;}
[DataMember]
public int MultipliedResult {get; set;}
[DataMember]
public int DividedResult {get; set;}
}
On your client side, the class ReturnValue
will be automatically generated by the same code that generates your client proxy (be it a tool built into visual studio or svcutil.exe )
Here is the MSDN Page at DataContracts
.
source to share
You have to create a structure and return all of them in the same call.
public class Service1 : IService1
{
public int GetValuesForCalculation(int value1, int value2, int value3, int value4, int value5)
{
int AddedResult;
int SubtractedResult;
int MultipliedResult;
int DividedResult;
AddedResult = (value1 + value2);
SubtractedResult = (AddedResult - value3);
MultipliedResult = (SubtractedResult - value4);
DividedResult =(MultipliedResult/value5);
CallResponse myResponse = new CallREsponse {AddResult = AddedResult,
SubtractResult = SubtractedResult,
MultiplyResult = MultipliedResult,
DivideResult = DividedResult};
return myResponse;
}
}
public struct CallResponse
{
int AddResult;
int SubtractResult;
int MultiplyResult;
int DivideResult;
}
source to share