Rob Angelier
Rob Angelier

Reputation: 2333

WCF JSON implementation

I'm trying to implement a very simple WCF service that returns JSON. I'm trying for 6 hours now and it still doesn't work. I hope you can help me out with this.

Person

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

namespace Tcf.AtX.Services
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }
    }
}

Service Contract

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

namespace Tcf.AtX.Services
{
    [ServiceContract]
    public interface IBroadcastService
    {
        /// <summary>
        /// Broadcasts the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(Method="GET", ResponseFormat= WebMessageFormat.Json)]
        Person Broadcast(string message);
    }
}

Service Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tcf.AtX.Broadcasting;

namespace Tcf.AtX.Services
{
    public class BroadcastService : IBroadcastService
    {
        /// <summary>
        /// Broadcasts the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Person Broadcast(string message)
        {
            return new Person() { Name = message };
        }
    }
}

Configuration

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

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Tcf.AtX.Services.BroadcastService">
        <endpoint address="" binding="webHttpBinding" contract="Tcf.AtX.Services.IBroadcastService" behaviorConfiguration="json"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/Tcf.AtX.Services/BroadcastService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="json">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

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

The problem is that i don't see the service inside the test client, so i can't test my method. I also wrote a test client myself, but i can't create an instance of my service when i reference it to my project.

Can someone please explain to me what i'm doing wrong?

Best regards,

Rob

Upvotes: 1

Views: 356

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87228

The test client doesn't work for non-SOAP endpoints (i.e., the one you have, which uses the WebHttpBinding). Try simply creating a program that tries to call the operation you have, something like the code below

WebClient c = new WebClient();
Console.WriteLine(
    c.DownloadString(
        "http://localhost:8732/Design_Time_Addresses/Tcf.AtX.Services/Broadcast?message=MyMessage"));

One more thing, you'll need to change the [WebInvoke(Method="GET")] attribute to [WebGet].

Upvotes: 3

Related Questions