You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将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?)

解决方法

  1. 安装兼容NuGet包
    在项目中安装System.ServiceModel.Web包,这是.NET 7中支持WCF Web特性的官方兼容包:

    Install-Package System.ServiceModel.Web
    

    也可通过NuGet包管理器搜索安装。

  2. 添加命名空间引用
    在代码文件顶部补充using指令:

    using System.ServiceModel.Web;
    
  3. 可选:替换过时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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:47:22