?
Solved

Consuming a WCF Service in a web page

Posted on 2012-09-19
4
Medium Priority
?
885 Views
Last Modified: 2012-09-25
I am learning to create a consume a WCF service called GreetingService using VS2010, .net 4.0 framework, 64 bit machine.

I successfully created the GreetingService. App.config code copied below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="mexBehavior" name="Wcf.Samples.ServiceLibrary.GreetingService">
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint binding="basicHttpBinding" bindingConfiguration=""
          contract="Wcf.Samples.ServiceLibrary.IGreeting" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8700/GreetingService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

Open in new window


I then added a WCF Website to the same solution that hosts this WCF service.
The web.config code is copied below:
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="mexBehavior" name="Wcf.Samples.ServiceLibrary.GreetingService">
        <endpoint binding="basicHttpBinding" bindingConfiguration=""
          contract="Wcf.Samples.ServiceLibrary.IGreeting" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:80/GreetingService/Service.svc" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

Open in new window


I can browse the service.svc in visual studio and it shows me the GreetingService Service page.

Now I have two questions:
1. How to call this web service from a web page. I have created a .net website where I have added web reference to the hosting website. I need to know what to do next?

2.I published the hosting website on IIS, but I can not browse to GreetingService Service page (that I see in debug mode) by browsing the service.svc file on IIS. What am I missing?
I am trying to browse to http://localhost:80/GreetingService/Service.svc
Whoops! The page could not be found. Try giving it another chance below

Also tried: http://localhost:80/GreetingServiceHostWebsite/Service.svc
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

Thank you for your help.
0
Comment
Question by:patd1
  • 3
4 Comments
 
LVL 5

Assisted Solution

by:sameer_goyal
sameer_goyal earned 2000 total points
ID: 38416433
Hi,

Did you create an SVC file in your wcf project?

See these 2  videos. They will help you get started with WCF services

This first one describes creating a WCF Service

http://www.youtube.com/watch?v=WAq_Z-dT67o&feature=plcp

And this other one, shows how to host wcf service on IIS and consume it

http://www.youtube.com/watch?v=mX8quq7MoeI&feature=plcp

Hope it helps.
0
 

Assisted Solution

by:patd1
patd1 earned 0 total points
ID: 38419076
Thanks Sameer_goyal. I checked the videos. I have the IIS hosting problem solved. it was actually an issue with iisreg for .net4.0.

I still need to know how to call  this service from a webpage.

When I created a web service in the past and hosted it in IIS, I could get to a page on the browser where I could put in my parameter value and invoke the web service to get result in an xml. Why am I not getting that in this solution? When I browse the svc file I get a page that links to the wsdl but not a page where I can test the wcf service. What am i missing?

Thanks.
0
 

Accepted Solution

by:
patd1 earned 0 total points
ID: 38420253
I figured it out.
Added a service refrence to the service I hosted on IIS, that created a web page with a two text boxes and a button and then gave the implementation for onClick for the button to invoke the service.

 protected void ButtonInvokeGreetingService_Click(object sender, EventArgs e)
    {
        GreetingClient client = new GreetingClient("WSHttpBinding_IGreeting");
        string name = TextBoxName.Text;
        TextBoxGreeting.Text = client.Greet(name);
        client.Close();
    }

Open in new window

0
 

Author Closing Comment

by:patd1
ID: 38431954
I figured it out.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question