public void Work(object state)
            {
                // Prepare stream
                Stream                s       = null;
                FileSendResult        result  = new FileSendResult();
                IGraphable            data    = (IGraphable)state;
                IFormatterGraphResult fResult = null;

                try
                {
                    // Graph the object.
                    //We graph to a memory stream and transfer to a file stream on success because
                    //the state of the stream is unknown if the result code is not accepted. This
                    //could cause a process which is listening to new files to read an invalid or
                    //non-conformant message from the publish service. In this model, we graph,
                    //verify and finally commit.
                    MemoryStream ms = new MemoryStream();
                    fResult        = Formatter.Graph(ms, data);
                    result.Code    = fResult.Code;
                    result.Details = fResult.Details;

                    // Did the operation succeed?
                    if (result.Code == ResultCode.Accepted ||
                        result.Code == ResultCode.AcceptedNonConformant)
                    {
                        //TODO: Should transfer in chunks instead of all at once.
                        s = System.IO.File.Create(TargetFile);
                        ms.WriteTo(s);
                        ms.Close();
                    }
                }
                catch (MessageValidationException e)
                {
                    result.Code = ResultCode.Rejected;
                    List <IResultDetail> dtl = new List <IResultDetail>(new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) });
                    dtl.AddRange(fResult.Details ?? new IResultDetail[0]);
                    result.Details = dtl.ToArray();
                }
                catch (FormatException e)
                {
                    result.Code    = ResultCode.Rejected;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                catch (DirectoryNotFoundException e)
                {
                    result.Code    = ResultCode.NotAvailable;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                catch (IOException e)
                {
                    result.Code    = ResultCode.Error;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                catch (Exception e)
                {
                    result.Code    = ResultCode.Error;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                finally
                {
                    if (s != null)
                    {
                        s.Close();
                    }
                    if (result.Code != ResultCode.Accepted)
                    {
                        System.IO.File.Delete(TargetFile);
                    }
                }

                this.Result = result;

                // Fire completed event
                if (Completed != null)
                {
                    Completed(this);
                }
            }
            public void Work(object state)
            {
                // Prepare stream
                Stream s = null;
                FileSendResult result = new FileSendResult();
                IGraphable data = (IGraphable)state;
                IFormatterGraphResult fResult = null;

                try
                {
                    // Graph the object.
                    //We graph to a memory stream and transfer to a file stream on success because
                    //the state of the stream is unknown if the result code is not accepted. This
                    //could cause a process which is listening to new files to read an invalid or
                    //non-conformant message from the publish service. In this model, we graph,
                    //verify and finally commit.
                    MemoryStream ms = new MemoryStream();
                    fResult = Formatter.Graph(ms, data);
                    result.Code = fResult.Code;
                        result.Details = fResult.Details;
                    
                    // Did the operation succeed?
                    if (result.Code == ResultCode.Accepted || 
                        result.Code == ResultCode.AcceptedNonConformant)
                    {
                        //TODO: Should transfer in chunks instead of all at once.
                        s = System.IO.File.Create(TargetFile);
                        ms.WriteTo(s);
                        ms.Close();

                    }
                }
                catch (MessageValidationException e)
                {
                    result.Code = ResultCode.Rejected;
                    List<IResultDetail> dtl = new List<IResultDetail>(new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) });
                    dtl.AddRange(fResult.Details ?? new IResultDetail[0]);
                    result.Details = dtl.ToArray();
                }
                catch (FormatException e)
                {
                    result.Code = ResultCode.Rejected;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                catch (DirectoryNotFoundException e)
                {
                    result.Code = ResultCode.NotAvailable;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                catch (IOException e)
                {
                    result.Code = ResultCode.Error;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                catch (Exception e)
                {
                    result.Code = ResultCode.Error;
                    result.Details = new IResultDetail[] { new FileResultDetail(ResultDetailType.Error, e.Message, TargetFile, e) };
                }
                finally
                {
                    if (s != null) s.Close();
                    if (result.Code != ResultCode.Accepted)
                        System.IO.File.Delete(TargetFile);
                }

                this.Result = result;

                // Fire completed event
                if (Completed != null) Completed(this);

            }