JeffreySu/WeiXinMPSDK · error · ArgumentOutOfRangeException

未知的 PlatformType :

Error message

未知的 PlatformType {nameof(platformType)}:{platformType.ToString()}

What it means

GetFirstOrDefaultAppId switches on the PlatformType parameter to apply platform-specific filtering. QY/Work (and some others) are no-ops, and any other/unexpected PlatformType value reaches default and throws ArgumentOutOfRangeException naming the type and value.

Solutions

  1. Pass only PlatformType values supported by your library version (validate with Enum.IsDefined and check the version's handled set).
  2. Upgrade Senparc.Weixin if you're using a newer platform type (e.g. WeCom) added after your version.
  3. Normalize stored platform values: map legacy/unknown ints to a known member before calling.
  4. Wrap the call and fall back to enumerating bags yourself via GetAllItems() if platform filtering isn't required.

Example fix

// before
var appId = container.GetFirstOrDefaultAppId((PlatformType)record.PlatformInt);
// after
if (!Enum.IsDefined(typeof(PlatformType), record.PlatformInt))
{
    record.PlatformInt = (int)PlatformType.MP; // or reject
}
var appId = container.GetFirstOrDefaultAppId((PlatformType)record.PlatformInt);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(PlatformType), platformType))
    throw new ArgumentException($"Unknown PlatformType: {platformType}");

Type guard

bool IsKnownPlatform(PlatformType p) => Enum.IsDefined(typeof(PlatformType), p);

Try / catch

try
{
    var appId = container.GetFirstOrDefaultAppId(platformType);
}
catch (ArgumentOutOfRangeException ex)
{
    Log($"Unhandled PlatformType: {ex.Message}; falling back.");
    var appId = GetAllItems().OfType<IBaseContainerBag_AppId>().FirstOrDefault()?.AppId;
}

Prevention

When it happens

Trigger: Calling GetFirstOrDefaultAppId (directly or via container helpers that accept platformType) with a PlatformType value not covered by the switch — typically an undefined enum cast or a newly added PlatformType the installed library version doesn't handle.

Common situations: Persisting PlatformType as an int in a database/config and casting it back without validation; upgrading WeChat platform types (e.g. Enterprise WeChat -> WeCom) with an older library version that lacks the case; passing default(PlatformType) when 0 isn't a handled member.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12). Data as JSON: /api/errors/a6c6ada49afe82cd. Report an issue: GitHub.

Appendix: source

Thrown at src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainer.cs:354

        {
            string appId = null;
            switch (platformType)
            {
                case PlatformType.MP:
                    appId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId;
                    break;
                case PlatformType.Open:
                    appId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId;
                    break;
                case PlatformType.WxOpen:
                    appId = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppId;
                    break;
                case PlatformType.QY:
                    break;
                case PlatformType.Work:
                    break;
                default:
                    throw new ArgumentOutOfRangeException($"未知的 PlatformType {nameof(platformType)}:{platformType.ToString()}");
            }

            if (appId == null)
            {
                var firstBag = GetAllItems().FirstOrDefault() as IBaseContainerBag_AppId;
                appId = firstBag == null ? null : firstBag.AppId;
            }

            return appId;
        }

        ///// <summary>
        ///// 获取完整的数据集合的列表,包括所有的Container数据在内(建议不要进行任何修改操作)
        ///// </summary>
        ///// <returns></returns>
        //public static IDictionary<string, IContainerItemCollection> GetCollectionList()
        //{
        //    return CollectionList;

View on GitHub (pinned to be573f6f94)