Pyth0n
Pyth0n

Reputation: 367

Unit Test of WPF Application fails with NotSupportedException "The Uri prefix is not recognized"

I'm currently writing unit tests and at this position the tests fails with a NotSupportedException "The URI prefix is not recognized" After small research I have registered the "pack" Uri scheme, but it dosn't helps.

return _WaitImageThumbnail ?? (_WaitImageThumbnail = new BitmapImage(new Uri("pack://application:,,,/MyAssemblyName;component/images/DefaultThumbnailLoading.png")));

Stacktrace:

   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   at System.Net.WebRequest.Create(Uri requestUri)
   at MS.Internal.WpfWebRequestHelper.CreateRequest(Uri uri)
   at System.IO.Packaging.PackWebRequest.GetRequest(Boolean allowPseudoRequest)
   at System.IO.Packaging.PackWebRequest.GetResponse()
   at MS.Internal.WpfWebRequestHelper.GetResponse(WebRequest request)
   at MS.Internal.WpfWebRequestHelper.CreateRequestAndGetResponse(Uri uri)
   at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
   at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
   at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
   at System.Windows.Media.Imaging.BitmapImage.EndInit()
   at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource, RequestCachePolicy uriCachePolicy)
   at System.Windows.Media.Imaging.BitmapImage..ctor(Uri uriSource)
   ...

Question: How can I load the image, and why this exception occurs?

Upvotes: 3

Views: 3258

Answers (1)

bryanbcook
bryanbcook

Reputation: 18053

There are a few things you will have to do in order to load the image for your unit test.

Register the Pack Uri or initialize WPF Application instance

You can register the pack URI as per the previous SO question Pack Urls and Unit Testing or initialize a WPF Application which will register the WPF framework components for you. I typically do this during the assembly initialization stage.

   [AssemblyInitialize]
   public static void InitializeTestAssembly(TestContext ctx)
   {
       if (Application.Current == null)
           new Application();
   }

Embed the Image as a Resource or Setup as Deployment Item

In order to use the pack uri as you've outlined above, the image must be set as a Resource so that it gets baked into your assembly. If you're not using as a Resource, change it to Content that gets copied to the output directory and then configure the test environment to deploy the image with the test:

 [DeploymentItem("/images/DefaultThumbnailLoading.png")]
 [TestMethod]
 public void WhenPerformingLongOperation_ShouldDisbleProgressIndicator()
 {
    // test here
 }

Upvotes: 8

Related Questions