public SoapXmlReader() { var path = "Xml\\Return\\AirShoppingRS.xml"; //var path = "Xml\\Return\\Errors.xml"; string xml = File.ReadAllText(path); var stream = new FileStream(path, FileMode.Open); ISoapFormatter formatter = new SoapPlainFormatter(); var result = (AirShoppingRS)formatter.Deserialize(stream).GetBodyObject(typeof(AirShoppingRS), "http://www.iata.org/IATA/EDIST"); stream.Close(); //XDocument xDoc = XDocument.Load(new StringReader(xml)); //var unwrappedResponse = xDoc.Descendants((XNamespace) "http://schemas.xmlsoap.org/soap/envelope/" + "Body"); //var result = unwrappedResponse.First().FirstNode.ToString(); //XmlSerializer deSerializer = new XmlSerializer(typeof(AirShoppingRS)); //using (StringReader reader = new StringReader(result)) //{ // var air = deSerializer.Deserialize(reader); //} }
//This will get called by whomever called BeginReceive on this Mailbox. //This is the part where we deserialize the incoming MailMessages into SoapEnvelopes. public SoapEnvelope[] EndReceive( IAsyncResult result ) { Mailbox.MailClientAsyncResult ar = result as Mailbox.MailClientAsyncResult; if ( null == ar ) throw new ArgumentException( "AsyncResult not obtained from Mailbox.BeginReceive()", "result" ); //This line is also very important, as it's the second half of the exception marshalling story. Any //exceptions that occured during the async operation will be rethrown here. AsyncResult.End( result ); //We need a UTF8 encoding because we're going to be mucking about //with MemoryStreams UTF8Encoding encoding = new UTF8Encoding( ); SoapEnvelope[ ] envelopes; envelopes = new SoapEnvelope[ ar.Messages.Length ]; //Iterate over each of for ( int i = 0; i < envelopes.Length; i++ ) { try { SimpleMailMessage m = ar.Messages[ i ]; if ( m != null ) { dump(i,m); //Wrap a MemoryStream around the message body and deserialize it using //the SoapPlainFormatter (we don't care about DIME attachments here). ISoapFormatter formatter = new SoapPlainFormatter( ); MemoryStream stream = new MemoryStream( encoding.GetBytes( m.TextDataString ) ); try { envelopes[ i ] = formatter.Deserialize( stream ); } catch { //swallow any exceptions that happened during deserialization -- a non-SOAP message //shouldn't crash us } //We need to set up some addressing headers here. This attaches an implicit "Via" to the //message, indicating it came in on this transport. if ( envelopes[ i ] != null ) { SoapEnvelope envelope = envelopes[ i ]; AddressingHeaders headers = envelope.Context.Addressing; Uri remoteEndpoint; Uri localEndpoint = this.EndpointUri; if ( m.From[ 0 ] != null && m.From[ 0 ].Address != String.Empty ) remoteEndpoint = SoapSmtpUri.UriFromAddress( m.From[ 0 ].Address ); else remoteEndpoint = localEndpoint; //AddressingHeaders.SetRequestHeaders() does most of the hard work for us. //Note that if you don't have MessagingConfiguration.EnableRedirectedResponses set to //true in configuration, all replies will get routed to localEndpoint (which won't do much //other than cause DispatchFailed events) headers.SetRequestHeaders( new Via( localEndpoint ), new Via( remoteEndpoint ) ); } } } catch ( Exception e ) { EventLog.WriteError( e.Message + "\nCould not deserialize message: " + ar.Messages[ i ].TextDataString ); } } return envelopes; }
//Writes a SoapEnvelope to a UTF8-encoded MemoryStream and mails it via SMTP. public void doSend( SoapEnvelope e, Uri destination ) { ISoapFormatter formatter = new SoapPlainFormatter( ); UTF8Encoding encoding = new UTF8Encoding( ); MemoryStream stream = new MemoryStream( ); formatter.Serialize( e, stream ); string smtpServer = Options.SmtpServer; string to = SoapSmtpUri.AddressFromUri( destination ); string from = this.Address; string subject = ( null == e.Context.Addressing.Action ) ? "soap.smtp" : e.Context.Addressing.Action.ToString( ); log.Debug(string.Format("send:to -- {0}" , to)); log.Debug(string.Format("send:subject -- {0}", subject)); System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage( from, to ); message.Subject = subject; message.Body = encoding.GetString( stream.GetBuffer( ) ); System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient( smtpServer ); client.Send( message ); }