Creating a JSON enabled WCF Svc service manually

c#, json, wcf

Solution

Found the solution... I had to use WebScriptEnablingBehavior instead of WebHttpBehavior.

Here's the working code....Hope it saves time for someone....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Activation;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

namespace WcfJsonServiceToGetImages
{
    public class Class1 : WebScriptServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {    
            ServiceHost host = new ServiceHost(typeof(Service1),baseAddresses);
           foreach(Uri uri in baseAddresses)
            {       
          WebHttpBinding webbinding=new WebHttpBinding(WebHttpSecurityMode.None);
            webbinding.AllowCookies=true;
            webbinding.CrossDomainScriptAccessEnabled=true;
            EndpointAddress ea=new EndpointAddress(uri);           
            WebScriptEnablingBehavior behavior = new WebScriptEnablingBehavior();
            behavior.DefaultOutgoingResponseFormat = WebMessageFormat.Json;
           // behavior.DefaultBodyStyle = WebMessageBodyStyle.WrappedRequest;      



            behavior.DefaultOutgoingRequestFormat = WebMessageFormat.Json;
           ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService1), webbinding, uri);
           endpoint.Behaviors.Add(behavior);         
          }            
            return host;        
        }
    }
}

Problem

How do I create a proper JSON enabled WCF svc service by overriding the createhost function? Here's what I've tried... ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ServiceModel.Activation; using System.ServiceModel; using System.ServiceModel.Description; using System.ServiceModel.Web; namespace WcfJsonServiceToGetImages { public class Class1 : WebScriptServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { ServiceHost host = new ServiceHost(typeof(Service1),baseAddresses); foreach(Uri uri in baseAddresses) { WebHttpBinding webbinding=new WebHttpBinding(WebHttpSecurityMode.None); webbinding.AllowCookies=true; webbinding.CrossDomainScriptAccessEnabled=true; EndpointAddress ea=new EndpointAddress(uri); WebHttpBehavior behavior = new WebHttpBehavior(); behavior.DefaultOutgoingResponseFormat = WebMessageFormat.Json; behavior.DefaultBodyStyle = WebMessageBodyStyle.Wrapped; behavior.HelpEnabled = true; behavior.DefaultOutgoingRequestFormat = WebMessageFormat.Json; ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService1), webbinding, uri); endpoint.Behaviors.Add(behavior); } return host; } } } ``` Here's the content of my svc file.. ``` <%@ ServiceHost Language="C#" Debug="true" Service="WcfJsonServiceToGetImages.Service1" CodeBehind="Service1.svc.cs" Factory="WcfJsonServiceToGetImages.Class1" %> ``` The interface and implemented class contain default code generated while creating a new WCF svc service.It's totally untouched. Please guide me on getting this custom service host factory to work to host a JSON service. Edit:I basically want to be able to access the service through http://localhost:portno/service1.svc/js Thanks so much.

Original source