Web service or web API to connect a Windows application to MVC 4?

After searching all day for what I should use, I'm not sure which option would be better for my needs, so I hope someone with more experience can help me.

I have a winforms application ( c# ) and an ASP.NET MVC 4 web application ( c# ). I want to link them, the goal is to send and receive data from the database that I use in the MVC 4 project, but from a Windows forms application. The data that I send from the Windows Forms application to the database is then used by the MVC 4 web application.

I am completely new to Web Services / Web Api , so I cannot decide which option would be better. Any help would be greatly appreciated.

+7
source share
6 answers

If you have already created an MVC4 project, then you can add actions to any controller and return JSON data, as shown below:

 public JsonResult GetCategoryList() { var list = //return list return Json(list, JsonRequestBehavior.AllowGet); } 

or you can create a new MVC4 project and select the WEBAPI template. He will create a webapi project for you. He will create with an example. So easy to create WEBAPI .In WEBAPI it returns data automatically convert to xml and JSON as per request

WCF Web API abstractions are displayed in ASP.NET Web API as follows

 WCF Web AP -> ASP.NET Web API Service -> Web API controller Operation -> Action Service contract -> Not applicable Endpoint -> Not applicable URI templates -> ASP.NET Routing Message handlers -> Same Formatters -> Same Operation handlers -> Filters, model binders 

Other links

+3
source

If you already have an MVC 4 application, it would be better to use the Web API (RESTful service). I assume that you have some knowledge in creating the REST API (understanding POST, PUT, UPDATE)

It is easy to configure and use. All you need to do is create a new controller, for example:

 class MyApiController: ApiController { public Post(SomeClass item) { ....connect to db and do whatever you need with the data } } 

You must also configure routing for Api.

And then in your winForms application, you can simply use the HttpClient class to make an api call.

 HttpClient aClient = new HttpClient(); // Uri is where we are posting to: Uri theUri = new Uri("https://mysite.com/api/MyApi"); // use the Http client to POST some content ( 'theContent' not yet defined). aClient.PostAsync(theUri, new SomeClass()); 

Take a look at some implementation details right here: Getting Started with Api

Getting started with WCF is not as easy as using the web API.

+2
source

Given the tags you used, I assume that you decide between SOAP Web Services and WCF. Given these two, I say to go WCF. SOAP Web Services (as implemented in Visual Studio) is an older technology; it’s still functional, but WCF can do everything the older SOAP service can do (including look just like a SOAP service), etc.

If you have a web service that connects your web server to the database server (these two things must be on different computers, your web server is working in the world as needed, while your database server should be locked as Fort Knox), I see no reason why you should not use the same service as for the internal WinForms application (using LAN / VPN to access the service level on the database server). For a WinForms application that needs to access data over the Internet, I would recommend redefining this service as a WCF service that supports secure encrypted data transfer. You can also configure the service endpoint only to accept HTTPS connections and thus simply start your existing service through SSL / TLS.

What you choose depends primarily on how much time resources you can solve to solve the problem; Switching to HTTPS is a quick fix that requires a minor code change, while re-executing in WCF will take longer, but will provide additional security measures outside of a simple secure tunnel.

+1
source

Or something light, like Nancy: http://nancyfx.org/

+1
source

We had some problems with MVC4 WebApi materials and ended up using ServiceStack on the server side JavaScript / AJAX for web clients and RestSharp for thick clients.

One of our specific problems was the inability to automatically generate documentation, significant differences in performance, and better support for unit testing / integration.

+1
source

Instead of WCF specifically, I would recommend WCF Data Services or OData with the condition that you need to protect it. If you approach pure WCF , you will see that in the end you will create a lot of code to process information from the database and then send this information back to your clients. It doesn't sound so bad at first, but after about 30 objects in the database, you quickly get tired of a clean WCF solution.

OData great, it uses the Entity Framework , and it quickly opens up data manipulation for an existing database or the one you are about to make. This will save you a lot of development time if you can make your service safe. The data response format is flexible. There are many client libraries ported to other programming languages.

The steps for providing service are pretty simple. Always deploy to https . Any registration or registration methods must be sent methods that return a token (encrypted value) or a unique secret that can be encrypted and sent back for any subsequent requests. Better use a token and expire on the token. Because otherwise, your service and your application, whether mobile or desktop, should have a common encryption / decryption method.

0
source

All Articles