Unexpected MissingMethodException

Heiko 1,191 Reputation points
2022-07-18T12:52:05.013+00:00

In App Center I can see that one installation with build 18362 throws an unhandled MissingMethodException in the folowing method at call of AppInfo.GetFromAppUserModelId(path):

private string GetPublisherName(string path)  
{  
	if (IsWindows10BuildHigherOrEqual_19041 && ApiInformation.IsMethodPresent("Windows.ApplicationModel.AppInfo", "GetFromAppUserModelId"))  
		try  
		{  
			AppInfo	info = AppInfo.GetFromAppUserModelId(path);  
  
			if (info != null)  
			{  
				Package	pack = info.Package;  
		  
				if (pack != null)  
				{  
					path = pack.PublisherDisplayName;  
					if (!String.IsNullOrWhiteSpace(path))  
						return path;  
				}  
			}  
		}  
		catch  
		{}  
  
	return GetNameFromPath(path);  
}  

It's a WPF desktop bridge app. How can it be? I check the Window 10 build version, call IsMethodPresent and use a try-catch-block. Do I have to place the call of AppInfo.GetFromAppUserModelId(path) within a sub method, like this?

private string GetPublisherName(string path)  
{  
	if (IsWindows10BuildHigherOrEqual_19041 && ApiInformation.IsMethodPresent("Windows.ApplicationModel.AppInfo", "GetFromAppUserModelId"))  
		try  
		{  
			string name = GetPublisherNameFromPackage(path);  
  
			if (!String.IsNullOrWhitespace(name))  
				return name;  
		}  
		catch  
		{}  
  
	return GetNameFromPath(path);  
}  
  
private string GetPublisherNameFromPackage(string path)  
{  
	AppInfo	info = AppInfo.GetFromAppUserModelId(path);  
  
	if (info != null)  
	{  
		Package	pack = info.Package;  
  
		if (pack != null)  
		{  
			path = pack.PublisherDisplayName;  
			if (!String.IsNullOrWhiteSpace(path))  
				return path;  
		}  
	}  
	return null;  
}  

Maybe the JIT compiler throws the MissingMethodException already when entering GetPublisherName(string path) ?

Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,633 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nathan Manis - MSFT 471 Reputation points Microsoft Employee
    2022-08-19T13:23:11.63+00:00

    Hi @Heiko ,

    Thank you for the post. We have reviewed this further and the recommendation is to not check versions directly as that can be highly inaccurate from end users using compat mode or other software intercepting the underlying version check call. So, no need to do the IsWindowsVersionLowerThan() code.

    So, we would recommend the call to check if the underlying API is available and if so, proceed. If you do get an exception, can handle it too accordingly.

    Note that 18362 is old at this point and no longer being serviced by Microsoft:
    https://learn.microsoft.com/en-us/windows/release-health/release-information

    Version 1903 (OS build 18362) - End of servicing
    Servicing option Availability date Build KB article
    Semi-Annual Channel 2020-12-08 18362.1256 KB4592449

    For that one installation running 18362 that you found in AppCenter, they are out of support from Microsoft. To help them, our team here would require them to update the OS. Perhaps can block installs for the application on older OS too.

    Best regards,
    Nathan Manis
    Microsoft Developer Support

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Roy Li - MSFT 31,361 Reputation points Microsoft Vendor
    2022-07-19T02:25:35.34+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Please take a look at the AppInfo.GetFromAppUserModelId(String) Method, it requirements Windows 10, version 2004 (introduced in 10.0.19041.0). So the exception is expected when your app is running on build 18362 because this method does not exist in the old version of Windows SDK.

    Back to your code, you said that you checked the windows version, but the code is still calling this method. I'd suggest you check the condition code and test it on a windows 10 build 18362 device. Make sure the condition is working on that windows version.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Heiko 1,191 Reputation points
    2022-07-28T14:56:14.327+00:00

    I already posted the main source code to reproduce this issue. I had similar issues a few years ago with a pure UWP app. I think the if statement and the call to the method whose existence is not certain should not be in the same method. If the method is not availabe, the containing method should not be called at all. This is a WPF-UWP bridge app.

    private static bool IsWindows10BuildHigherOrEqual_19041;  
      
    public MainWindow()  
    {  
    	IsWindows10BuildHigherOrEqual_19041 = !IsWindowsVersionLowerThan(10, 0, 19041, 0);  
    }  
      
    internal static bool IsWindowsVersionLowerThan(uint major, uint minor = 0, uint build = 0, uint revision = 0)  
    {  
    	if (ulong.TryParse(AnalyticsInfo.VersionInfo.DeviceFamilyVersion, out ulong version))  
    	{  
    		uint    _major = (uint)((version & 0xFFFF000000000000UL) >> 48),  
    			_minor = (uint)((version & 0x0000FFFF00000000UL) >> 32),  
    			_build = (uint)((version & 0x00000000FFFF0000UL) >> 16),  
    			_revision = (uint)(version & 0x000000000000FFFFUL);  
      
    		if (_major > major || (_major == major && (_minor > minor || _minor == minor && (_build > build || _build == build && _revision >= revision))))  
    			return false;  
    	}  
    	return true;  
    }  
      
    private async void Window_Loaded(object sender, RoutedEventArgs e)  
    {  
    	SetPublisherNames();  
    }  
      
    private async Task SetPublisherNames()  
    {  
    	try  
    	{  
    		string	name = await GetPublisherName("Microsoft.WindowsCalculator_8wekyb3d8bbwe!App");  
    	//	string	name = await GetPublisherName("Microsoft.WindowsFeedbackHub_8wekyb3d8bbwe!App");  
    	}  
    	catch (MissingMethodException ex)  
    	{  
    	}  
    }  
      
    private string GetPublisherName(string path)  
    {  
    	if (IsWindows10BuildHigherOrEqual_19041 && ApiInformation.IsMethodPresent("Windows.ApplicationModel.AppInfo", "GetFromAppUserModelId"))  
    		try  
    		{  
    			AppInfo    info = AppInfo.GetFromAppUserModelId(path);  
      
    			if (info != null)  
    			{  
    				Package    pack = info.Package;  
      
    				if (pack != null)  
    				{  
    					path = pack.PublisherDisplayName;  
    					if (!String.IsNullOrWhiteSpace(path))  
    						return path;  
    				}  
    			}  
    		}  
    		catch  
    		{}  
      
    	return GetNameFromPath(path);  
    }  
      
    private string GetNameFromPath(string path)  
    {  
    	return "...";  
    }