如何消耗MVC一个web服务? [英] How to consume a webservice in MVC?

查看:75
本文介绍了如何消耗MVC一个web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是超级新MVC(其实,这是我的第一项任务)

所以,我有一个很好的web服务的运行,功能齐全,我的本地机器上的

  HTTP://www.$c$ctrials.local/wcf/UserServices.svc WSDL

在我的MVC应用程序,我添加了一个服务引用像往常一样,然后在我的Model.cs我想这样的:

 使用(codeTrials.UserServicesClient _client =新UserServicesClient())
  {
       UserWebsite = _client.GetUserWebsite(用户名);
  }

但是当我尝试运行此,我总是得到异常端点未找到。我可以从我的(不同的)asp.net项目访问这和它工作得很好,同样code和一切。一些周围挖后,我发现<一个href=\"http://stackoverflow.com/questions/24993/invalidoperationexception-while-creating-wcf-web-service-instance/2423898#2423898\">this回答我修改了上面的code为:

  basicHttpBinding的约束力=新basicHttpBinding的();
  的EndpointAddress地址=新的EndpointAddress(HTTP://www.$c$ctrials.local/wcf/UserServices.svc);
 使用(codeTrials.UserServicesClient _client =新UserServicesClient(绑定,地址))
  {
       UserWebsite = _client.GetUserWebsite(用户名);
  }

但现在,我得到一个新的异常:有没有在端点的 HTTP://www.$c$ctrials.local/wcf/UserServices.svc WSDL 是可以接受的消息

所以我在我束手无策。

<一个href=\"http://stackoverflow.com/questions/6276979/how-to-consume-webservices-from-asp-net-mvc-application\">I发现了类似的问题,但它不是我所期待的。

能否请您指引我正确的道路?结果
什么我不这样做对吗?结果
我应该从型号转向Web服务的消费来控制?

感谢。

修改 - 这是我的配置文件 system.serviceModel 部分。我只是复制从 WCF客户端测试GUI工具,它粘贴到的网络。因为配置没有被由Visual Studio生成了。

 &LT; system.serviceModel&GT;
&LT;&绑定GT;
  &LT;&basicHttpBinding的GT;
    &LT;绑定名称=BasicHttpBinding_IUserServicescloseTimeout =00:01:00
        openTimeout =00:01:00receiveTimeout =00:10:00的SendTimeout =00:01:00
        allowCookies =假bypassProxyOnLocal =假hostNameComparisonMode =StrongWildcard
        MAXBUFFERSIZE =65536maxBufferPoolSize =524288maxReceivedMessageSize =65536
        messageEncoding =TEXTtextEncoding =UTF-8transferMode =缓冲
        useDefaultWebProxy =真正的&GT;
      &LT; readerQuotas MAXDEPTH =32maxStringContentLength =8192maxArrayLength =16384
          maxBytesPerRead =4096maxNameTableCharCount =16384/&GT;
      &LT;安全模式=无&GT;
        &LT;运输clientCredentialType =无proxyCredentialType =无
            境界=/&GT;
        &LT;消息clientCredentialType =用户名algorithmSuite =默认/&GT;
      &LT; /安全&GT;
    &LT; /&结合GT;
  &LT; / basicHttpBinding的&GT;
&LT; /绑定&GT;
&LT;客户端&GT;
  &LT;端点地址=HTTP://www.$c$ctrials.local/wcf/UserServices.svc/wcf/UserServices.svc
      绑定=basicHttpBinding的bindingConfiguration =BasicHttpBinding_IUserServices
      合同=IUserServicesNAME =BasicHttpBinding_IUserServices/&GT;
&LT; /客户&GT;
&LT; /system.serviceModel>


解决方案

从注​​释重新张贴,因为它原来是答案。 :)原来,web.debug.config被覆盖在这种情况下,web.config文件。

您务必正确的web.config那么它的? VS应该自动创建它,当您添加服务引用(它至少不会对我来说)。有一个问题是,正常的MVC应用程序有两个web.config文件中,有默认的浏览文件夹中的第二个。其他然后,我真的不知道怎么回事。

I am super new to MVC (in fact, this is my first assignment)

So, I have a good webservice running, functional, on my local machine

http://www.codetrials.local/wcf/UserServices.svc?wsdl

and In my MVC application, I added a service reference as usual, and then in my Model.cs I am trying this:

 using (CodeTrials.UserServicesClient _client = new UserServicesClient())
  {
       UserWebsite = _client.GetUserWebsite(username);
  }

but when I try to run this, I always get the exception endpoint not found. I can access this from my (different) asp.net project and it works just fine, same code and everything. After some digging around I found this answer I modified my above code to:

  BasicHttpBinding binding = new BasicHttpBinding();
  EndpointAddress address = new EndpointAddress("http://www.codetrials.local/wcf/UserServices.svc");
 using (CodeTrials.UserServicesClient _client = new UserServicesClient(binding, address))
  {
       UserWebsite = _client.GetUserWebsite(username);
  }

but now, I get a new exception: There was no endpoint listening at http://www.codetrials.local/wcf/UserServices.svc?wsdl that could accept the message

So I am at my wits end.

I found a similar question but it's not what I am looking for.

Can you please guide me to the right path?
what am I not doing right?
should I shift the consuming of webservice from Model to Controller?

Thanks.

EDIT - This is my config file system.serviceModel section. I just copy pasted it from the WCF client test gui tool into web.config since it was not being generated by visual studio.

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IUserServices" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
        useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://www.codetrials.local/wcf/UserServices.svc/wcf/UserServices.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserServices"
      contract="IUserServices" name="BasicHttpBinding_IUserServices" />
</client>
</system.serviceModel>

解决方案

Reposting from the comments since it turned out to be the answer. :) Turned out that web.debug.config was overwriting the web.config in this case.

Are you sure its in the right web.config then? VS should create it automatically when you add the service reference (it does for me at least). One gotcha is that a normal MVC app has two web.config files, there's a second one inside the Views folder by default. Other then that, I'm not really sure whats going on.

这篇关于如何消耗MVC一个web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