HttpBaseProtocolFilter.ClearAuthenticationCache() throw 'System.InvalidCastException'

社会主义新天地 提交于 2019-12-11 12:37:50

问题


I create an simple uwp app. The sdk version is Universal Windows 10.0.14332.0. The app does nothing, only call the function "HttpBaseProtocolFilter.ClearAuthenticationCache()". But when I call the "HttpBaseProtocolFilter.ClearAuthenticationCache()", an exception throwed:

An exception of type 'System.InvalidCastException' occurred in App2.exe but was not handled in user code

Additional information: Unable to cast object of type 'Windows.Web.Http.Filters.HttpBaseProtocolFilter' to type 'Windows.Web.Http.Filters.IHttpBaseProtocolFilter4'.

How can I use "HttpBaseProtocolFilter.ClearAuthenticationCache()"?


回答1:


According to the documentation, the ClearAuthenticationCache method is introduced in Windows.Foundation.UniversalApiContract, version 3 and is available in OS version 10.0.14295.0 and later.

It means that the Windows.Web.Http.Filters.IHttpBaseProtocolFilter4 is not available in older version and you recieve an InvalidCastException.

So if you are targetting an older version as minimum, you need to check if the API is available before calling the method:

using Windows.Foundation.Metadata;
 ...
 ...
if(ApiInformation.IsMethodPresent("Windows.Web.Http.Filters.HttpBaseProtocolFilter.ClearAuthenticationCache"))
{
    // Call the method here
}


来源:https://stackoverflow.com/questions/37200801/httpbaseprotocolfilter-clearauthenticationcache-throw-system-invalidcastexcep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!