JQuery with WCF

I am trying to get my jquery code to work with WCF. I start with something very simple. WCF is hosted in IIS. It has a simple interface and simple implementation. Below is my WCF code.

[ServiceContract] public interface IEvalService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "GetId", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string GetId(); } 

The implementation of the above interface is as follows:

 [AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed)] public class EvalService: IEvalService { public string GetId() { BenchMarkStatus benchMarkStatus = new BenchMarkStatus(); return benchMarkstatus.Name; } } 

Of course, there is a benchMark class as follows:

 [DataContract] public class BenchMarkStatus { private string _name; public BenchMarkStatus() { _name = "Sudarshan"; } [DataMember] public string Name { get { return _name; } set { _name = value; } } } 

The configuration file for the Webservice is as follows:

 <services> <service name="BenchMarkServiceLiabrary.EvalService"> <endpoint address="10.66.26.171" binding="webHttpBinding" contract="BenchMarkServiceLiabrary.IEvalService" behaviorConfiguration="WEB" /> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="WEB"> <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 

And on the client side, I have the following code:

 $.ajax({ type: 'GET', url: "http://10.66.26.171/EvalService/Eval.svc/GetId?callback=?", contentType: "json/application; charset=utf-8", dataType: "jsonp", jsonpCallback: 'jsonCallback', success: function (data) { alert(data) } }); function jsonCallback() { alert("me"); } 

I get error 404. Initially, I had problems with Cross domain stuff ... but now, using the client code, you cannot access the GetId function. Can anybody help?

+4
source share
2 answers

The problem is the address part of the service in web.config

Since you host in iis, your service will be available in:

http://localhost/EvalService/Eval.svc (localhost if your iis is running locally or if not, ip or the address of the remote iis)

This will be the endpoint address of your service if you had not added the address part to web.config, and with the added part of the address you can find this endpoint at http://localhost/EvalService/Eval.svc/10.66.26.171

If you have only one endpoint, you can safely omit or simply specify the address = "" in your service configuration and access your service at this address http://localhost/EvalService/Eval.svc :

  <service name="BenchMarkServiceLiabrary.EvalService"> <endpoint address="" binding="webHttpBinding" contract="BenchMarkServiceLiabrary.IEvalService" behaviorConfiguration="WEB" /> </service> 
0
source

StackOverflow offers some old questions; I really hope you have switched to WebAPI by now, it is much easier. Every time I tried to set up the wcf service, it was a fight. But regarding the Question; I had a very similar question a while ago, and adding to EndpointBehaviors resolved it for me.

Try updating endPointBehaviors to the following

 <endpointBehaviors> <behavior name="WEB"> <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/> <enableWebScript /> </behavior> </endpointBehaviors> 
-1
source

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


All Articles