如何更改Soap XML元素名称'ArrayOfResponse'XML WCF服务

问题描述:

我的问题如何更改SOAP XML中的ArrayOfResponse元素WCF服务 使用Message类....请参阅图片我想更改红色框中的元素... 任何想法???如何更改Soap XML元素名称'ArrayOfResponse'XML WCF服务

enter image description here

我的代码来得到这样的结果!

我的界面

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

这种方法的任务之一参数EvaluationRequest类,返回消息

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

请求类(GetEval的参数uations法)

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

任何人,谁愿意消费你的服务,不知道他会reveice什么样的结果。它只是“消息”,并没有具体的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); 
} 

的金科玉律是:尽量不要在服务操作混合DataContract和MessageContract,因为WCF会将此视为安全缺陷。

所以定义BOTH类,导致参数,如DataContract OR MessageContract,但不要混合起来。