I am learning WCF. where I tried to return an object from my service to client after the operation completed from my client as required. But Its not giving any error and also not returning the result.
Service.svc.cs
public class myWCF : ImyWCF
{
public int myAddition(int IP_One, int IP_Two, out int IP_Three)
{
IP_Three = IP_One + IP_Two;
return IP_Three;
}
public MYTYPE mySubstraction()
{
MYTYPE mType = new MYTYPE();
mType.InpOne = 10;
mType.InpTwo = 20;
mType.InpThree = mType.InpTwo - mType.InpOne;
return mType;
}
}
Interface and Classes
[ServiceContract]
public interface ImyWCF
{
[OperationContract]
int myAddition(int IP_One, int IP_Two, out int IP_Three);
[OperationContract]
MYTYPE mySubstraction();
}
[DataContract]
public class myArithmatics
{
[DataMember]
public int IP_One;
[DataMember]
public int IP_Two;
[DataMember]
public int IP_Three;
}
[Serializable]
[DataContractAttribute(IsReference=true)]
public class MYTYPE
{
public int inpOne = 0;
public int inpTwo = 0;
public int inpThree = 0;
[DataMemberAttribute]
public int InpOne
{
get { return inpOne; }
set { inpOne = value; }
}
[DataMemberAttribute]
public int InpTwo
{
get { return inpTwo; }
set { inpTwo = value; }
}
[DataMemberAttribute]
public int InpThree
{
get { return inpThree; }
set { inpThree = value; }
}
}
Client App
Console.WriteLine("Service Started");
ClientMyWCF.ImyWCFClient oMYWcf = new ClientMyWCF.ImyWCFClient();
int INP_One = 10;
int INP_Two = 20;
int INP_Three = 0;
oMYWcf.myAddition(out INP_Three,INP_One,INP_Two);
Console.WriteLine("Out put from Service :"+ INP_Three.ToString());
ClientMyWCF.MYTYPE objMT = new ClientMyWCF.MYTYPE();
objMT.InpOne = 10;
objMT.InpTwo = 20;
//objMT.InpThree = 0;
oMYWcf.mySubstraction();
Console.WriteLine("Out put from Service :" + objMT.InpThree.ToString());
Console.ReadLine();
So any idea how to get the object returned?
I’m not sure why you’re using an OUT parameter in the first call when you’re returning the result of the operation as well as the OUT parameter.
Also, in the first method call (addition) you have the parameters in the wrong order.
In the second method call, you’re not assigning the MYTYPE object to a variable.
I would suggest the following code:
Use the following in your program code: