Can't call WCF service from jQuery - wcf

Thou there a dozen posts that cover how to call WCF methods from jQuery, I can't make it work. I have simple WCF service application
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);
}
This is the implementation
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
And this is the web.config of my service
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="TestWebApp.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="TestWebApp.Service1AspNetAjaxBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="jQueryToWCF.Service1">
<endpoint address=""
behaviorConfiguration="TestWebApp.Service1AspNetAjaxBehavior"
binding="webHttpBinding"
contract="jQueryToWCF.IService1" />
</service>
</services>
Now I'm trying to call this from jQuery ( from html page )
$(document).ready(function () {
var param = "{value: 'Hello World!'}";
$.ajax({
type: "GET",
url: "http://localhost:5555/Service1.svc/GetData",
data: param,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result.d);
}
});
});
But no call even makes to Service. I checked it up by Fiddler. But when I'm puting the url into browser, I can get the response. Can anybody help me to figure this out ?

I've been struggling off and on with this for awhile.
The page that finally got me moving was this one: http://forums.asp.net/t/1765610.aspx/1
Meanwhile: Here is a functional project file that should work. You may need to replace the url in the javascript to be the correct port #.
http://submissiv.com/share/playground.wcf.service.zip

Related

WCF Service receives null request

var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(mi));
var req = HttpWebRequest.Create("http://localhost/Service1.svc/json/MethodName");
req.ContentType = "application/json";
req.ContentLength = dataToSend.Length;
req.Method = "POST";
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
var response = req.GetResponse();
Here "/json" is my endpoint address and my service is configured with multiple endpoints. As per image here, request i sent is recieving null at server.
If my request format is not proper then suggest proper way to call this service.
// Service inter face
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method="POST")]
Response MethodName(Request request);
}
// Service1
public class Service1 : IService
{
public Response MethodName(Request request)
{
some logical operation....
}
}
// End point configuration (Web config)
<endpoint address="json" behaviorConfiguration="jsonBehavior"
binding="webHttpBinding" bindingConfiguration="webHttpBindingJson"
name="jsonn" contract="Service1.IService" />
<endpoint address="xml" behaviorConfiguration="poxBehavior" binding="webHttpBinding"
bindingConfiguration="webHttpBindingXml" name="xmll" contract="Service1.IService" />
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
<behavior name="poxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<webHttpBinding>
<binding name="webHttpBindingJson">
<security mode="None" />
</binding>
<binding name="webHttpBindingXml">
<security mode="None" />
</binding>
</webHttpBinding>
// Request class
[DataContract]
public class Request
{
string userMobile;
string otp;
[DataMember]
public string UserMobile
{
get { return userMobile; }
set { userMobile = value; }
}
[DataMember]
public string OTP
{
get { return otp; }
set { otp = value; }
}
}
Finally i found for this.
I modified endpoint of json behaviour configuration to this,
<behavior name="jsonBehavior">
<webHttp defaultBodyStyle ="Bare"/>
<!--<enableWebScript />-->
</behavior>
and removed enableWebScript. Finally my code working.

WCF jquery parsererror unterminated string constant response

