将WCF服务从.NET Framework迁移至.NET 7遇编译错误求助
WCF服务迁移至.NET 7后编译错误:WebInvoke找不到
问题背景
将WCF服务从.NET Framework迁移至.NET 7,已通过微软升级助手完成迁移操作,但出现编译错误。
相关代码
[WebInvoke(Method = "POST", UriTemplate = "UploadFotoAndroid")] public string UploadPhotoAndroid(Stream fileContents) { var sFileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + "foto.png"; EscreverNoLog("Recebendo arquivo Android - Foto"); try { string absFileName = string.Format("{0}\\FileUpload\\{1}" , AppDomain.CurrentDomain.BaseDirectory , sFileName); EscreverNoLog("Nome do Arquivo Android - Foto: " + absFileName); using (FileStream fs = new FileStream(absFileName, FileMode.Create)) { fileContents.CopyTo(fs); fileContents.Close(); } /* try { GerenciaThreadCopia thCopia = new GerenciaThreadCopia(); thCopia.ChamarThreadCopiaFoto(absFileName); } catch (Exception) { } */ return "$$$" + sFileName + "$$$"; } catch (Exception ex) { EscreverNoLog("Erro recebimento do Arquivo Android - Foto: " + sFileName); EscreverNoLog("Mensagem de Erro recebimento do Arquivo Android - Foto: " + ex.Message); return "FALHA: " + ex.Message; } }
编译错误
The Type or namespace name 'WebInvoke' could not be found (are you missing a using directive or an assembly reference?)
The Type or namespace name 'Method' could not be found (are you missing a using directive or an assembly reference?)
解决方法
安装兼容NuGet包
在项目中安装System.ServiceModel.Web包,这是.NET 7中支持WCF Web特性的官方兼容包:Install-Package System.ServiceModel.Web也可通过NuGet包管理器搜索安装。
添加命名空间引用
在代码文件顶部补充using指令:using System.ServiceModel.Web;可选:替换过时API
代码中AppDomain.CurrentDomain.BaseDirectory在.NET 7已标记为过时,建议替换为AppContext.BaseDirectory:string absFileName = string.Format("{0}\\FileUpload\\{1}" , AppContext.BaseDirectory , sFileName);
说明
.NET Framework中WCF的Web相关特性(如WebInvoke)属于System.ServiceModel.Web命名空间,但.NET 7默认不包含该组件,必须手动引入对应NuGet包才能正常使用。
内容的提问来源于stack exchange,提问作者Kamlesh Jangley
相关产品推荐
相关产品推荐

