.NET Framework 4.6.1 MVC项目GCS上传报MissingMethodException如何解决
.NET Framework 4.6.1调用Google Cloud Storage上传出现MissingMethodException的解决方案
异常根因
该System.MissingMethodException异常由Google API依赖包版本不兼容导致:Google.Cloud.Storage.V1调用的Google.Apis.Upload.ResumableUploadOptions的ModifySessionInitiationRequest属性在实际加载的Google.Apis包版本中不存在,要么是版本过高已移除该API,要么是版本过低未加入该API,同时.NET Framework的程序集绑定重定向机制也可能没有自动处理依赖版本冲突。
解决步骤
- 清理现有依赖
卸载项目中所有Google.开头的NuGet包,包括Google.Cloud.Storage.V1、Google.Apis、Google.Apis.Core、Google.Apis.Storage.v1、Google.Apis.Auth等相关依赖,删除项目的packages文件夹以及bin、obj目录,避免残留旧版本程序集。 - 安装兼容版本包
不要安装最新版的Google.Cloud.Storage.V1,最新版已停止对.NET Framework 4.6.1的支持,安装经过验证的兼容版本组合即可:Google.Cloud.Storage.V12.5.0版本,官方明确兼容.NET Framework 4.6.1的稳定版- 配套安装
Google.Apis1.49.0版本、Google.Apis.Core1.49.0版本,完全匹配上述Storage库的依赖要求,包含所需的ModifySessionInitiationRequest属性
- 配置程序集绑定重定向
打开项目的Web.config文件(MVC项目用Web.config),检查<runtime><assemblyBinding>节点下是否自动生成了Google相关包的绑定重定向规则,没有则手动添加:<dependentAssembly> <assemblyIdentity name="Google.Apis" publicKeyToken="4b01fa6e34db77ab" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.49.0.0" newVersion="1.49.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Google.Apis.Core" publicKeyToken="4b01fa6e34db77ab" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.49.0.0" newVersion="1.49.0.0" /> </dependentAssembly> - 运行验证
原有上传代码逻辑无问题,无需修改,重新编译项目后运行即可正常上传:using Google.Cloud.Storage.V1; using System; using System.IO; public class UploadFileSample { public void UploadFile(string bucketName = "your-unique-bucket-name", string localPath = "my-local-path/my-file-name", string objectName = "my-file-name") { var storage = StorageClient.Create(); using var fileStream = File.OpenRead(localPath); storage.UploadObject(bucketName, objectName, null, fileStream); Console.WriteLine($"Uploaded {objectName}."); } } - 额外排查项
若调整后仍出现异常,右键项目属性确认目标框架为.NET Framework 4.6.1,不要使用更高版本的.NET Framework避免引入额外兼容问题。
内容的提问来源于stack exchange,提问作者Sahil
相关产品推荐
相关产品推荐

