I have a problem with calling a simple method in WCF service. I am stuck and I do not know how to solve this problem. I will appreciate any help.
My WCF service:
[ServiceContract]
interface IMyService
{
[OperationContract]
string GetSomething();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class MyService : IMyService
{
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetSomething",
BodyStyle = WebMessageBodyStyle.Bare)]
public string GetSomething()
{
return "Hello";
}
}
Starting the service:
using (ServiceHost host = new ServiceHost(typeof(MyService)))
{
host.Open(); // end point specified in app config
Console.WriteLine("Hit Enter to quit.");
Console.ReadLine();
}
App.Config file
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<services>
<service name="testWCF.MyService">
<endpoint address="http://localhost:8003/myservice"
binding="webHttpBinding"
contract="testWCF.IMyService"
behaviorConfiguration="webHttp"/>
</service>
</services>
</system.serviceModel>
</configuration>
Thats all about WCF service. My web application is running on http://127.0.0.1:8085
and here is how I am sending jQuery request from web application:
$.ajax({
url: "http://127.0.0.1:8003/myservice/GetSomething?callback=?",
dataType: 'jsonp',
cache: false,
beforeSend: function () {
console.log("Loading");
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
},
success: function (data) {
console.log('Success');
console.log(data);
},
complete: function () {
console.log('Finished all tasks');
}
});
My response is following: I can see in my JavaScript concole of my chrome, that response has been send from WCF service (content of GetSomething method is "Hello"), but I am getting following console output:
Loading
GetSomething:-1Resource interpreted as Script but transferred with MIME type application/json.
Object
parsererror
Error: jQuery1101030437586596235633_1390485791492 was not called
My succes function has never been executed. As I followed some of posts similar to this I undestood that it have something to do with Content-Type, but I could not find way how to get this work. Can anybody help me?
I have resolved it by myself. Problem was, in App.config file. i have added bindingConfiguration="webHttpBinding"
Now my App.config file is:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBinding" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<services>
<service name="testWCF.MyService">
<endpoint address="http://localhost:8003/myservice"
binding="webHttpBinding"
bindingConfiguration="webHttpBinding"
contract="testWCF.IMyService"
behaviorConfiguration="webHttp"/>
</service>
</services>
</system.serviceModel>
</configuration>