DotnetSparrow
DotnetSparrow

Reputation: 27996

WCF service to receive json and add entry into database

I need to create a service which will be hosted in IIS. This service will have a function with one parameter of type string. This function will receive Json data. The client which will be using this service need to be able to access this service method with url like this:

http://[local]host/Myservice/mymethod

I am new to services. I am using asp.net 4.0 and VS 2010. My questions are:

  1. Should I create SOAP service or WCF service (I assume WCF can not be SOAP)

  2. If I need to user WCF service then which type of WCF service? I see there are several types of WCF service, like REST service, Data service (and probably JSON service as well).

  3. I had used this template to create WCF service. Is it good to use this: http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd because I don't need to create data contracts etc. or may be this article where http://www.dotnetspark.com/kb/3189-create-rest-service-wcf-40-step-by-step.aspx because I don't have to change web.config and change end points manually ?

  4. This service method will then parse the json using json.net and get some values and then add them in database and send email. Does it mean that I need to WCF data services?

Please suggest.

Regards, Asif Hameed

Upvotes: 0

Views: 1194

Answers (1)

marc_s
marc_s

Reputation: 754598

Well, first off - WCF is very strong in the SOAP area, in fact, it supported SOAP-based bindings exclusively in the beginning.

SOAP is a strong contender if you want to be able to "auto-discover" your service and its methods. SOAP has a well-established metadata system (using the WSDL and XSD standards), so a client can basically just point to a service location and get the whole description of what that service can do, and what parameters it expects etc. Also: SOAP is based on the I have a service with x methods metaphor - so you'll have service methods and their parameters as your main items to design.

SOAP on the other hand is a bit less widely available - it's available on most desktop systems, but not really on most mobile devices like phones and tablets.

That's where REST shines - since it uses only HTTP, it's available everywhere and thus you can reach more clients. Also, since you're specifically using JSON and your service seems to be a rather simplistic one (just a single call with a single parameter) this might be a better fit for REST (the WCF webHttpBinding or a service based on the ASP.NET Web API).

Note however: REST is fundamentally based on resources - e.g. you're not talking about methods and method parameter in REST - you're manipulating resources. So you'll use the various HTTP verbs (GET, PUT, POST, DELETE) and the URI you're talking about represents a resource (whatever that might be).

So if you can model your system to be something like I'm POSTing a new resource (like a new database record), then you can use REST for this. To add a new row to your database, you would be doing a POST call to a URI that represents your resource collection, e.g.

POST  /host/app/MyResourceCollection

and provide the necessary data to actually create a new row in the post body of your HTTP call.

So your requirements a bit conflicting:

  • the idea of having a service method seems more suited to SOAP
  • the idea of using JSON is more appropriate for REST

I would recommend you read this excellent MSDN article An Introduction To RESTful Services With WCF by Jon Flanders to get a better understanding of what REST is and how to use it. See if it fits your needs, and if yes - go with it, try it, experience how it works.

Upvotes: 1

Related Questions