public WriteMessageAsyncResult(Message message, Stream stream, TextMessageEncoder textEncoder, AsyncCallback callback, object state)
                    : base(callback, state)
                {
                    this.message     = message;
                    this.textEncoder = textEncoder;
                    this.xmlWriter   = textEncoder.TakeStreamedWriter(stream);


                    this.eventTraceActivity = null;
                    if (TD.TextMessageEncodingStartIsEnabled())
                    {
                        this.eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message);
                        TD.TextMessageEncodingStart(this.eventTraceActivity);
                    }

                    if (!textEncoder.optimizeWriteForUTF8)
                    {
                        xmlWriter.WriteStartDocument();
                    }

                    IAsyncResult result = message.BeginWriteMessage(this.xmlWriter, PrepareAsyncCompletion(onWriteMessage), this);

                    if (SyncContinue(result))
                    {
                        this.Complete(true);
                    }
                }
Example #2
0
            public WriteMessageAsyncResult(Message message, Stream stream, AsyncCallback callback, object state)
                : base(callback, state)
            {
                this.message = message;
                this.stream  = stream;
                this.writer  = new XmlByteStreamWriter(stream, false);
                if (onCleanup == null)
                {
                    onCleanup = new Action <IAsyncResult, Exception>(Cleanup);
                }
                this.OnCompleting += onCleanup;
                Exception completionException = null;
                bool      completeSelf        = false;

                this.eventTraceActivity = null;
                if (TD.ByteStreamMessageEncodingStartIsEnabled())
                {
                    this.eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message);
                    TD.ByteStreamMessageEncodingStart(this.eventTraceActivity);
                }

                try
                {
                    IAsyncResult result = message.BeginWriteMessage(writer, PrepareAsyncCompletion(HandleWriteMessage), this);
                    completeSelf = SyncContinue(result);
                }
                catch (Exception ex)
                {
                    if (Fx.IsFatal(ex))
                    {
                        throw;
                    }
                    completeSelf        = true;
                    completionException = ex;
                }

                if (completeSelf)
                {
                    this.Complete(true, completionException);
                }
            }