I want to add a public (externally callable) JSON data feed to my ASP.net (4) Forms web site. To this end, I have created the following Web Service:
[WebService(Namespace = "http://localtest.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class BlogWebService : System.Web.Service.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Blog> GetLatestBlogs(int noBlogs)
{
return GetLatestBlogs(noBlogs)
.Select(b => b.ToWebServiceModel())
.ToList();
}
}
I have tested this on the local server by opening
http://localhost:55671/WebServices/BlogWebService.asmx?op=GetLatestBlogs
and it works correctly.
When I try to access this service remotely and get an Internal Server Error. For example, I have run the following code using LinqPad (based on some script from http://geekswithblogs.net/JuanDoNeblo/archive/2007/10/24/json_in_aspnetajax_part2.aspx):
void Main()
{
GetLatestBlogs().Dump();
}
private readonly static string BlogServiceUrl =
"http://localhost:55671/BlogWebService.asmx/GetLatestBlogs?noBlogs={0}";
public static string GetLatestBlogs(int noBlogs = 5)
{
string formattedUri = String.Format(CultureInfo.InvariantCulture,
BlogServiceUrl, noBlogs);
HttpWebRequest webRequest = GetWebRequest(formattedUri);
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
jsonResponse = sr.ReadToEnd();
}
return jsonResponse;
}
private static HttpWebRequest GetWebRequest(string formattedUri)
{
Uri serviceUri = new Uri(formattedUri, UriKind.Absolute);
return (HttpWebRequest)System.Net.WebRequest.Create(serviceUri);
}
I have a number of questions/doubts:
- How should the call to the web service be formatted? I’m not sure the construction of my BlogServiceUrl in the LinqPad test code is correct.
- Are my BlogWebService class and GetBlogs() method defined and attributed correctly?
- Do I need to to add anything to my web site configuration to make this work?
Any advice would be much appreciated.
I was unable to find a way to get the HttpHandler (.ashx) mechanism working for remote JavaScript calls – I always received the ‘Invalid referrer’ error in the remote jQuery call. I’m not saying it is not possible, but I could not find any documentation on how to do it – not that worked anyway.
In the end, I converted the code to a WCF service guided by this article by Ben Dewey – which was very helpful – make sure you apply the class and method attributes carefully – I didn’t and spent a happy hour or so trying to figure out what was wrong!
Here’s the code:
IBlogService.cs
BlogService.svc
Web.config
Test JavaScript
As you can see, I also used Ninject DI. This was problematical until I came across the Ninject.Extension.Wcf module which made life very easy. Couple of gotchyas though.
I couldn’t find much documentation
Add the ‘factory’ attribute to the WCF page:
Factory=”Ninject.Extensions.Wcf.NinjectWebServiceHostFactory”
I hope it helps.