【问题标题】:how do i call webservice from MVC3 Razor Controller?我如何从 MVC3 Razor 控制器调用 web 服务?
【发布时间】:2013-06-12 00:52:38
【问题描述】:

在我的项目中,我需要从控制器调用 Web 服务。我已经完成了以下操作。

  1. 将 Web 服务的 Web 引用添加到项目中。

  2. 调用服务如下:

    Service Wservice=new Service();
    Wservice.loginCompleted+=new Wservice_login_Completed;
    WService.login_Async("username","Password");
    

    注意:每当我调用此服务时,它都会抛出一个错误,即 “此时无法启动异步操作。 异步操作只能在异步处理程序中启动 或模块或在页面生命周期中的某些事件期间。如果这 执行页面时发生异常,请确保该页面已标记 ."

为了克服这个问题,我使用了

 [Httppost]
 public ActionResult login(logmodel model)
 {
   Task.Factory.StartNew(() => 
    { 
    Wservice.loginCompleted+=new Wservice_login_Completed;
    WService.login_Async("username","Password");
    });

    if(finalresult==true)
    {
      *** return View();
    }
  }

  void Wservice_login_completed()
  {
      Here i got the output.
  }

但是 Wservice_login_completed() 函数的调用是在 View*** 返回之后,所以我没有得到结果。如何实现“从控制器调用 web 服务”.. 有什么想法吗?

【问题讨论】:

  • 你在用AsynController吗?
  • 我的预感是这与页面渲染周期有关,你能从浏览器 Web 开发者工具中发布 HTTP 请求吗,我也怀疑你是否真的处于异步模式。
  • 您为什么使用网络参考而不是服务参考?这是旧版“ASMX”技术的一部分,不应在新代码中使用。
  • @JohnSaunders - 您能否解释一下您说为什么使用网络参考而不是服务参考?
  • @PankajGarg:我建议使用服务参考

标签: asp.net-mvc asp.net-mvc-3 web-services


【解决方案1】:

最后我成功地从 MVC 控制器调用了 webservice。

注意:添加 ServiceReference 而不是 WebReference 并避免
“Task.Factory.StartNew(()=>);”处理。

  [Httppost]
 public ActionResult login(logmodel model)
 {
    Wservice.ServiceSoapClient _host = new Wservice.ServiceSoapClient("ServiceSoap");

    var result_out = _host.login(uname, pwd, "test1", "test2", "test3", "test4");
 }

这里的“ServiceSoap”是我们服务的一个端点。您可以在 app.confiq 或 web.config 文件中显示该端点。 快乐编码...!

【讨论】:

    【解决方案2】:
    1. 获取以下 NuGet:

      microsoft http client   
      (id = Microsoft.Net.Http)
      
    2. 创建 Web API 控制器 (webapi_Controller_Name)
      您的 Post 函数应类似于以下函数
      把这个函数放在你的 Web Api Controller 中

      [HttpPost]  
      public void PostForm(objUser ws_Obj)  
      {   
          // put you code here
      }
      
    3. 从您的常规控制器调用您的 Web 服务,如下所示。 这是一个异步调用,Web Service 将立即返回。

      //call the web service, Asynch        
      HttpClient client = new HttpClient();    
      client.BaseAddress = new Uri("52323/");    
      client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
      client.PostAsJsonAsync("//52323/api/webapi_Controller_Name/PostForm", objContact);
      

    【讨论】:

    • 我为格式道歉,但编辑很挑剔,我把步骤编号了。
    【解决方案3】:

    首先,通过在解决方案资源管理器中右键单击项目名称来创建服务引用,然后将鼠标悬停在“添加”选项上并单击“服务引用...”

    其次,在“添加服务参考”页面的“地址”字段中粘贴您的Web服务地址,一定要在您的Web服务地址末尾添加“?wsdl”,否则将不起作用,然后按“开始”。您将看到 Web 服务出现在“服务”区域中。单击服务以查看将出现在“操作”部分中的可用服务。如果需要,重命名服务,然后按 OK 创建服务。

    最后,将以下代码放入您的 MVC 控制器中。将代码放在 Get 或 Post 控制器中,没关系。

        // Declare the Request object.
        ServiceReference1.GetSurveyRequest myGSRq = new ServiceReference1.GetSurveyRequest();
    
        // You can set the webservice parameters here like...
        myGSRq.Address = getUserData[0].address;
        myGSRq.City = getUserData[0].city;
        myGSRq.State = getUserData[0].state;
    
        // Now declare the Response object.
        ServiceReference1.GetSurveyResponse myGSR = new ServiceReference1.GetSurveyResponse();
    
        //And then use the following to process the request and response.
        ServiceReference1.EMPortTypeClient emptc = new ServiceReference1.EMPortTypeClient();
        myGSR = emptc.GetSurvey(myGSRq);
    
        // In this example the response Object comes back with a URL used to redirect the user 
        //after the webservice has been processed.
        if (myGSR.Url != null)
            Response.Redirect(myGSR.Url.ToString());
        else
            Response.Write("Error");
        return null;
    

    很简单,希望对你有帮助!

    如果您正在创建新服务并且可以选择使用 Web 服务或 Web API,我建议您使用 Web API。 Build RESTful API's with ASP.NET Web API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 2014-02-03
      • 1970-01-01
      • 2011-09-09
      • 2013-11-21
      • 2012-04-03
      相关资源
      最近更新 更多