Как сериализовать пользовательский SOAP для OTRS - PullRequest
0 голосов
/ 20 марта 2019

В OTRS есть схема WSDL для SOAP, к сожалению, с автоматом (соединяющими ссылками) она не работает, потому что Sharp считает файл неверным. для нормальной работы вы должны получить следующее:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tic="http://www.otrs.org/TicketConnector/">
 <soapenv:Header/>
  <soapenv:Body>
  <SessionCreate>         
          <tic:UserLogin>ULogin</tic:UserLogin>         
          <tic:CustomerUserLogin>CustUser</tic:CustomerUserLogin>         
          <tic:Password>ULPassword</tic:Password>
  </SessionCreate>
 </soapenv:Body>
</soapenv:Envelope>

Я пробовал через System.Runtime.Serialization.Formatters.Soap следующим образом:

  [Serializable]
  public partial class Envelope
  {
      /// <remarks/>
      public object Header { get; set; }

      /// <remarks/>
      public EnvelopeBody Body { get; set; }
  }
   [Serializable]
  public partial class EnvelopeBody
  {     
     public SessionCreate SessionCreate { get; set; }
  }
  [Serializable]
  [System.Xml.Serialization.SoapType(IncludeInSchema =true, Namespace = "")]
  public partial class SessionCreate
  {

      /// <remarks/>
      [System.Xml.Serialization.SoapElement("http://www.otrs.org/TicketConnector/")]
      public string UserLogin { get; set; }

/// <remarks/>
      [System.Xml.Serialization.SoapElement("http://www.otrs.org/TicketConnector/")]
      public string CustomerUserLogin { get; set; }

/// <remarks/>
      [System.Xml.Serialization.SoapElement("http://www.otrs.org/TicketConnector/")]
      public string Password { get; set; }

  }
  class TEST
  {
    void GetSoap()
    {
 var model = new SessionCreate
    {
        UserLogin ="ULogin",
        CustomerUserLogin="CustUser",
        Password="ULPassword"
    };

    var en = new Envelope
    {
        Body = new EnvelopeBody
        {
            SessionCreate = model
        }
    };
     SoapFormatter formatter = new SoapFormatter();
    var ms = new MemoryStream();
    formatter.Serialize(ms, en);
    Console.WriteLine(StreamToString(ms));
    }
    string StreamToString(Stream stream)
      {
          stream.Position = 0;
          using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
          {
              return reader.ReadToEnd();
          }
      }
  }

В результате отображается следующее:

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <SOAP-ENV:Body>
 <a1:Envelope id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/OTRS.Request/OTRS%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
 <_x003C_Header_x003E_k__BackingField xsi:type="xsd:anyType" xsi:null="1"/>
 <_x003C_Body_x003E_k__BackingField href="#ref-3"/>
 </a1:Envelope>
 <a1:EnvelopeBody id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/OTRS.Request/OTRS%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
 <_x003C_SessionCreate_x003E_k__BackingField href="#ref-4"/>
 </a1:EnvelopeBody>
 <a1:SessionCreate id="ref-4" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/OTRS.Request/OTRS%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
 <_x003C_UserLogin_x003E_k__BackingField id="ref-5">ULogin</_x003C_UserLogin_x003E_k__BackingField>
 <_x003C_CustomerUserLogin_x003E_k__BackingField id="ref-6">CustUser</_x003C_CustomerUserLogin_x003E_k__BackingField>
 <_x003C_Password_x003E_k__BackingField id="ref-7">ULPassword</_x003C_Password_x003E_k__BackingField>
 </a1:SessionCreate>
 </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

Возникает вопрос, как это сделать нормально, и что я делаю не так?

...