Reputation: 645
This has been driving me nuts today. Hopefully someone here can help me crack it.
Basically, I have created a test site with a WCF service to be consumed by jQuery. It has three methods, two of which work perfectly.
The service returns JSON. It has three methods, two of which work perfectly. The third does not - and I cannot see why.
This is what I do know.
If I launch the site from Visual Studio, I can browse to the service and view the formatted JSON. If I try to do the same in a browser i.e. the IIS version, I get a 400 error. But only for the one method - the others are fine.
It's that inconsistency that has me stumped.
I have set it up as a website in IIS, rather than a virtual directory. To browse to it, the URL is of the form http://mytestsite.local
Having followed a walk-through (it has been a while since I have done any WCF work!), I took the approach of removing service-related configuration info from web.config. Instead, I have a Factory attribute that references the class WebServiceHostFactory in the .svc file.
Here are the methods:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public UserDTO[] GetUsers()
{
using (var dbContext = new GSChallengeContext())
{
var results = from u in dbContext.Users
select new UserDTO()
{
UserId = u.UserId,
Forename = u.Forename,
Surname = u.Surname,
};
return results.ToArray();
}
}
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public UserDTO[] GetUserById(int userId)
{
using (var dbContext = new GSChallengeContext())
{
var results = from u in dbContext.Users
where u.UserId == userId
select new UserDTO()
{
UserId = u.UserId,
Forename = u.Forename,
Surname = u.Surname,
DateOfBirth = (DateTime)u.DateOfBirth
};
return results.ToArray();
}
}
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public VehicleDTO[] GetVehiclesByUserId(int userId)
{
using (var dbContext = new GSChallengeContext())
{
var results = from v in dbContext.Vehicles
where v.UserId == userId
select new VehicleDTO()
{
VehicleId = v.VehicleId,
UserId = v.UserId,
Registration = v.Registration,
Alias = v.Alias,
Disabled = v.Disabled
};
return results.ToArray();
}
}
This is the code that makes the call:
function ShowUserVehicleInfo()
{
var userId = $("#users").val();
var outStr = "";
$.getJSON('Services/GSChallenge.svc/GetVehiclesByUserId?userId=' + userId,
function (data) {
$.each(data, function (index, elem) {
outStr += "<div class=\"vehicleData\">" +
"<p>Registration: " + elem.Registration + "</p>" +
"<p>Alias: " + elem.Alias + "</p>" +
"<p>Disabled: " + elem.Disabled + "</p>" +
"</div>"
});
}
);
}
I have searched before posting, but have not yet found anything that solves the problem - or even helps.
Can anyone help?
Upvotes: 1
Views: 186
Reputation: 645
Looks like I may have stumbled across the cause; I changed one of the fields in my database from nchar(10) to nvarchar(10). Hey presto: no more 400 bad request error!
I'm not entirely sure why this fixed the problem - but I'm glad it did!
Upvotes: 0
Reputation: 5274
This gave me my result to a similar scenario ...
Web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
GSChallenge definition
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class GSChallenge
Upvotes: 1