Here is a complete working example of a WCF service hosted in IIS:
[ServiceContract] public interface ICalculators { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )] float Weight(float width, float diameter, float size, float factor); } public class Calculators : ICalculators { public float Weight(float width, float diameter, float size, float factor) { return 10f; } }
calculators.svc :
<%@ ServiceHost Language="C#" Debug="true" Service="XXX.SharePoint.WebServices.Calculators" Factory="System.ServiceModel.Activation.WebServiceHostFactory" CodeBehind="Calculators.svc.cs" %>
web.config:
<system.serviceModel> <services> <service behaviorConfiguration="XXX.SharePoint.WebServices.CustomServiceBehaviour" name="XXX.SharePoint.WebServices.Calculators"> <endpoint address="" binding="webHttpBinding" contract="XXX.SharePoint.WebServices.ICalculators" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="XXX.SharePoint.WebServices.CustomServiceBehaviour"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
Consumption using jQuery in the same ASP.NET application:
$.ajax({ url: '/calculators.svc/Weight', type: 'POST', contentType: 'application/json', data: JSON.stringify({ Width: 1.2, Diameter: 2.3, Size: 3.4, Factor: 4.5 }), success: function (result) { alert(result.WeightResult); } });
Note the use of webHttpBinding instead of webHttpBinding (SOAP) in web.config, as well as the special WebServiceHostFactory used in the .svc file.
source share