如何从WCF擦除datacontract XLMS?

时间:2016-06-02 23:15:35

标签: c# xml web-services wcf soap

我正在尝试向WCF服务发送soap请求。我正在尝试使用System.ServiceModel.Channels.Message.CreateMessage()方法生成Soap请求,但是我得到了这个xml

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header>
      <h:AuthHeader xmlns:h="http://schemas.multi-mit.com/SLMi5" xmlns="http://schemas.multi-mit.com/SLMi5" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <Username>GMMX166821</Username>
         <Password>Password1</Password>
      </h:AuthHeader>
      <Timestamp xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
         <Created xmlns="http://schemas.datacontract.org/2004/07/LeadIt">2016-06-02T17:25:38.5022598Z</Created>
         <Expires xmlns="http://schemas.datacontract.org/2004/07/LeadIt">2016-06-02T17:30:37.5022598Z</Expires>
      </Timestamp>
      <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
         <usernameToken xmlns="http://schemas.datacontract.org/2004/07/LeadIt">
            <Password>SonicRS2017</Password>
            <Username>cheval</Username>
         </usernameToken>
      </Security>
   </s:Header>
   <s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <GetLead xmlns="http://schemas.multi-mit.com/SLMi5">
         <LeadID>123456</LeadID>
      </GetLead>
   </s:Body>
</s:Envelope>

问题是,webservice返回错误,我尝试调试xml,错误是这部分xmlns="http://schemas.datacontract.org/2004/07/LeadIt"xmlns:i="http://www.w3.org/2001/XMLSchema-instance";是否可以删除该部分?

我用来生成标题的代码是

Security security = new Security();
Timestamp timestamp = new Timestamp();

MessageHeader time = MessageHeader.CreateHeader("Timestamp","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", timestamp);
MessageHeader secu = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", security);

request.Headers.Add(time);
request.Headers.Add(secu);

public class Timestamp
{
  public DateTime Created;
  public DateTime Expires;

  public Timestamp()
  {
    Created = DateTime.UtcNow;
    Expires = DateTime.Now.AddSeconds(299);
  }
}

public class Security
{
  public Security()
  {
    usernameToken = new UsernameToken();
  }

  private UsernameToken usernameToken;

  public UsernameToken UsernameToken
  {
    get
    {
      return usernameToken;
    }
    set
    {
      usernameToken = value;
    }
  }
}

我尝试使用[DataContract(Namespace = "")]但是当我尝试它并没有显示节点时间戳和安全性的内容时,希望你能帮助我,抱歉我的英语不好,不是我的第一语言

1 个答案:

答案 0 :(得分:0)

  

我正在尝试向WCF服务发送soap请求

有许多方法可以从WCF客户端使用WCF服务,其中任何一种方法都不应该涉及自己编写soap包装器。 WCF框架旨在使您不必进行此操作。

您可以使用ChannelFactory直接使用服务,或者如果可用,通过使用服务元数据生成代理。