Michael Wilson
Michael Wilson

Reputation: 1297

Not getting the expected results from WCF REST Service (Newbie)

I'm new to WCF Web Services. I'm trying to test my simple hello world web service. For now, I'm doing self hosting. I'm at the point where I've started the host application, opened my browser and typed in the address to my resource. I've also run Fiddler and created a Request by using the Composer. In both cases, I get the "You have created a service." page that has a link to my .wsdl.

I was expecting to see the "Hello World" text in my Response or a web page that has "...Hello world".

What am I missing? or am I just misunderstanding the process?

App.Config

<?xml version="1.0"?>
<configuration>

    <system.serviceModel>
      <services>
         <service name="My.Core.Services.GreetingService" behaviorConfiguration="MyServiceTypeBehaviors">
            <host>
               <baseAddresses>
                 <add baseAddress="http://localhost:8080/greeting"/>
               </baseAddresses>
            </host>
            <endpoint name="GreetingService" binding="webHttpBinding" contract="My.Core.Services.IGreetingService"/>
            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
       </service>
    </services>

    <behaviors>
       <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
       </serviceBehaviors>
    </behaviors>
   </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

Host code

using System;
using System.ServiceModel;
using My.Core.Services;    

namespace My.Service.Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var host = new ServiceHost(typeof(GreetingService)))
            {
                host.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
                host.Close();
            }
        }
}

}

Hello World Contract and Service

using System.ServiceModel;
using System.ServiceModel.Web;

namespace My.Core.Services
{
   [ServiceContract]
   public interface IGreetingService
   {
      [OperationContract]
      [WebGet(UriTemplate = "/")]
      string GetGreeting();
   }
}

using System.Collections.Generic;

namespace My.Core.Services
{
    public class GreetingService : IGreetingService
    {
        public string GetGreeting()
        {
             return "Greeting...Hello World";
        }
    }
}

Upvotes: 0

Views: 160

Answers (2)

sanpaco
sanpaco

Reputation: 815

If I understand you correctly, you can see your wsdl link at the following url

http://localhost:8080/greeting

In order to now call your endpoint, you need to add it to the url like this

http://localhost:8080/greeting/GetGreeting/

I'm not entirely sure why you have the UriTemplate thing in there though other than my guessing that you probably just copy pasted it from an example. Unless you have specific query string parameters that you want defined, you don't really need it and it kind of tends to complicate things so I'd recommend taking it out. That means your Interface would look something like this...

[ServiceContract]
public interface IGreetingService
{
   [OperationContract]
   [WebGet]
   string GetGreeting();
}

...and you can then lose the final "/" on the url.

Upvotes: 1

Michael Wilson
Michael Wilson

Reputation: 1297

I figure out the problem. When I use the url: "http://localhost:8080/greeting" the server sends the temp page. When I add the backslash "/" on the end of the url it execute my service. so, "http://localhost:8080/greeting/" works and sends me the "...Hello World" back.

Upvotes: 0

Related Questions