24

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?

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.

Basically, what do I need to implement such WCF REST concepts like WebGet, WebInvoke, UriTemplate, etc?

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!

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.

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.

user118190
  • 2,139
  • 7
  • 29
  • 45
  • When you say you referenced it in the project, did you reference System.ServiceModel or System.ServiceModel.Web? They are different and you need a project reference to System.ServiceModel.Web – alanquillin Jun 24 '09 at 17:46
  • That's the odd thing - I added both as project references first. Then I went to my code and first added a using statement to System.ServiceModel and that was recognized but System.ServiceModel.Web is not recognized. – user118190 Jun 24 '09 at 17:50
  • Have you tried just adding the using for System.ServiceModel then changing your decorator to look like [Web.WebGet(UriTemplate = "/")]? – alanquillin Jun 24 '09 at 17:53
  • I tried that, but no go... I've downloaded a sample project from the web that uses similar WebGet attributes and found that the System.ServiceModel.Web.dll is located here: C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel.Web\3.5.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll, but when I go browse for it in my other project and try to add it, it doesn't add it?!? What gives? – user118190 Jun 24 '09 at 18:00
  • Very strange, very strange. <- Yes I know that is not helpful... Can you zip up and post the source code, maybe I can help hunt down the issue. – alanquillin Jun 24 '09 at 18:11
  • FYI, I've added some UPDATES to my initial problem scope. – user118190 Jun 24 '09 at 18:27
  • Thanks for the updates, but you still haven't said what target framework you're using. Try creating a new WCF project and note the Target Framework dropdown in the Add New Project dialog. Set it to 3.5 and see if [WebGetAttribute] works. – John Saunders Jun 24 '09 at 18:44
  • John - As per my UPDATES, I noted the 3.5 Framework. I followed your instructions and created a totally new Solution/Project and did a simple WCF Service and upon load, noticed only the System.ServiceModel DLL referenced. I then went to the dfault public class Service1 : IService1 file it created for me and tried adding [WebGet] and [WebGetAttribute] and those were not recognized. What could I be missing? What do I need to download? – user118190 Jun 24 '09 at 19:46
  • I had exactly the same problem as you. I am pretty sure that you don't need to download REST Starter Kit to make it work since I haven't downloaded it. Have you tried to reinstal a fresh copy of VS? I am using Visual Web Developer edition with SP1 and "Add Reference..." helped me. Maybe you could try a free Web Developer Edition and check if it will work. – Wodzu Sep 29 '09 at 10:10

8 Answers8

32

You need to reference the 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'.

AgileJon
  • 53,070
  • 5
  • 41
  • 38
  • That's what I did first - and then I went to my code and first added a using statement to System.ServiceModel and that was recognized but System.ServiceModel.Web is not recognized. Odd right?! – user118190 Jun 24 '09 at 17:51
  • Start with a fresh project, add a reference to that dll and make sure your using statement works. If not, you're having other problems. – Tad Donaghe Jun 24 '09 at 18:01
  • Try cleaning your project and then building. Also try restarting Visual Studio – AgileJon Jun 24 '09 at 18:11
  • OK - I feel I'm getting somewhere... When go to add a reference to the project and the 'Add Reference' window comes up, under the .NET tab, I CANNOT find the System.ServiceModel.* dlls. It does look like I have them in my GAC (GAC_MSIL), but I guess they're not 'registered' properly or something. How can I make sure they come up properly in VS? – user118190 Jun 24 '09 at 18:12
  • As @Hossam and DBJDBJ pointed out, the problem is that you need to target full .NET Framework, not Client Profile. Them you will be able to add the references. – Dani bISHOP Nov 02 '11 at 11:53
25

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.

I have faced that with a WCF project not finding System.ServiceModel.Web untill I changed the default framework settings.

HTH

Hossam
  • 1,037
  • 2
  • 13
  • 17
  • 4
    This is the correct answer. You should change the project settings so the target is ".NET Framework X", not ".NET Framework X Client Profile". Then you can add the reference to your project from the .NET tab. – Dani bISHOP Nov 02 '11 at 11:49
9

This happened to me too.

I did this:

  1. Delete System.Service.Web from References
  2. Build
  3. Clean Project
  4. Add System.Service.Web to References
  5. Build

..and VS found it??

Rabskatran
  • 2,269
  • 5
  • 27
  • 41
Lee Smith
  • 6,339
  • 6
  • 27
  • 34
9

In "project properties" make sure your "target framework" is set to : .NET Framework 4

and not: .NET Framework 4 Client Profile, or any lower .NET version.

Also, if possible use VS 2010.

--DBJ

3
  1. right click on the project name and choose Properties.
  2. change the target framework to .NET Framework 4.
  3. right click on the References and choose Add Reference.
  4. And then you can see System.ServiceModel.Web.

By default the target framework is .NET Framework 4 Client Profile, so you cannot find the System.ServiceModel.Web.

John
  • 31
  • 1
2

I had the same problem.

I have added this missing reference:

System.ServiceModel.Web

and this code line:

using System.ServiceModel.Web;

and all got solved! ;)

Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
1

using System.ServiceModel.Web;

0

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:

Tools -> Options -> Resharper Ultimate -> Options -> Code Inspection-> Settings -> Enable code analysis

This restored the built-in VS code analysis, and problem was fixed straight away.

If you'd prefer to keep using ReSharper code analysis, clearing the cache in:

Tools -> Options -> Resharper Ultimate -> Options -> Environment -> General -> Clear Caches

may also sort the issue.

Breeno
  • 3,007
  • 2
  • 31
  • 30