For my recent project, I created a Web service that returns an array of custom type to jquery client-side code. WCF is called by $.ajax command and is in the same domain.
When I run my web applicaiton on localhost (which is IIS run on local machine), everything works fine. When I deploy it to our integration server, suddenly ajax call to WCF ends with an error: "parsererror - unterminated string constant" and status of 200. Returned message is however something like "[{\"Text\":\"Test dodatnih naslov", which of course is not a correct json format.
Correct response should have been: "[{"Text":"Test dodatnih naslovov","Value":"100"},{"Text":"Test dodatnih naslovov - ISO2","Value":"101"},{"Text":"UPDATE","Value":"102"}]"
I have traced WCf service for malfuncitons, but it does not seem to be crashing. I also tried and set timeout to ajax call, but to no avail. Some help would be much appreciated.
My IIS is IIS7, where integration runs IIS6 on Windows Server 2008.
js file
function InsuranceClientContact_ItemsRequesting(o, e) {
var $ = $telerik.$;
var urlSvc = ServiceBaseUrl + '/GetContacts'
$.ajax({
type: "POST",
url: urlSvc,
data: '{"ixClient": ' + selectedItemId + '}', //selectedItemId is a positive number
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
// do something
},
error: function (result) {
var msg = result.status + " - " + result.statusText;
setTimeout(function () { throw new Error(msg) }, 0);
}
});
wcf interface
namespace Sid.Skode.Web.Services.Populate {
[ServiceContract]
public interface IInsuranceClientContactService {
[OperationContract]
[WebInvoke(Method="POST",
BodyStyle=WebMessageBodyStyle.WrappedRequest,
ResponseFormat=WebMessageFormat.Json)]
Contact[] GetContacts(long ixClient);
}
[DataContract]
public class Contact {
[DataMember]
public string Text;
[DataMember]
public string Value;
}
}
wcf service implementation
namespace Sid.Skode.Web.Services.Populate {
[AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed )]
public class InsuranceClientContactService : IInsuranceClientContactService {
public Contact[] GetContacts( long ixClient ) {
return GetContactsFromDatabase( ixClient );
}
#region Private methods
private Contact[] GetContactsFromDatabase( long ixClient ) {
DataTable dt = GetDataFromDataBaseById( ixClient );
return ConvertDataTableToContactArray( dt );
}
private DataTable GetDataFromDataBaseById( long ixClient ) {
AutoCompleteBLL model = new AutoCompleteBLL();
return model.SearchContactsByPartner( ixClient );
}
private Contact[] ConvertDataTableToContactArray( DataTable dt ) {
Contact[] rgContact = new Contact[dt.Rows.Count];
int cnContact = 0;
foreach (DataRow dr in dt.Rows) {
if (!dr.IsNull( "NAZIV" )) {
Contact contact = new Contact();
contact.Text = dr["NAZIV"].ToString();
contact.Value = dr["ID_DODATEN_KONTAKT"].ToString();
rgContact[cnContact++] = contact;
}
}
return rgContact;
}
#endregion
}
}
web.config wcf part
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="httpServiceBehavior">
<serviceMetadata httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="httpEndpointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithTransportWindowsSecurity">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true" />
<services>
<service name="Sid.Skode.Web.Services.Populate.InsuranceClientContactService" behaviorConfiguration="httpServiceBehavior">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithTransportWindowsSecurity"
contract="Sid.Skode.Web.Services.Populate.IInsuranceClientContactService"
behaviorConfiguration="httpEndpointBehavior">
</endpoint>
<endpoint
address="mex"
binding="mexHttpsBinding"
bindingConfiguration=""
contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
As described here, you need remove all instances of RadCompression http module from web config. Then, it works.

How to Make Multiple Parallel WCF Service Calls from jquery

I have to make multiple WCF service calls by using jquery to update different areas of the web page after a specified time interval. If I make a single call to the WCF service it works fine, but when there are two or more parallel calls to the WCF service no response is received.
All the service calls are made after some specified time-interval. Following is the code of one of them:
var type = "POST";
var contentType = "application/json; charset=utf-8";
var dataType = "json";
var processData = true;
function LoadData()
{
var url = serviceURL;
var data = '{}';
CallService(url, data, LoadDataSuccess);
}
function LoadDataSuccess(result)
{
if (dataType == "json")
{
//other code...
setTimeout("LoadData()", 5000);
}
}
function CallService(url, data, SuccessMethod)
{
$.ajax({
type: type, //GET or POST or PUT or DELETE verb
url: url, // Location of the service
data: data, //Data sent to server
contentType: contentType, // content type sent to server
dataType: dataType, //Expected data format from server
processdata: processData, //True or False
success: function (msg)
{
SuccessMethod(msg);
},
error: ServiceFailed
});
}
function ServiceFailed(result)
{
alert('Service call failed: ' + result.status + '' + result.statusText);
type = null; contentType = null; dataType = null; processData = null;
}
And my wcf service looks like this:
public interface ITestService
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<Data> GetData();
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<Data> GetUser();
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
List<Data> GetCustomer();
}
And the configuration looks like this:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<behaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior" name="TestService">
<endpoint address="" binding="webHttpBinding" contract="ITestService" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
</system.serviceModel>

mismatch between server and client

I have a WCF rest service. I created it using 4.0 rest service application, so it is SVC-less.
I have this service contract:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{
[WebGet(UriTemplate = "/Login/?username={username}&password={password}", ResponseFormat= WebMessageFormat.Json)]
public Response Login(string username, string password)
{
Response res;
BillboardsDataContext db = new BillboardsDataContext();
var q = from lgin in db.logins
where lgin.username == username && lgin.password == password
select lgin;
if (q.Count() != 0)
{
res = new Response(true, "Login successful");
return res;
}
else
{
res = new Response(false, "Login failed!");
return res;
}
}
[WebInvoke(UriTemplate = "", Method = "POST")]
public void Upload(Stream fileStream)
{
FileStream targetStream = null;
string uploadFolder = #"C:\inetpub\wwwroot\Upload\test.jpg";
using (targetStream = new FileStream(uploadFolder, FileMode.Create,
FileAccess.Write, FileShare.None))
{
const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = fileStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
fileStream.Close();
}
}
}
and this web.config:
<services>
<service name="BillboardServices.Service1" behaviorConfiguration="Meta">
<endpoint name="restful" address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="BillboardServices.Service1" />
<endpoint name="streamFile" address="/Upload" binding="basicHttpBinding" bindingConfiguration="streamBinding" contract="BillboardServices.Service1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Meta">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="streamBinding" maxReceivedMessageSize="64000" maxBufferSize="64000" transferMode="Streamed" messageEncoding="Mtom">
<readerQuotas maxDepth="64000" maxStringContentLength="64000" maxArrayLength="64000" maxBytesPerRead="64000" maxNameTableCharCount="64000"/>
</binding>
</basicHttpBinding>
</bindings>
The login service works very well, but I am having an issue with the Upload action. I call it through an Android app via http://www.myhost.com/Upload and I get this error:
Content Type multipart/form-data; boundary=wjtUI0EFrpQhBPtGne9le5_-yMxPZ_sxZJUrFf- was sent to a service expecting multipart/related; type="application/xop+xml". The client and service bindings may be mismatched.
I can't find info on this error. Anybody seen this before?
Thank you!
So it turns out that I needed to use webHttpBinding for both endpoints, not just the login.

