Manually creating a WCF Svc service with JSON support

How to create a proper WCF svc JSON service by overriding the createhost function?

Here is what I 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 is the contents of my svc file ..

<%@ ServiceHost Language="C#" Debug="true" Service="WcfJsonServiceToGetImages.Service1" CodeBehind="Service1.svc.cs" Factory="WcfJsonServiceToGetImages.Class1" %>

The interface and the implemented class contain the default code created when the new WCF svc service was created. It is completely untouched. Let me know that this custom factory service host works to host the JSON service.

Edit: I basically want to have access to the service through http: // localhost: portno / service1.svc / js

Many thanks.

+3
source share
1 answer

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

.... , -....

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;        
        }
    }
}
+2

Source: https://habr.com/ru/post/1727951/


All Articles