How to create JSON webservice in ASP.NET with database connectivity? - asp.net

How to create a JSON webservice in ASP.NET that connect to MS SQL Database and update/delete values from the database?
This webservice will eventually be used to communicate with iPhone SDK.
Any help would be appreciated.
Thanks!

You could do this:
1) Make a data access layer of some sort.
Example: Linq to SQL, Entity Framework, SubSonic, nHibernate, ADO.Net
2) Create a web service (.asmx file) which calls your data access.
3) Make methods in the web service which return json seralized objects or what not.
[WebMethod]
public Item GetSingleItem()
{
return DataStore.FetchItem(); // return a single object
}
4) Have a client which can send parameters to the web service and then consume the returned json. I'm thinking that your client on the iPhone will either be a native iPhone application or a web application run through the phones browser. Either way, this sounds like a neat idea.
Good luck, and hope this short list helps you out some.

Related

Understanding of OData services

This is a question to improve my understanding about OData and the process of OData services. I'm not sure about the process when an OData request is sent to the server from my Fiori app. The app is added to our Fiori Launchpad. When the user wants to create a new target group in the UI, a create request is sent. What happens then in detail? What I thought so far:
OData service checks the data
If the data is valid, a new entry in the database is created (HTTP POST)
If the data is not valid, the OData service sends an error
I'm not sure about what information is delivered by the OData service and what information is delivered directly from the database? Does the OData service work like a adjustor which transfers the messages sent from the database to the application?
I hope you can understand what I'm trying to figure out. Thank you for your time.
it depends how your backend-methods are implemented. Every Entityset usually has one of these Methods:
Get Entity
Get EntitySet
Create
Update
Delete
There are some more I guess, but these are mostly used by developers. You can redefine every single method and implement your own Business Logic in there.
So, let's assume you want to send data from the Frontend to your service and insert the data into a table inside your database. You have to redefine the create method of your entity and implement own logic. This could contain an insert into a database-table. You have to consider, that your oData Service will throw an Error if the types which are sent from the frontend do not match the Entity-Types (i.e. a String into an edm.Time type).
Here you can find all EDM.Types oData could consume and the correct mapping of the types:
https://help.sap.com/saphelp_gateway20sp12/helpdata/en/76/4a837928fa4751ab6e0a50a2a4a56b/frameset.htm
Hope this helps :)

C#/Asp.net: Can we consume WCF Web Service with SqlDataReader?

Right now I'm currently converting WCF web service to DataTable (we know this can get messy), then planning to convert it to SQL Db Type. I was thinking, can't I just consume the WCF service as a SQL Db Type? If so, I've searched and couldn't find a solution to this. What I'm planning to do is sending the DataTable over Sql Data Type.
There exists this approach: http://sharpfellows.com/post/Returning-a-DataTable-over-SqlContextPipe. However, that's a 2006 article and I'd like to skip the .net DataTable.
Code example of how to read WCF as SQL datatype would be much appreciated, thanks!
A DataReader is a class that keep connection open with database, so you cannot consume a service that returns a DataReader.
And return a DataTable is equals a bad idea, you must read the data and return a class with just data.
Maybe a good solution to your scenario can be WCF Data Services, that do what you want: data access.
You can read more here: https://msdn.microsoft.com/en-us/library/cc668794(v=vs.110).aspx

ASP.net "generic" SOAP web services

I've developed a data access layer that grants the following methods to any subclasses:
List<DataObject> Select(int primaryKey)
List<DataObject> SelectAll()
void Insert(...)
void Delete(int primaryKey)
void Update(...)
I'd like for there to be some easy way I can wire them up to some kind of dispatcher that would look at the URL, pick out the type and present the CRUD operations as web methods in a traditional SOAP web service, generating the WSDL for them on the fly.
Something like:
http://Server/Customer/
or
http://Server/Address/
That would present the typical web service client test page we're all used to seeing, with Select(), etc presented as Web Methods. Is that possible in ASP.net? How would I go about doing this?
You should not use ASMX web services for new development. Use WCF instead.
In your particular case, see ADO.NET Data Services (renamed to WCF Data Services in .NET 4.0).

asp.net mvc as a restful service and an application?

Currently I am working on small application in asp.net mvc. It is a some kind of localization tool. Our clients login on our application and they can translate terms that shows up in our applications that they use. It is happens like this:
login to localization site
find and translate certain terms for example title for button in "Catalog" application
start that application(for example, "Catalog" application from 2.) and trough web services they update local database of terms with these that are translated
It is our old solution and that works fine.
But now, I am doing refactoring of a translating tool application in asp.net mvc and I think, why we have separate logic(doubled) in mvc and in web services? Why we can't use only mvc as a web service...this way I have only one logic(fetching elements and updating) and we don't need to create wcf web service or something. And most important, I don't have mesh desktop applications with dependency on dll's in which I have this logic.
And now the question. What I can get from controller in mvc, except of views and JsonResults...can I get collections of my objects directly?
Or more plain question, how I can use asp.net mvc as a web services. What is your experience?
cheers
Marko
It really depends on your needs. You can most certainly use an ASP.NET MVC application as a data service, but it sounds like you should tease out your shared code into a common library and reference that library in both applications. There are some things that web service projects provide that would be more difficult purely as a controller action. (de/serialization, wsdl, etc...)
It really depends on what will consume your web service.
For example: jQuery consumes JsonResults well because i can return complex objects (with collections, arrays, nested objects etc) from an action and have jQuery deserialize it back to javascript object for use in the clients browser. Of course you loose type safety with the serialization process but that's pretty expected in most REST/SOAP based web services. If you really need the type safety for the consuming application, stick with WCF (or similar).
I'd just create a flag to return an action as Json. I've noticed a few sites do it this way. Say you have this action:
public ActionResult GetPeople()
{
IList<Person> result = svc.GetPeople();
return View(result);
}
..this action's result is normally rendered into some view. That's great, but if you want to use the action as a web service, you could simply change it to this:
public ActionResult GetPeople(string ajax)
{
IList<Person> result = svc.GetPeople();
if (Convert.ToBoolean(ajax))
return Json(result);
else
return View(result);
}
..so if your consuming app didnt mind the serialized Json then instead of invoking the GET request like this http://domain.com/controller/GetPeople (as a browser would to get the View), you'd just add the ajax flag like so http://domain.com/controller/GetPeople?ajax=true to return the Json. A more appropriate flag might be 'json' instead of 'ajax' - 'ajax' is common because this method is used to support downlevel browsers for actions that may optionally be invoked with ajax.
I've been thinking of adding this to my mvc app for a while but i don't like the idea of modding every action with this flag and add more if statements. My idea is to create a custom attribute to decorate actions that you want this functionality for and the attribute dynamically adds the extra flag and conditionally returns the model data as Json rather than what's originally specified. Give it a go.

What are all the valid ASP.NET Webmethod return types?

Can my webmethod only return strings like I see in all the asp.net site examples?
asp.net Webmethods can return any serializable data type.
Assuming that this question is about the legacy ASMX web service technology, see Data Types Supported by XML Web Services Created Using ASP.NET.
Be sure to take note of where it says:
This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation (WCF).
As far as I know, you can pretty much return any .NET class, including anonymous types. I've returned custom objects representing my business entities, including generic collections of children entities. Guids, ints, strings, etc. Anything that can be serialized into a string basically.

Resources