• Decrease Text SizeIncrease Text Size

Custom WCF Client Services

Posted Date: 5/3/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size
In this example ClientServices.svc is located inside of Root/my_Pages directory to expose a WCF web service automatically that is accessible from jQuery using the example below. It is intended to create module-specific services required from the front end of the module, and the file, namespace, and classes should be placed in the module's folder and renamed accordingly. If we require global client services we may want to include this file in the WebServices folder in the development site, but this would require further review.

ClientServices.svc


<%@ servicehost language="C#" debug="true" service="Centralpoint.WebSite.my_Pages.ClientServices" factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Text;
using System.Web;

namespace Centralpoint.WebSite.WebServices
{
    [ServiceContract]
    public interface IClientServices
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        string Test(string input);
    }

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ClientServices : IClientServices
    {
        public string Test(string input)
        {
            // In some cases you may want to take a key parameter and enter a unique service key generated using Utilities > Service Key.
            //if (!Management.Application.ServiceKeyIsValid(key))
            //    throw new MethodAccessException("Centralpoint.Master.WebServices.Services.Test was accessed with an invalid key (" + key + ").");

            //System.Threading.Thread.Sleep(660 * 1000);
            return String.Format("Input = {0}, DateTime = {1}", HttpUtility.JavaScriptStringEncode(input), HttpUtility.JavaScriptStringEncode(DateTime.Now.ToString()));
        }
    }
}


In your .aspx page./strong>


<script type="text/javascript">
    	$.ajax({
    		type: 'POST',
    		url: '/my_Pages/ClientServices.svc/Test',
    		data: '{ "input": "input1" }',
    		dataType: 'json',
    		contentType: 'application/json; charset=utf-8',
    		success: function (data) {
    		    alert(eval(data).TestResult);
    		},
    		error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus + errorThrown); }
    	});
    </script>



Keywords: wcf services,wcf



Related Taxonomy

Comments:

Be the first to leave a comment.
Please Login to post comments.