If your a .NET Developer like me and you are looking to transition into the wonderful world of mobile development. There is a quick way for you to start your journey towards developing HTML5 applications supported on IOS and Android without having to develop in Java or CoCoa (Objective C). First things first, lets look at what you already know or what your familar with. Data/Content is king, so if your developing an app that is not dyanamically bringing some data to your user, just stop now and close this post.
Lets look at WCF Serves for .NET 4.0. you want starting using these if you are not currently. My suggestion is to set up a DAL (Data Access Layer) for all of your communications to your datasource then consume it through your services. In Visual Studios what you want to do is create a WCF web service, let just start off with the configuration this will kill you if it is not configured right. I know i definetly bit the bullet on this one. List below is an example of mission critical configuration elements, that need to be present in your web.config file to enable JSONP communcations in your webservice:
<service behaviorConfiguration=“ServiceBehavior“ name=“thenews“>
<endpoint address=“” binding=“webHttpBinding“
<binding name=“webHttpBindingJsonP“ crossDomainScriptAccessEnabled=“true“/>
The items that are highlighted are essential for making this all work for you. The “crossDomainScriptAccessEnabled” will bite you hard if you are trying to have your UI connect to your services. So make sure you enable it.
Next move over to your Interface “Ithenews.cs”. This will be created when you create your service. You will have two files. The interface which is what is called from application, and the actual class “thenews.cs”. This i where all your magic is happening.
Your interface should be configured like this:
[WebInvoke(Method=”GET”,BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = “getNews“)]
List<newsItem> getNews(); (Use List<> or Obervable Collections. Simple create a class with your return properties in a format that you want deliver, then use the List<> or Observable to create and array of that object. You can use DataTables and Datasets to create these collections, within your SqldataAdapter or whatever your using to populate the data from its source.)
The WebMessageFormat.Json specifies the format your service is going to give back. And the UriTemplate is what your url call will be from your jquery client. E.G. (http://api.myservices.com/news.svc/getNews)
Writting your method for the “thenews.cs” is up to you. (good luck)
http://jquery.com/ (Simple Right ^_^)
After you have the file add it to your html file.
here is your div on your page
//$(e).append(“<h3>” + item.title+ “</h3>”);
$(e).append(“<li>” + item.url+ “</li>”);
loadNews(); //Go out to get the news on the page reload If you would like to do this on a frequency, you can wrap it with a setTimeout(“loadNews()”,30000);
Make sure you set your dataType to jsonp and set your success: is the callback function after it retrieves the data. After the data is retrieved your can use a jQuery function to iterate throuth the collection like listed above. By using the classes in your WCF Webservice it ill make it easier for you to grab the field names from the results.
If you have any quetions you can find me at:
Chief Executive Officer