【问题标题】:WCF REST Webinvoke not found未找到 WCF REST Webinvoke
【发布时间】:2026-01-06 02:35:01
【问题描述】:

以下是我在测试应用中使用的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.IO;

namespace MyWCFServices
{
    [ServiceContract]
    interface IHelloWorldService
    {
        [OperationContract]
        String GetMessage(String name);

        //[OperationContract]
        //[WebInvoke(Method = "PUT",UriTemplate = "File/{fileName}")]
        //[WebContentType("application/octet-stream")]
        // bool UploadFile(string fileName, Stream fileContents); 
        [OperationContract]
        [WebInvoke(UriTemplate = "UploadFile/{fileName}")]
        void UploadFile(string fileName, Stream fileContent); 
    }
}

它在编译 webinvoke 时出现错误。有相同的想法吗??

【问题讨论】:

    标签: .net wcf rest .net-4.0 webinvoke


    【解决方案1】:

    WebInvokeAttribute 位于单独的程序集 System.ServiceModel.Web.dll 中。你引用了那个程序集吗?另外你必须添加using System.ServiceModel.Web;

    编辑:

    要使用 System.ServiceModel.Web.dll 程序集,您必须至少使用 .NET 3.5,并且不能使用 .NET 4.0 客户端配置文件。

    【讨论】:

    • 您使用的是什么版本的 .NET/WCF?
    • 我使用的是 .NET 4.0(不是 4.0 客户端配置文件)
    • 我从未听说过WebContentType 属性,而且我知道它不是 WCF 的一部分。
    • 你用的是什么类型的项目?
    • @Ladislav :我面临同样的问题。此外,我在添加引用对话框中看不到对 System.ServiceModel.Web 的引用。我正在使用 Visual Studio 2010 (.net 4.0)。如果我遗漏了什么,你能帮我吗??