Serializable issue on PrintDocument class

493 views Asked by At

I am using a software called Laserfiche. This has a workflow server and an SDK that allows you to use your own custom activities. There is a requirement when you create a custom activity. It must be serialize. So I put the property [Serializable] on all the classes. The issue is when I use the System.Drawing.Printing.PrintDocument class. I get:

02/09/2013 13:07:12 v9SignoffAuthentication Type 'System.Drawing.Printing.PrintDocument' in Assembly 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

I google a bit and found that PrintDocument cannot be serialize. I add the property

[NonSerialized]
PrintDocument pd;

And the custom activity run but not completely. I get an exception on

 pd.Print();

This is inside an override method

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

The error is

System.NullReferenceException: Object reference not set to an instance of an object. at WFPrintCustomActivity.WFPrintCustomActivity.Execute(ActivityExecutionContext executionContext)||02/09/2013 13:15:38

If I mark an object as NonSerialized, I can't call one of this methods later?

Any ideas?

Thanks

Gianfranco

1

There are 1 answers

0
gianfrancopintus On

Thanks Hans Passant, That was the answer. I was initializing pd inside the

  Public WFPrintCustomActivity()
            {
                InitializeComponent();

                settings = new Settings();
                //Logs("STARTED");


                pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            }

I commented those 2 lines

    pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

And initialize pd inside

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

Which runs first. Now is working.

This is the first time I ask something on stackoverflow and I wanted to mark your comment as an answer. I don