C# WCF - 无法解析 [WebGet] 符号 - 我做错了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1039839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF - Cannot resolve [WebGet] symbol - what am I doing wrong?
提问by user118190
I am working on a REST WCF project and when I implement the following code, it complains that it can't resolve the WebGet class? What am I missing?
我正在处理一个 REST WCF 项目,当我实现以下代码时,它抱怨无法解析 WebGet 类?我错过了什么?
I tried importing the System.ServiceModel.Web namespace but it can't find it even though I referenced it. The "Web" in System.ServiceModel.Web does not register when I register it in a using statement on top of my code.
我尝试导入 System.ServiceModel.Web 命名空间,但即使我引用了它也找不到它。当我在代码顶部的 using 语句中注册 System.ServiceModel.Web 中的“Web”时,它不会注册。
Basically, what do I need to implement such WCF REST concepts like WebGet, WebInvoke, UriTemplate, etc?
基本上,我需要什么来实现像 WebGet、WebInvoke、UriTemplate 等这样的 WCF REST 概念?
UPDATE:After some feedback and thinking about this a little bit more what I've done, it seems that the DLLs (System.ServiceModel & System.ServiceModel.Web) do not come up via the 'Add Reference' window when I go to add a project reference. When I first started the project, FYI, since these assemblies did not come up at first, I went 'searching' for them, and copied them to a temp folder so I can reference them and thus, I guess I am having the resolve issues. So, now that I am at this point, how can I get my VS to recognize/register these WCF REST DLLs? Thanks!
更新:经过一些反馈并稍微思考一下我所做的事情后,似乎 DLL(System.ServiceModel 和 System.ServiceModel.Web)在我转到添加项目引用。当我第一次开始这个项目时,仅供参考,因为这些程序集一开始没有出现,我去“搜索”它们,并将它们复制到一个临时文件夹,以便我可以引用它们,因此,我想我有解决问题. 那么,现在我处于这一点,我怎样才能让我的 VS 识别/注册这些 WCF REST DLL?谢谢!
UPDATE:I believe I am update-to-date on everything: developing on VS 2008 SP1 - I try to download the latest SPs, downloaded the REST Preview 2 Starter Kit, developing against 3.5 Framework, trying to create a WCF REST layer to ultimately be consumed by Silverlight 2 client.
更新:我相信我在所有方面都是最新的:在 VS 2008 SP1 上开发 - 我尝试下载最新的 SP,下载 REST Preview 2 Starter Kit,针对 3.5 Framework 进行开发,尝试创建 WCF REST 层以最终由 Silverlight 2 客户端使用。
This is what I have:
这就是我所拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using UtilityClasses;
using Microsoft.ServiceModel.Web;
using Microsoft.Http;
namespace WcfRestService
{
[ServiceContract]
public interface IRestService
{
[OperationContract(Name = "Add")]
[WebGet(UriTemplate = "/")] // ** can't compile here **
int Add();
}
}
Any advice will be greatly appreciated it.
任何建议将不胜感激。
采纳答案by Lee Smith
This happened to me too.
这也发生在我身上。
I did this:
我这样做了:
- Delete System.Service.Web from References
- Build
- Clean Project
- Add System.Service.Web to References
- Build
- 从引用中删除 System.Service.Web
- 建造
- 清洁工程
- 将 System.Service.Web 添加到引用
- 建造
..and VS found it??
..VS找到了??
回答by AgileJon
You need to reference the System.ServiceModel.Web DLL.
您需要引用 System.ServiceModel.Web DLL。
Right-click the 'References' folder in your project and choose 'Add Reference...'. Scroll down to System.ServiceModel.Web and click 'OK'.
右键单击项目中的“引用”文件夹,然后选择“添加引用...”。向下滚动到 System.ServiceModel.Web 并单击“确定”。
回答by AgileJon
using System.ServiceModel.Web;
使用 System.ServiceModel.Web;
回答by Hossam
Just a one thought, you might be targeting your project to .Net Client Profile which exposes limited namespaces. you may need to check the target framework setting at your project properties.
只是一个想法,您可能将您的项目定位到 .Net Client Profile,它公开了有限的命名空间。您可能需要检查项目属性中的目标框架设置。
I have faced that with a WCF project not finding System.ServiceModel.Web untill I changed the default framework settings.
我遇到了一个 WCF 项目,直到我更改了默认框架设置才找到 System.ServiceModel.Web。
HTH
HTH
回答by Hossam
In "project properties" make sure your "target framework" is set to : .NET Framework 4
在“项目属性”中确保您的“目标框架”设置为:.NET Framework 4
and not: .NET Framework 4 Client Profile, or any lower .NET version.
而不是:.NET Framework 4 Client Profile,或任何较低的 .NET 版本。
Also, if possible use VS 2010.
此外,如果可能,请使用 VS 2010。
--DBJ
--DBJ
回答by John
- right click on the project name and choose Properties.
- change the target framework to .NET Framework 4.
- right click on the References and choose Add Reference.
- And then you can see System.ServiceModel.Web.
- 右键单击项目名称并选择属性。
- 将目标框架更改为 .NET Framework 4。
- 右键单击引用并选择添加引用。
- 然后你可以看到 System.ServiceModel.Web。
By default the target framework is .NET Framework 4 Client Profile, so you cannot find the System.ServiceModel.Web.
默认情况下,目标框架是 .NET Framework 4 Client Profile,因此您找不到 System.ServiceModel.Web。
回答by Piero Alberto
I had the same problem.
我有同样的问题。
I have added this missing reference:
我添加了这个缺失的参考:
System.ServiceModel.Web
系统.服务模型.Web
and this code line:
和这个代码行:
using System.ServiceModel.Web;
and all got solved! ;)
一切都解决了!;)
回答by Breeno
In my case my project was building despite this warning in the designer view of the service class. Not really a big issue, but still pretty annoying. Realised it was just ReSharper playing up - it hadn't updated its internal cache when the reference to System.ServiceModel
was added automatically by VS when I added a new WCF Service. I turned off real-time code analysis in:
就我而言,尽管在服务类的设计器视图中出现此警告,我的项目仍在构建。不是什么大问题,但仍然很烦人。意识到它只是在播放 ReSharper -System.ServiceModel
当我添加新的 WCF 服务时 VS 自动添加对的引用时,它没有更新其内部缓存。我关闭了实时代码分析:
Tools -> Options -> Resharper Ultimate -> Options -> Code Inspection-> Settings -> Enable code analysis
工具 -> 选项 -> Resharper Ultimate -> 选项 -> 代码检查 -> 设置 -> 启用代码分析
This restored the built-in VS code analysis, and problem was fixed straight away.
这恢复了内置的 VS 代码分析,并立即修复了问题。
If you'd prefer to keep using ReSharper code analysis, clearing the cache in:
如果您希望继续使用 ReSharper 代码分析,请清除以下缓存:
Tools -> Options -> Resharper Ultimate -> Options -> Environment -> General -> Clear Caches
工具 -> 选项 -> Resharper Ultimate -> 选项 -> 环境 -> 常规 -> 清除缓存
may also sort the issue.
也可以对问题进行排序。