Error: Cannot obtain Metadata; using WCF Test client, C#, and trying to implement webhttpbinding and json

user717236 picture user717236 · Jun 12, 2013 · Viewed 8.5k times · Source

I'm getting the fabled Error: Cannot obtain Metadata from... message, particularly http://localhost:1640/Service1.svc I've scoured stackoverflow and Google and, so far, none of it has helped. I created a new product to dumb it down and it's still not working. So I am here asking for help.

I'm trying to setup a WCF Service that uses JSON, which means I need to use webhttpbinding, in C#. When I use the WCF Test Client, I get the aforementioned metadata error. I'm using Visual Studio Express 2010 and the target framework. I've been at this for two days and cannot understand why or what is the problem.

I would appreciate any help. Thank you.

Here is my web.config file:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="WcfService4.Service1"
        behaviorConfiguration="jsonRestDefault">

        <endpoint 
              name="jsonRestEndpoint"
              behaviorConfiguration="RESTFriendly"
              binding="webHttpBinding"
              contract="IService1"
              address="http://localhost:1640/Service1.svc"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="jsonRestDefault">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="RESTFriendly">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Here is the IService1.cs file:

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

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET", 
            UriTemplate = "players",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare)]
        List<Person> GetPlayers();
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        [DataMember]
        public int Age { get; set; }

        public Person(string firstName, string lastName, int age)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }
    }
}

Here is the Service1.svc.cs file:

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

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public List<Person> GetPlayers()
        {
            List<Person> players = new List<Person>();
            players.Add(new Person ( "Peyton", "Manning", 35 ) );
            players.Add(new Person ( "Drew", "Brees", 31 ) );
            players.Add(new Person ( "Brett", "Favre", 38 ) );

            return players;
        }
    }
}

Answer

bruno.bologna picture bruno.bologna · Jun 12, 2013

Take a look at this link

It states the following:

WebHttpBinding is a REST-based binding - REST does not expose metadata like WSDL/XSD contrary to SOAP.