Umakanta.Swain
Umakanta.Swain

Reputation: 1117

Returning Object from WCF Service

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?

Upvotes: 1

Views: 6290

Answers (4)

Tariq
Tariq

Reputation: 31

Remove the SerializableAttribute e.g; [Serializable] because there is no need to be serialize this object.

  1. For class , use attribute [DataContract]
  2. For class level properties , use attribute [DataMemberAttribute]
  3. to expose the method , use attirbute [OperationContract]

these are standards for wcf service.

Upvotes: 2

A-Dubb
A-Dubb

Reputation: 1709

You can also remove these 2 attributes all together

[Serializable]
[DataContractAttribute(IsReference=true)]

By default, all public properties will be serialized. Also seeing your proxy would help. There are certain classes you should be inheriting from. I'm also not seeing a new instance of ServiceHost(typeof(MyService), myUri) being initialized which is strange. Maybe you have it encapsulated somewhere. Anyway, it's typical to construct a new channel using the ChannelFactory as such and to host a new service as such.

Upvotes: 1

Tim
Tim

Reputation: 28530

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:

public class myWCF : ImyWCF
{
    // remove the OUT parameter
    public int myAddition(int IP_One, int IP_Two)
    {
        return IP_One + IP_Two;
    }
    public MYTYPE mySubstraction()
    {
        MYTYPE mType = new MYTYPE();
        mType.InpOne = 10;
        mType.InpTwo = 20;
        mType.InpThree = mType.InpTwo - mType.InpOne;
        return mType;
    }
}

Use the following in your program code:

Console.WriteLine("Service Started");
ClientMyWCF.ImyWCFClient oMYWcf = new ClientMyWCF.ImyWCFClient();
int INP_One = 10;
int INP_Two = 20;
int INP_Three = 0;

INP_Three = oMYWcf.myAddition(INP_One, INP_Two);
Console.WriteLine("Out put from Service :"+ INP_Three.ToString());

ClientMyWCF.MYTYPE objMT = new ClientMyWCF.MYTYPE();
// Not needed - these are set in the mySubtraction method
//objMT.InpOne = 10;
//objMT.InpTwo = 20;
//objMT.InpThree = 0;
objMT = oMYWcf.mySubstraction();
Console.WriteLine("Out put from Service :" + objMT.InpThree.ToString());
Console.ReadLine();

Upvotes: 3

Jack
Jack

Reputation: 715

First of all, remove SerializableAttribute, using DataContractAttribute is enough.

Why you use out in myAddition method? it already returns the result through the return value, the third parameter is unnecessary.

In your code, you don't store the result from mySubstraction, while you read the objMT.InpThree that isn't assigned any value, I don't understand.

Upvotes: 0

Related Questions