Connor Gervin
Connor Gervin

Reputation: 946

REST Service not showing results in browser

I am trying to create my own web service using Rest for a university Project. After doing some tutorials I have been able to create my own and the service is working and returning results in visual studio using the wcf test client.

When I browse to the service however (http://localhost:53215/UserService1.svc) I can see the service page but http://localhost:53215/UserService1.svc/GetUsersNames send gives me 404, page not found error!

Can anyone see what I'm doing wrong?

Here's my code.

Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>    
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfRestSample.IUserService1">
        <endpoint address="" contract="WcfRestSample.IUserService1" binding="webHttpBinding" behaviorConfiguration="restBehavior"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
      <add name="cs4_databaseEntities" connectionString="metadata=res://*/cs4_model.csdl|res://*/cs4_model.ssdl|res://*/cs4_model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\cs4_database.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

IUserService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfRestSample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IUserService1" in both code and config file together.
    [ServiceContract]
    public interface IUserService1
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Xml)]
        List<string> GetUsersNames();
    }
}

UserService1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfRestSample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "UserService1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select UserService1.svc or UserService1.svc.cs at the Solution Explorer and start debugging.
    public class UserService1 : IUserService1
    {
        public List<string> GetUsersNames()
        {
            using (cs4_databaseEntities entities = new cs4_databaseEntities())
            {
                return entities.Users.Select(user => user.Name).ToList();
            }
        }
    }
}

The tutorials I used worked fine with the browser!

Upvotes: 2

Views: 1230

Answers (2)

Marvin Smit
Marvin Smit

Reputation: 4108

Your endpoint is http://localhost:53215/UserService1.svc/rest/GetUsersNames. The "rest" part comes from your configuration 'address="rest"'

Editor:

The service name is set to the interface not the implementation class, change:

<service name="WcfRestSample.IUserService1">

to

<service name="WcfRestSample.UserService1">

One consequence of this is the service no longer loads in the wcf test client, maybe this is why its a source of confusion - some people document their steps to work with the wcf test client and others for a web browser but its an easy fix to change it back to using the interface!

Upvotes: 4

Connor Gervin
Connor Gervin

Reputation: 946

changed <service name="WcfRestSample.IUserService1"> to <service name="WcfRestSample.UserService1"> . Thanks to User -Marvin Smitt in the comments

Upvotes: 1

Related Questions