1

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>
Community
  • 1
  • 1
LocustHorde
  • 6,361
  • 16
  • 65
  • 94
  • Could you post your endpoint configuration in the web service itself? – Timbo Aug 15 '11 at 11:38
  • You shouldn't need to do any of that stuff in code if your MVC app has a correct system.servicemodel web.config entry. Can you post it? – Tridus Aug 15 '11 at 13:45
  • Could you post the full exception stack trace? – Darin Dimitrov Aug 15 '11 at 14:56
  • @LocustHorde - your endpoint address has a duplicate /wcf/UserServices.svc on the end - try removing this (though this should have been overridden by your hard coding - which you should probably remove). Then double check your service's endpoint address - in your services app, web.config > system.serviceModel > services > service > endpoint. For local testing you can set address="" to rule out this being an issue. – Timbo Aug 15 '11 at 15:47
  • @LocustHorde hmm. That looks fine. 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. – Tridus Aug 15 '11 at 15:48
  • 1
    @Tridus! you're a life saver. All this time I was trying to modify ``web.config`` and when I compiled, the ``web.debug.config`` was overwriting it and hence no end point. Now, I changed it directly in debug config and it's all fine. Can you post your comment as an answer so I can accept it? Thanks. – LocustHorde Aug 16 '11 at 11:42

2 Answers2

1

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.

Tridus
  • 5,021
  • 1
  • 19
  • 19
0

Your code shows you are using this URL: http://www.codetrials.local/wcf/UserServices.svc to access the service endpoint but your exception message says you are actually using http://www.codetrials.local/wcf/UserServices.svc?wsdl instead.

Check your MVC app web.config file for a serviceModel element. If you need to configure the WCF client in code then remove that entire element from the web.config file which may be where the wrong URL is coming from. If you do want to configure WCF from the web.config file, then remove your current code and use the following two lines to create the client and invoke the service:

var _client = new UserServicesClient("BasicHttpBinding_IUserServices");
UserWebsite = _client.GetUserWebsite(username);

where the something like the following section exists in your web.config serviceModel element:

<system.serviceModel>
    <client>
      <endpoint
        name="BasicHttpBinding_IUserServices"
        address="http://www.codetrials.local/wcf/UserServices.svc"
        binding="basicHttpBinding"
        contract="IUserServices" >
      </endpoint>
    </client>
<!-- rest of element snipped -->

Finally, you should not wrap the UserServicesClient instantiation in a using statement because of the reasons outlined in this post. WCF is a tricksty beast....

EDIT: Based on the update with your config, your problem may be that the service URL is:

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

The wcf/UserServices.svc seems to be duplicated.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • Hi, the second ``wcf/UserServices.svc`` is being added by the VS itself, I am not manually adding and and when the same stuff is being generated by the WCF Client test tool gui, it's working with the double ending... I am confused too! – LocustHorde Aug 16 '11 at 11:43