2

I've been trying to create a simple WCF RESTful web service, but it seems to work only in SOAP mode. I'm hosting my service with the local IIS.

It looks really standard:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    Guid Login(string username, string password);
...

and:

public class MyService : IMyService
{
    [WebGet(UriTemplate = "login?user={user}&password={password}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    public Guid Login(string username, string password)
    {
        return Guid.Empty;
    }

I also included the behavior in the config file and was using it in:

<endpoint address="" binding="webHttpBinding" contract="MyService" behaviorConfiguration="webHttp"/>

according to all of the examples I know...

Now the thing is when invoking the login from a stub client which uses ServiceReference, it looks just fine in Fiddler, but it is SOAPy. From some reason I cannot invoke my service in a RESTy way, even the /help seems to return a 400 Bad Request. (I'm invoking http://localhost:8080/MyService.svc/help or /login etc.)

What is preventing the REST to take action? Thanks in advance :)

Edit: I found an answer.

It turns out one must define Routings...

After adding this to Global.asax :

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("MyService",
           new WebServiceHostFactory(), typeof(MyService)));
    }

It went through just fine.

Plus I earned that the ".svc" is now not part of the URL.

Jacob
  • 133
  • 1
  • 1
  • 10
  • As a general note, logging in via GET is considered to be Bad Practice as it is conceptually a state change. Use POST (over HTTPS for preference). – Donal Fellows Jan 09 '12 at 13:38
  • To invoke a REST service you should not do a Add Service Reference in the client side. You need to use HttpWebRequest class to invoke it the RESTful way. – Rajesh Jan 12 '12 at 14:52

2 Answers2

1

I had the same issue the reason was my posting xml had the below on top

<?xml version="1.0" encoding="utf-16"?>

I removed it and it just worked!

The Light
  • 26,341
  • 62
  • 176
  • 258
1

Your code looks good to me. Can you try couple of things

[OperationContract]
[WebGet(UriTemplate = "/Login/{username}/{password}", ResponseFormat = WebMessageFormat.Xml)]
Guid Login(string username, string password);

At the same time please remove the WebGet attribute from MyService.Login function.

-OR-

Put this block in your web.config under system.web

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

Hope this helps.

Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
  • Still nothing :( All I get is: `HTTP/1.1 400 Bad Request Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET Date: Mon, 09 Jan 2012 12:22:22 GMT Content-Length: 0` – Jacob Jan 09 '12 at 12:22
  • Try this and one behavior – Amar Palsapure Jan 09 '12 at 12:28
  • I'm using this exact configuration! (only the name is different, instead of "RestFriendly" I'm using "webHttp" - but I guess it does not matter, just like a variable name...) – Jacob Jan 09 '12 at 12:34
  • Can you browse the URL, instead of calling it from Service Reference, as this RESTful, you should be able to see GUID in response. In my project I had returned _Stream_ from my REST service. You can check IIS logs too. – Amar Palsapure Jan 09 '12 at 12:37
  • Browsing to the URL with chrome gives a blank page, when using Fiddler Compose I can see that the blank page is actually a HTTP 400 Bad Request response with no body. I will check now the IIS logs, that's a wonderful idea. – Jacob Jan 09 '12 at 12:52
  • Well, those logs were not helpful. They just say there was an 400 error and that's about it... – Jacob Jan 09 '12 at 12:58
  • Hmmm... open the svc from Visual Studio, it will get hosted in Web Server at some port, browse this url, and try your login there. Also attach debugger to this web server, so that you might some debug point in your app. Or try to host this service in may be console app and try. – Amar Palsapure Jan 09 '12 at 13:03