title: How to consume a webservice in MVC?
body: <p>I am super new to MVC (in fact, this is my first assignment)</p>
  
  <p>So, I have a good webservice running, functional, on my local machine</p>
  
  <pre><code>http://www.codetrials.local/wcf/UserServices.svc?wsdl
  </code></pre>
  
  <p>and In my MVC application, I added a service reference as usual, and then in my Model.cs I am trying this: </p>
  
  <pre><code> using (CodeTrials.UserServicesClient _client = new UserServicesClient())
    {
         UserWebsite = _client.GetUserWebsite(username);
    }
  </code></pre>
  
  <p>but when I try to run this, I always get the exception <code>endpoint not found</code>. 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 <a href="https://stackoverflow.com/questions/24993/invalidoperationexception-while-creating-wcf-web-service-instance/2423898#2423898">this answer</a> I modified my above code to: </p>
  
  <pre><code>  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);
    }
  </code></pre>
  
  <p>but now, I get a new exception: <code>There was no endpoint listening at http://www.codetrials.local/wcf/UserServices.svc?wsdl that could accept the message</code></p>
  
  <p>So I am at my wits end.</p>
  
  <p><a href="https://stackoverflow.com/questions/6276979/how-to-consume-webservices-from-asp-net-mvc-application">I found a similar question</a> but it's not what I am looking for.</p>
  
  <p>Can you please guide me to the right path?<br>
  what am I not doing right?<br>
  should I shift the consuming of webservice from Model to Controller?   </p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong> - This is my config file <code>system.serviceModel</code> section. I just copy pasted it from the <a href="http://msdn.microsoft.com/en-us/library/bb552364.aspx" rel="nofollow noreferrer">WCF client test gui tool</a> into web.config since it was not being generated by visual studio.</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;bindings&gt;
    &lt;basicHttpBinding&gt;
      &lt;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"&gt;
        &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
        &lt;security mode="None"&gt;
          &lt;transport clientCredentialType="None" proxyCredentialType="None"
              realm="" /&gt;
          &lt;message clientCredentialType="UserName" algorithmSuite="Default" /&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/basicHttpBinding&gt;
  &lt;/bindings&gt;
  &lt;client&gt;
    &lt;endpoint address="http://www.codetrials.local/wcf/UserServices.svc/wcf/UserServices.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserServices"
        contract="IUserServices" name="BasicHttpBinding_IUserServices" /&gt;
  &lt;/client&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>