EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Dispose or Not Dispose

Also by EldoS: Solid File System
A virtual file system that offers a feature-rich storage for application documents and data with built-in compression and encryption.
#30555
Posted: 09/01/2014 03:58:21
by Fernando Del Canio (Standard support level)
Joined: 06/24/2014
Posts: 14

Hello,

we are developing a service that have to sign xmls with xades every minute zip the result files and upload them by SFTP.

I have the speed as main requeriment. The keys, main properties, etc. are the same between each execution.

My question is:
For performance reason (speed) Can I shared the TElXMLSigner, TElXAdESSigner, TElXMLKeyInfoX509Data, TElX509Certificate, ... between every execution without dispose the objects, the memory or other resources could be affected negatively?

The same for:
TElZipWriter
TElSimpleSFTPClient, TElSSHMemoryKeyStorage, TElSSHKey

Thanks in advance
#30556
Posted: 09/01/2014 04:01:38
by Eugene Mayevski (EldoS Corp.)

You can (I'd say should) keep all instances of object if they are reused all the time, but be sure to perform closing the documents where this is applicable (ZIP archive, XML etc).

Should there be any problem in this scenario (eg. the second and consequent operation would fail), please report it to us, so that we can find what's up with it.


Sincerely yours
Eugene Mayevski
#30564
Posted: 09/01/2014 08:27:05
by Fernando Del Canio (Standard support level)
Joined: 06/24/2014
Posts: 14

Thank you very much
#30650
Posted: 09/09/2014 03:24:58
by Fernando Del Canio (Standard support level)
Joined: 06/24/2014
Posts: 14

Hello,

Do you think this code is good for production?

I have a unique instance of the class between signatures (is a service), in the constructor i get the certificate and initialize the xadesSigner and signer, all of them are global vars then i call every 5 minutes a method to sign one file, in this method i dispose some objects.

I want to know if i'm in good way sharing instances and disposing object in each iteration (i wasn't able to dispose the signer.Signature, because in the next try the signer.UpdateReferencesDigest() throw and exception when internally try to do a Count "null reference")

This is the code:

Code

Constructor:
            LoadCertAndGenerateKeyData();
            CreateXadesSigner();
            SetSignerProperties();

Sign:

            TElXMLReferenceList treflist = new TElXMLReferenceList();
            TElXMLReference tref = new TElXMLReference();
            TElXMLDOMDocument xmlDocument = new TElXMLDOMDocument();

            xmlDocument.LoadFromFile(origenName, "utf-16", true);

        
                //IS THE ORDER OF THESE SENTENCE RIGHT? Update/add/generate
                //calculate the digest which will be signed
                signer.UpdateReferencesDigest();
                tref.DigestMethod = SBXMLSec.Unit.xdmSHA256;
                tref.URI = "#ManifestId";
                signer.References.Add(tref);                
                signer.GenerateSignature();

                var obj = new SBXMLSig.TElXMLObject();
                signer.Signature.Objects.Add(obj);
                var manifest = new SBXMLSig.TElXMLManifest();
                manifest.ID = "ManifestId";
                obj.DataList.Add(manifest);

                tref = new TElXMLReference();
                tref.DigestMethod = SBXMLSec.Unit.xdmSHA256;
                tref.URI = URI;
                tref.URINode = xmlDocument.DocumentElement;
                tref.UpdateDigestValue();

                manifest.Add(tref);
                  

...

signer.Save(ref node);

            using (FileStream F = new FileStream(signFileDestName, FileMode.Create, FileAccess.ReadWrite))
            {
                xmlDocument.SaveToStream(F, SBXMLDefs.Unit.xcmNone, "utf-16");
            }

            
            //Clean
            for (var i = 0; i < signer.References.Count; i++)
            {
                signer.References.get_Reference(i).Dispose();
            }
            signer.References.Clear();

            if (signer.Signature.Objects != null && signer.Signature.Objects.Count > 0) //Ojo si algún día metemos más objects.
            {
                SBXMLSig.TElXMLObject obj = signer.Signature.Objects.get_Objects(0);
                SBXMLSig.TElXMLManifest manifest = (SBXMLSig.TElXMLManifest)obj.DataList[0];
                manifest.get_Reference(0).Dispose();
                manifest.Dispose();
                obj.Dispose();
            }

            // if i uncomment this line in the next sign the signer.UpdateReferencesDigest(); throw an exception
            //signer.Signature.Dispose();

Thank you very much.
#30651
Posted: 09/09/2014 03:56:56
by Dmytro Bogatskyy (EldoS Corp.)

Hello,
Quote

I want to know if i'm in good way sharing instances and disposing object in each iteration (i wasn't able to dispose the signer.Signature, because in the next try the signer.UpdateReferencesDigest() throw and exception when internally try to do a Count "null reference")

You don't need to dispose any of those objects (manifest, obj while they are added to the signature, and signer.Signature is internal object it will be disposed on GenerateSignature method call or when a TElXMLSigner object is disposed).
#30659
Posted: 09/10/2014 03:42:08
by Fernando Del Canio (Standard support level)
Joined: 06/24/2014
Posts: 14

Hello i have found a problem with some xmls in .net.
When TElXMLDOMDocument loadFromFile normalize parameter is activated and exception is raised with some files always, if normalize parameter is deactivated works as expected.

The xml file is autogenerated with xmlSerializer. I can't attached the sample file is bigger than 256kb. I can send to you by email if is needed.

Serialization:
Code
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                using (XmlWriter xmlWriter = XmlWriter.Create(fs, settings))
                {
                    serializer.Serialize(xmlWriter, obj);
                }
            }          


Code
xmlDocument.LoadFromFile(origenName, "utf-16", true);


The exception message and stacktrace:
{"XML parse error at position 1151022 (0x11902E): TElXMLDOMElement: closing tag name mismatch: [Jugador] vs [SaldoInicial]"}

en SBXMLCore.TElXMLParser.RaiseExc(String aMsg)
en SBXMLCore.TElXMLDOMElement.ParseInnerXMLFromStream(Int16 Mode)
en SBXMLCore.TElXMLDOMElement.ParseFromStream(Int16 Mode, Boolean aReload)
en SBXMLCore.TElXMLDOMNode.SafeParseFromStream(Int16 Mode, Boolean aReload)
en SBXMLCore.TElXMLDOMElement.ParseInnerXMLFromStream(Int16 Mode)
en SBXMLCore.TElXMLDOMElement.ParseFromStream(Int16 Mode, Boolean aReload)
en SBXMLCore.TElXMLDOMNode.SafeParseFromStream(Int16 Mode, Boolean aReload)
#30660
Posted: 09/10/2014 03:49:41
by Dmytro Bogatskyy (EldoS Corp.)

Let's continue in HelpDesk ( https://www.eldos.com/helpdesk/ ) please. I have created a new support ticket based on your above message. You will see your (and only your) support tickets by following this URL. You will also get e-mail notifications about updates related to your support ticket.
Also by EldoS: CallbackDisk
Create virtual disks backed by memory or custom location, expose disk images as disks and more.

Reply

Statistics

Topic viewed 953 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!