1

I'm new to WCF Web Service and don't know a lot how to debug web services...

Following examples, I created a Web service which has the interface like this:

public interface IMyService
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    List<MyClass> GetData(string param);
}

and the implementation which returns a list of MyClass.

After the deployment, I called the service in Fiddler like

http://localhost/MyService.svc/GetData?keyword=blabla

It returns:

HTTP/1.1 401 Unauthorized
Server: Microsoft-IIS/7.5
SPRequestGuid: blabla
WWW-Authenticate: blabla
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 14.0.0.6114
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Date: Mon, 12 Nov 2012 17:24:08 GMT
Content-Length: 0

and if I just write in the composer:

http://localhost/MyService.svc/GetData

without the param, it returns un empty json with status 200.

When I try the first request in Chrome, it demanded consecutively my username and password to the server, but entering them didn't help me get out of the loop(of demanding username and password).

Has anyone encountered problem like this? Or could you give any suggestion on the debugging?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
wceo
  • 934
  • 3
  • 18
  • 40
  • 1
    Please post the relevant sections of your web.config file. Correct configuration is a major part of getting a WCF service working properly, especially when you are trying to access via GET. – Jude Fisher Nov 12 '12 at 17:31
  • @JcFx I was following the example of others, curiously, there's no relevant section in web.config, as well as for lots of other services... – wceo Nov 12 '12 at 17:36
  • If you're going to access the service via GET, then you are going to need some configuration. See my answer here: http://stackoverflow.com/questions/13228995/wcf-error-405-method-not-allowed/13229373#13229373 You will at least need a service behaviour with `` – Jude Fisher Nov 12 '12 at 17:55
  • 1
    FYI, note how I edited your title to actually provide a little information to the readers. – John Saunders Nov 12 '12 at 18:32

1 Answers1

0

WCF Services don't typically allow GET access out of the box. To plagarise my own answer (here: https://stackoverflow.com/a/13229373/1014822) you need a configuration section in your web.config that looks something like this:

<bindings>
  <webHttpBinding>
    <binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehavior>
    <behavior name="restBehavior">
      <webHttp />
    </behavior>
  </endpointBehavior>
  <serviceBehavior>         
     <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="true"  />
        <serviceDebug includeExceptionDetailInFaults="true"/>
     </behavior>
  </serviceBehavior>
</behaviors>
<services>
  <service name="..." behaviorConfiguration="MyServiceBehavior">
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="crossDomain" 
              contract="..." behaviorConfigurations="restBehavior" /> 
  </service>
</services>

You may not need the cross domain elements, but you will at least need the endpoint behavior that permits <webHttp /> and the service behavior that allows <serviceMetadata httpGetEnabled="true" />. You can't then have a configuration-less service - you need the <service/> element in order to be able to bind those behaviors. You will need to complete the <service/> element with the name of your service, and the interface name of your contract (e.g. IMyService)

If you are only testing via GET because it's easier to do, but will use POST (or some protocol other than http) in your actual app, then you might consider using the WCF test client for debugging, instead of the browser and/or fiddler. Details of the client here: http://msdn.microsoft.com/en-us/library/bb552364.aspx

Community
  • 1
  • 1
Jude Fisher
  • 11,138
  • 7
  • 48
  • 91
  • Thanks very much for your detailed answer! However, after I added the bindings, the service behavior and the service in the app.`config`, things just went as before. I also tried the WCF test client, but it says "Cannot obtain Metadata". Even I tried other services which worked for Fiddler, it doesn't work here =( – wceo Nov 13 '12 at 11:06
  • Well, that's a more useful error message. See here for some causes of 'Cannot obtain Metadata': http://stackoverflow.com/questions/9114870/wcf-test-client-cannot-add-service-cannot-obtain-metadata Also, what happens if you just try to access this url: http://localhost/MyService.svc (without the method call)? – Jude Fisher Nov 13 '12 at 11:09
  • Without the method call it didn't work. I think something must be wrong with my IIS or config. However, the problem got solved with the help of my colleagues... My Class wasn't recognized by the IIS, it seems... By adding `[ServiceKnowType(typeof(MyClass))]`, as well as the [DataMember] and [DataContract] in the definition of `MyClass` it finally worked=) – wceo Nov 13 '12 at 16:42
  • Sounds like you had more than one problem - not having `ServiceKnowType` shouldn't cause an HTTP 401 (more like 500, I think). Anyway: I'm glad it's fixed. – Jude Fisher Nov 13 '12 at 16:46