3

这是 WCF 服务的内容,我想同时从用户那里获取 5 个值,并向他们发送前两个值相加作为 AdditionalResult 的响应,相加结果的减法和第三个值作为 SubtractedResult,SubtractedResult 的乘法和第四个值作为 MultipliedResult 和multipliedResult 和第 5 个值的除法为 DividedResult。

我知道这看起来毫无意义,但我正在尝试对这些进行一些预先测试,但我是 WCF 的新手,任何可以提供帮助的人都会受到赞赏。

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;
    }


}

如何同时返回 4 个值?

4

2 回答 2

7

您需要创建DataContract并返回该对象。

[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 that ReturnValue class will be automatically generated by the same code that generates your client proxy (be it the tool built in to visual studio or svcutil.exe)

Here is the MSDN Page on DataContracts.

于 2013-02-06T21:10:16.617 回答
2

您应该创建一个结构并在同一个调用中将它们全部返回。

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;
}
于 2013-02-06T21:08:48.807 回答