This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Call a WCF Rest Service in an ASP.Net Web Form

 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

The title basically says it all. I have created a rest service using a WCF Service Application in C# and I am trying to call the service in an ASP.net Web Application (Web Form). Here are some things I have tried as well as my config file for the restful service. Please let me know if anything else is needed to understand the problem and how to solve it. Also, I should mention the first method was "working" until recently. Thank you for your help!


This provides this error "System.InvalidOperationException: 'Could not find default endpoint element that references contract 'RestBMI.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.'"


this provide this error: System.Net.WebException: 'Unable to connect to the remote server'

webconfig:
 
Saloon Keeper
Posts: 12133
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rhyeca,

The first snippet won't work because your web.config doesn't provide an endpoint address for your WCF service.

I'm not exactly sure why the second snippet won't work, although in general it's not a good idea to access a WCF service using WebClient. Instead, for a declarative approach use a service reference in your client project (this is likely what you already did to make your first snippet work), or for a programmatic approach use ChannelFactory or for RESTful web services use HttpClient.

First make sure the service is running by browsing to http://localhost/Service1.svc. This should give you a basic WCF page that links to service descriptors. If it is running, add the endpoint to your web.config:

Let us know if this solves your problem. If not, remove all configuration related to the web service from your client project, and try to add a new service reference through Visual Studio's service reference wizard.
 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I tried both of your suggestions and I am still getting the same "System.InvalidOperationException: 'Could not find default endpoint element that references contract 'RestBMI.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element'" error. I removed the service reference and added the address="Service.svc" (wasnt able to add the localhost portion as I was getting errors).
 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what my IService1.cs file looks like. Not sure if this would help with understanding how I am setting up the Restful service. (The ... after the healthindicator and BMI are to avoid getting flagged for cheating (if this were to get flagged).

 
Stephan van Hulst
Saloon Keeper
Posts: 12133
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably you also have an implementation of that interface on your server side.

Can you show us the implementation class (minus the method bodies), and can you also show us the service interface header and its attributes?

You should have a web.config on both the server-side (because it is a WCF application) and on the client-side (because it is an ASP.NET application). Please show us both web.config files.

How did you add the service reference, by the way? Did you use the wizard provided by Visual Studio?
 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Implementation of interface





Client side web.config. To answer your question yes I used the wizard to add the service reference.

 
Rhyeca Riley
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, I solved this by doing the HttpWebRequest method which worked out great. Thank you all for your help and guidance!
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic