如何在Soap XML WCF服务中更改元素名称“ArrayOfResponse”

时间:2017-01-28 23:31:14

标签: c# xml wcf soap service

我的问题如何更改SOAP XML WCF服务中的ArrayOfResponse元素 使用Message类....参见图片我想更改红框中的元素.... 任何想法???

enter image description here

我的代码可以得到这个结果!

我的界面

[ServiceContract()]
    public interface IEvaluationWebService
    {
        [OperationContract(ReplyAction = "Evaluations")]
        Message GetEvaluations(EvaluationRequest evaluationRequest);
    } 

此方法任务一个参数EvaluationRequest类,返回Message(评估类列表)

Message IEvaluationWebService.GetEvaluations(EvaluationRequest evaluationRequest)
    {
        SqlConnection cnn = new SqlConnection("");

        DateTime dtstart = new DateTime();
        DateTime.TryParseExact(evaluationRequest.PeriodStart, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out dtstart);
        evaluationRequest.PeriodStart = dtstart.ToString("yyyy-MM-dd");

        DateTime dtend = new DateTime();
        DateTime.TryParseExact(evaluationRequest.PeriodEnd, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault, out dtend);
        evaluationRequest.PeriodEnd = dtend.ToString("yyyy-MM-dd");

        SqlCommand cmd = new SqlCommand("GetEmployeeEvaluation", cnn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@PeriodStart", SqlDbType.VarChar, 10).Value = evaluationRequest.PeriodStart;
        cmd.Parameters.Add("@PeriodEnd", SqlDbType.VarChar, 10).Value = evaluationRequest.PeriodEnd;

        cnn.Open();
        SqlDataReader sdr = cmd.ExecuteReader();

        List<Evaluation> evaluationList = new List<Evaluation>();

        MessageVersion ver = OperationContext.Current.IncomingMessageVersion;
        while (sdr.Read())
        {
            Evaluation evaluation = new Evaluation();
            evaluation.EstLaborOfficeId = Convert.ToInt32(sdr["EstLaborOfficeId"].ToString());
            evaluation.EstSequenceNumber = Convert.ToInt32(sdr["EstSequenceNumber"].ToString());
            evaluation.IdNumber = 
            evaluationList.Add(evaluation);
        }
        Evaluation eval = new Evaluation();
        cnn.Close();
        return Message.CreateMessage(ver, "Evaluations", evaluationList);
    }

传递方法日期并获取数据的请求类....

        [MessageContract(IsWrapped = true, WrapperName = "GetEvaluation", WrapperNamespace = "http://tempuri.org/")]
[XmlType(Namespace = "http://tempuri.org/")]
public class EvaluationRequest
{
    [MessageBodyMember(Order = 1)]
    public string PeriodStart { get; set; }

    [MessageBodyMember(Order = 2)]
    public string PeriodEnd { get; set; }
}

请求类(GetEvaluations方法的参数)

        [MessageContract(IsWrapped = true, WrapperName = "GetEvaluation", WrapperNamespace = "http://tempuri.org/")]
[XmlType(Namespace = "http://tempuri.org/")]
public class EvaluationRequest
{
    [MessageBodyMember(Order = 1)]
    public string PeriodStart { get; set; }

    [MessageBodyMember(Order = 2)]
    public string PeriodEnd { get; set; }
}

评估班

        [DataContract()]
    public class Evaluation
    {
        private int _EstLaborOfficeId;
        private int _EstSequenceNumber;
        private long _IdNumber;

        [DataMember]
        public int EstLaborOfficeId
        {
            get { return _EstLaborOfficeId; }
            set { _EstLaborOfficeId = value; }
        }

        [DataMember(Order = 2)]
        public int EstSequenceNumber
        {
            get { return _EstSequenceNumber; }
            set { _EstSequenceNumber = value; }
        }
}

1 个答案:

答案 0 :(得分:0)

任何想要消费你服务的人都不知道他会透露什么样的结果。它只是“消息”而没有特定的MessageContract。

据我所知,您想要提供一份评估清单。

您可以尝试的是:(未经测试)

[MessageContract]
public class Evaluation
{
    private int _EstLaborOfficeId;
    private int _EstSequenceNumber;
    private long _IdNumber;

    [MessageBodyMember]
    public int EstLaborOfficeId
    {
        get { return _EstLaborOfficeId; }
        set { _EstLaborOfficeId = value; }
    }

    [MessageBodyMember]
    public int EstSequenceNumber
    {
        get { return _EstSequenceNumber; }
        set { _EstSequenceNumber = value; }
    }
}



[MessageContract]
public class EvaluationList
{
     [MessageBodyMember]
     public List<Evaluation> Values {get;set}
}


[ServiceContract()]
public interface IEvaluationWebService
{
    [OperationContract]
    EvaluationList GetEvaluations(EvaluationRequest evaluationRequest);
}

...或者只是尝试使用List作为返回并忘记EvaluationList。

[ServiceContract()]
public interface IEvaluationWebService
{
    [OperationContract]
    List<Evaluation> GetEvaluations(EvaluationRequest evaluationRequest);
}

现在,EvaluationList和EvaluationRequest都是MessageContracts。

请记住,如果您想要完全控制SOAP消息,那么您只想使用MessageContract。这似乎不是这种情况。

因此,更简单的方法是将EvaluationRequest更改回DataContract:

[DataContract()]
public class Evaluation
{
    private int _EstLaborOfficeId;
    private int _EstSequenceNumber;
    private long _IdNumber;

    [DataMember]
    public int EstLaborOfficeId
    {
        get { return _EstLaborOfficeId; }
        set { _EstLaborOfficeId = value; }
    }

    [DataMember(Order = 2)]
    public int EstSequenceNumber
    {
        get { return _EstSequenceNumber; }
        set { _EstSequenceNumber = value; }
    }
}

[DataContract]
public class EvaluationRequest
{
    [DataMember]
    public string PeriodStart { get; set; }

    [DataMember]
    public string PeriodEnd { get; set; }
} 

[ServiceContract()]
public interface IEvaluationWebService
{
    [OperationContract]
    List<Evaluation> GetEvaluations(EvaluationRequest evaluationRequest);
}

GOLDEN RULE IS:尽量不要在服务操作中混用DataContract和MessageContract,因为WCF会将此视为安全漏洞。

所以定义 BOTH 类,结果 AND 参数,作为DataContract OR MessageContract,但不要混淆。