简体   繁体   English

WCF和.net服务

[英]WCF and .net service

I have .net service. 我有.net服务。 I want to consume it in website service which is hosted in MVC web application. 我想在MVC Web应用程序中托管的网站服务中使用它。 What is the best design to do that in MVC pattern. 在MVC模式中做到这一点的最佳设计是什么。

The solution ultimately depends on 1) Time and 2) Security. 解决方案最终取决于1)时间和2)安全性。 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. 如果您有足够的时间,可以购买一个包装器API,这将使您可以设置对外部Web服务方法的访问安全性。 I assume you don't want to have direct access to the external service by javascript code. 我假设您不希望通过javascript代码直接访问外部服务。 If you need information like "what user just asked for record XYZ" then you can also wrap auditing into your wrapper api. 如果您需要诸如“什么用户刚刚要求记录XYZ”之类的信息,则还可以将审核包装到包装api中。 You will likely want to invoke some web service methods on the client side as well. 您可能还希望在客户端调用一些Web服务方法。

I would use ajax to get call the service (through your controller) if they are simple invocations. 如果它们是简单的调用,我将使用ajax来调用服务(通过您的控制器)。 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. 如果它们返回较大的结果,那么您可能需要直接在控件中调用Web服务并将数据推送到视图模型。

An example of just getting your web service results to json: 仅将您的Web服务结果转换为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). 如果您愿意,可以将该操作结果转换为WebApi控制器,然后实质上是在现有Web服务(当然是受公共保护且仅称为服务器端)的周围进行WebApi包装。

Alternatively you can pass your IEnumerable result from the web service back to the view model. 或者,您可以将IEnumerable结果从Web服务传递回视图模型。 Or just a T back to the view model. 或者只是一个T回到视图模型。

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 有关其他信息,请查看这个类似的问题ASP.NET MVC和Web服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM