I have .net service. I want to consume it in website service which is hosted in MVC web application. What is the best design to do that in MVC pattern.
-
WCF Webservices is usually hosted separately then can be consumed by Web application by adding Service Reference (NOT Webservice Rerefence) – Jobert Enamno Feb 24 '13 at 05:20
-
Perhaps your services might be simple enough for `WebApi`? – Jeremy Feb 24 '13 at 05:22
-
@JobertEnamno here is my requirement you are required to store and display a numbers in an ASP.NET MVC4 application fetched from a .NET service and describe the architecture. The website service must communicate with the.NET service using a WCF binding of some sort. The service method shall accept a prime number and return the next greater prime number – Feb 24 '13 at 05:36
-
@JeremyChild ... Kindly read requirements – Feb 24 '13 at 05:39
-
@ArslanAhson you can either call the ASMX/WCF service from the controller or you can create another `WebApi` service which in turn calls your ASMX/WCF service. It depends if you want to return the results ajaxly etc. You could make an `ActionResult` on your controller return the data from the service too. – Jeremy Feb 24 '13 at 05:43
-
@JeremyChild There are two services as I can understand,one is containing business logic which shall be consumed by service hosted in web application. My question is what is design of solution in term of best practice, Thanks. – Feb 24 '13 at 05:50
-
@ArslanAhson You should add more detail to your question – Jeremy Feb 24 '13 at 06:00
-
@JeremyChild Agree with you Thanks anyways ... but again is it that simple that client add refernce of serviceA and serviceA add refernce of serviceB (which is business service) – Feb 24 '13 at 06:08
-
@ArslanAhson Yes it is that simple. The references generate all the code required. It will take 10 minutes tops. – Jeremy Feb 24 '13 at 06:09
1 Answers
The solution ultimately depends on 1) Time and 2) Security. If you have plenty of time you can invest in a wrapper api which will in tern allow you to set security on access to the external web service methods. I assume you don't want to have direct access to the external service by javascript code. If you need information like "what user just asked for record XYZ" then you can also wrap auditing into your wrapper api. You will likely want to invoke some web service methods on the client side as well.
I would use ajax to get call the service (through your controller) if they are simple invocations. If they return large results then perhaps you need to call the web service directly in the control and push the data to the view model.
An example of just getting your web service results to json:
public ActionResult MarkEmployeeAsInactive() {
// Check if user is allowed to make the change to this employee?
// Check if session is valid if not using OAUTH etc
var client= new MyWebServiceClient();
var result = client.SetEmployeeInactive(443);
return Json(result);
}
public ActionResult GetList() {
// Check if user is allowed to make the get this list
// Check if session is valid if not using OAUTH etc
var client= new MyWebServiceClient();
var result = client.GetListOfEmployees(23443);
return Json(result); // Returns json List<Employee>
}
You can turn that action result into a WebApi
controller if you so desire then you are essentially making a WebApi
wrapper around your existing web service (which is of course protected from the public and called server side only).
Alternatively you can pass your IEnumerable result from the web service back to the view model. Or just a T
back to the view model.
public ActionResult Employee()
{
var client= new MyWebServiceClient();
var employee = client.GetEmployeeRecord(33445);
// perhaps employee has a property called FullName?
// That will map to the model then
return View(employee); // Passes Employee class to view
}
For additional information have a look at this similar question ASP.NET MVC & Web Services