Custom System.IO.Packaging - ContentType problem

Unanswered Custom System.IO.Packaging - ContentType problem

  • 2008年4月29日 下午 01:52
     
     

    Hi,

     

    I've tried to implement custom Packaging (over SQL CE, but it is not important for this problem).

     

    When I inherit from PackagePart I want use base constructor that takes Package and Uri as arguments and override GetContentTypeCore method to provide content string a bit later than in ctor.

     

    Unfortunatelly _contentType private field is initialized during ctor to null in case of missing contenttype. It can be initialized later on access to ContentType property (it will hit GetContentypeCore method).

    There is additional internal property ValidatedContentType, but it simply returns _contentType.

     

    Relationship collection access only this ValidatedContentType collection so it happens that it can get null _contentType field value triggering exception.

     

    After this long introduction there are questions:

     

    1. Is this bug in ValidatedContentType property that is doesn't call GetContentTypeCore in some cases?

    2. Lack of information in documentation that it is for internal purposes?

    3. It's feature by design and ther eis lack of information in documentaion how to correctly use it?

     

    Regards,

    Wojciech Gebczyk

     

     

    Sample code with "buggy" behavior:

    using System;
    using System.IO;
    using System.IO.Packaging;
    using System.Net.Mime;

    class Program {
        static void Main() {
            new MyPackage(FileAccess.ReadWrite)
                .GetPart(MyPackage.InvalidPartUri)
                .GetRelationships();
        }
    }


    class MyPackage: Package {
        public static readonly Uri InvalidPartUri = new Uri("/aaa.txt", UriKind.Relative);

        public MyPackage(FileAccess openFileAccess)
            : base(openFileAccess) {}

        protected override PackagePart CreatePartCore(Uri partUri, string contentType, CompressionOption compressionOption) {
            return new InvalidPackagePart(this, partUri);
        }
        protected override void DeletePartCore(Uri partUri) { }
        protected override void FlushCore() { }
        protected override PackagePart GetPartCore(Uri partUri) {
            return new InvalidPackagePart(this, partUri);
        }
        protected override PackagePart[] GetPartsCore() { return new PackagePart[0]; }
    }
    class InvalidPackagePart: PackagePart {
        public InvalidPackagePart(Package package, Uri partUri)
            : base(package, partUri)
        {}
        protected override string GetContentTypeCore() {
            return MediaTypeNames.Text.Plain;
        }
        protected override Stream GetStreamCore(FileMode mode, FileAccess access) { return new MemoryStream(); }
    }