I have used VS2012 to create a WCF WebSite (Add->New Web Site-> WCF Service). I want to get the web site to return JSON-formatted data.
I edited the Service.cs and IService.cs classes, and edited the web.config to look as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "one", ResponseFormat = WebMessageFormat.Json)]
string One();
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "two", ResponseFormat = WebMessageFormat.Json)]
string Two();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
public class Service : IService
{
public string One()
{
return "{\"Result\":\"One\"}";
}
public string Two()
{
return "{\"Result\":\"Two\"}";
}
}
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="ZuantResearchWCFServiceWebSite.Service" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" behaviorConfiguration="restfulBehaviour" bindingConfiguration="" binding="webHttpBinding" contract="ZuantResearchWCFServiceWebSite.IService" ></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restfulBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
The Service.svc file contains one line:
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
Going to the browser, I then tap in either of the following URIs:
http://localhost:54795/Service.svc/one
http://localhost:54795/Service.svc/two
In each case I get a completely blank page.
Why isn't my website returning any JSON?
I have created a WCF Service Application with exactly the same services and configuration etc and this does return the JSON correctly to the browser.
Can anyone explain the key differences between configuring a WCF Service WebSite and a WCF Service Application, and tell me why the application returns the JSON but the website does not?
Thanks very much.