.netpowershellpowershell-7.0

Migration from PowerShell 5 to 7: can't load types from System.ServiceModel.Web v4.0.0.0


Recently transitioned from PowerShell 5.1 to 7.3.6. Most scripts are fine, but there is one puzzling issue with a script calling a .NET class.

The class can't load, crashing with this message:

System.Management.Automation.ExtendedTypeSystemException: The following exception occurred while retrieving member "Parse": "Could not load type 'System.ServiceModel.Web.WebInvokeAttribute' from assembly 'System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'."
 ---> System.TypeLoadException: Could not load type 'System.ServiceModel.Web.WebInvokeAttribute' from assembly 'System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
   at System.ModuleHandle.ResolveType(QCallModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctorWithParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
   at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
   at System.Management.Automation.DotNetAdapter.MethodCacheEntry.get_IsHidden()
   at System.Management.Automation.DotNetAdapter.GetDotNetMethodImpl[T](Object obj, String methodName, MemberNamePredicate predicate)
   at System.Management.Automation.DotNetAdapter.GetDotNetMethod[T](Object obj, String methodName)
   at System.Management.Automation.Adapter.BaseGetMember[T](Object obj, String memberName)
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

Add-Type doesn't help.

Interestingly enough, the very same script runs just fine in a neighboring tab with PowerShell 5.1.


Solution

  • System.ServiceModel.Web is not supported on .NET Core. Every new release of PowerShell since 6 is built on a newer version of .NET Core, so classes from .NET Framework will not work in them. You can find out more about the differences between versions 5.1 and later here.