Question

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.

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top