Valentin P.
Valentin P.

Reputation: 1151

WCF Service returns json

I want my WCF service to return json.

I test it on ASP.NET Development Server and it works from WCF Test Client (method invokation is ok).

But testing from browser fails with error 400 bad request.

web.config:

<configuration>
   <system.web>
      <compilation debug="true" targetFramework="4.0" />
   </system.web>
   <system.serviceModel>
      <services>
         <service name="WebServiceExample" 
                  behaviorConfiguration="MetadataBehaviour">
            <endpoint 
                address="" 
                behaviorConfiguration="AjaxBehaviour" 
                binding="webHttpBinding" 
                contract="WebService.IWebServiceExample">
               <identity>
                  <dns value="localhost" />
               </identity>
             </endpoint>
          </service>
      </services>
      <behaviors>
          <serviceBehaviors>
             <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
             </behavior>
             <behavior name="MetadataBehaviour">
                 <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
             </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
             <behavior name="AjaxBehaviour">
                 <webHttp/>
             </behavior>
          </endpointBehaviors>
       </behaviors>
       <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
   </system.serviceModel>
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>
</configuration>

Contract:

[ServiceContract]
public interface IWebServiceExample
{        

    [OperationContract]
    [WebGet(UriTemplate = "/check/{key}",
       ResponseFormat = WebMessageFormat.Json)]
    DataElement Check(string key);
}


[DataContract]
public class DataElement
{

    [DataMember]
    public string Key { get; set; }

    [DataMember]
    public DateTime DateSince { get; set; }

    [DataMember]
    public DateTime DateTill { get; set; }       
}

SVC Markup:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="WebService.WebServiceExample" 
    CodeBehind="WebServiceExample.svc.cs" %>

the URI I try is:

http://localhost:16679/WebServiceExample.svc/check/asd

Upvotes: 1

Views: 2137

Answers (1)

Valentin P.
Valentin P.

Reputation: 1151

Uhh. I've got it. I think it will be useful for others if I tell how I went through it.

At first there are differences between WCF service applciation and WCF service libraries. I consider it may lead to conflicts if set full address to endpoint in service application, because of it is set by server (IIS or ASP.NET Development).

Second, my error was in missing namespace in service name attribute. I set name="WebServiceExample" instead of name="WebService.WebServiceExample".

I am newbie in WCF and it is not obviously for me that name attribute means service full class name. And error texts doesn't indicate to it.

I came to it after reading this topic WCF REST Service returns HTTP 400 Bad Request

The way to come around is to define routings in Global.asax (perverted way, but it works).

Third, I thought that it is wrong that my service allowed me to view wsdl page. It is strange that RESTful WCF Service generates wsdl, and it was looking like mistake. But know I can tell that it is normal behaviour.

If in config set up something like this

then your RESTful service will allow to show

WSDL on localhost/Service.svc?wsdl

and

REST functions on localohost/Service.svc/help

At last, some usefull links: http://www.codeproject.com/Articles/167159/How-to-create-a-JSON-WCF-RESTful-Service-in-60-sec --- WCF service library. I need start VS as admin to run this.

http://robbincremers.me/2012/01/05/wcf-rest-service-with-xml-json-response-format-according-to-content-type-header/ --- WCF service application

Upvotes: 1

Related Questions