Sending JSON to WCF Rest Service - object is always null

I am trying to get my application working by using REST, WCF and JSON (new to all those technologies). I have the 'GET' working fine. It is the 'POST' that is causing me problems.
As you will see below I 'pack up' my JSON using JSON.stringify, then fire off the POST to the REST resource. However, when the object gets to the WCF method that is handling the request the object is always null.
Here is the code:
$.ajax({
type: "POST",
dataType: "json",
url: "Services/ContactCompanyService.svc/contactcompanies/customers",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ contactcompany: newCustomer }),
success: function (html) { alert(html); }
});
Here is the config stuff:
<services>
<service behaviorConfiguration="ServiceBehaviour" name="ContactCompanyService">
<endpoint address="contactcompanies" behaviorConfiguration="web" binding="webHttpBinding" contract="IContactCompanyService"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
Here is the contract:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "customers")]
[return: MessageParameter(Name = "ContactCompany")]
ContactCompany AddContactCompany(ContactCompany ContactCompanyObject);
And it is the method that implements the above interface where ContactCompanyObject is null.
What on earth am I doing wrong? Please don't rule out stupidity on my part.
Further:
I changed the WebMessageBodyStyle to .Bare, and this resulted in the object not being null ... but EVERY property of the object being null. That said, wrapped is the way I would like to go.
I would be grateful of any assistance. Let me know if you need further information.
UPDATE
I started from scratch with a completely new project - stripped back.
I am getting exactly the same result - the object, when received by the WCF code, is null.
Here's what I did on this new test project.
WCF Contract:
(under the namespace: NullTestService
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "NullTestPost")]
[return: MessageParameter(Name = "NullTestType")]
NullTestType GettMethod();
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "NullTestPost")]
[return: MessageParameter(Name = "NullTestType")]
NullTestType PostMethod(NullTestType NullTestTypeObject);
}
[DataContract]
public class NullTestType
{
[DataMember]
public string NullTestString { get; set; }
[DataMember]
public int NullTestInt { get; set; }
}
Service Implementation:
(same namespace)
public class Service1 : IService1
{
public NullTestType PostMethod(NullTestType NullTestTypeObject)
{
return NullTestTypeObject;
}
public NullTestType GettMethod()
{
return new NullTestType { NullTestString = "Returned String", NullTestInt = 25 };
}
}
Website project.
Service.svc:
<%# ServiceHost Service="NullTestService.Service1" %>
web.config in the web project:
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="NullTestService.Service1">
<endpoint address="nulltestaddress" behaviorConfiguration="web" binding="webHttpBinding" contract="NullTestService.IService1"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
and finally the jQuery in the web project:
$(function () {
// $.ajax({
// type: "GET",
// url: "http://localhost:8080/TestWeb/Service.svc/nulltestaddress/nulltestpost",
// success: alertResult
// });
alert('about to do it');
$.ajax({
type: "POST",
url: "http://localhost:8080/TestWeb/Service.svc/nulltestaddress/nulltestpost",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: '{"NullTestType":{"NullTestString":"This is a post string","NullTestInt":25}}',
success: alertResult
});
});
function alertResult(data) {
alert(data.NullTestType.NullTestString);
}
So. The (commented out) GET works fine and returns the JSON. The POST does not. On the line:
public NullTestType PostMethod(NullTestType NullTestTypeObject)
{
return NullTestTypeObject;
}
(the 'return' line) the NullTestTypeObject is always NULL.
I would be very grateful for help. I have lost a lot of time over this.
If Wrapped is what you want to do, then you need to wrap the request in the operation parameter name:
var input = { "ContactCompanyObject" : newCustomer };
$.ajax({
data: input
...
});
Or for the second example, if you change the ajax call to the one shown below, you should get the expected result:
var input = { NullTestTypeObject: { NullTestString: "Hello", NullTestInt: 123} };
alert("Input: " + JSON.stringify(input));
$.ajax({
type: "POST",
url: "./Service1.svc/nulltestaddress/NullTestPost",
contentType: "application/json",
data: JSON.stringify(input),
success: function (result) {
alert("POST result: " + JSON.stringify(result));
}
});