1

this post is off the back of my last question (thought i'd start a new question).

i am trying to create and test a simple (simple to you not me lol) WCF web service that outputs JSON. I beleive (with help) the application is ok but i need to test it and i a not sure how.

I'll attach my code below but here is what is happening, if i run the application it loads http://localhost:52002/ and i get the welcome to ASP.NET page, if run the application while i am in my .svc it loads http://localhost:52002/MyTestService.svc and i get the page:

You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

svcutil.exe http://localhost:52002/MyTestService.svc?wsdl

I have been told i can http://localhost:52002/MyTestService.svc/GetResults to see the JSON output (see code below) but all i get is:

The webpage cannot be found

Any suggestions would be great, i apologies now for being abit new to all this and please ask you to maybe spell things out abit more then you normally would lol

Here is my code and tanks very much

Person.cs

[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;
    }
}

MyTestServices.svc.cs

[ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode =  `AspNetCompatibilityRequirementsMode.Allowed)]`
    public class TestService
    {
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]

    public List<Person> GetResults()
    {
        List<Person> results = new List<Person>();
        results.Add(new Person("Peyton", "Manning", 35));
        results.Add(new Person("Drew", "Brees", 31));
        results.Add(new Person("Tony", "Romo", 29));

        return results;
    }

MyTestService.svc

<%@ ServiceHost Language="C#"
    Service="TestService.TestService"
    Factory="System.ServiceModel.Activation.ServiceHostFactory" %>

Web.Config

<configuration>
  <connectionStrings>
    <add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" />
  </connectionStrings>

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

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>
    <bindings />
    <client />
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>
Community
  • 1
  • 1
domsbrown
  • 75
  • 2
  • 10

3 Answers3

2

I always use fiddler to debug the request. With the new Web Api for WCF you can set the accept header for the client to application/JSON and the response will be formatted as JSON.

Carlo Kuip
  • 311
  • 2
  • 3
  • Here is the link to the fiddler documentation.http://www.fiddler2.com/Fiddler/help/ – Jamie Oct 21 '11 at 17:59
  • Sorry about that. To be able to inspect what http packets are sent back and forth I use a HTTP debugger called fiddler, it acts a proxy and therefore transparent to your app. Cool thing, it allows to alter requests and re-send them to your service. This allows you to change e.g. the accept header through which your client informs the server of the accepted return type, hence we like JSON. The doc, tells all about setup and usage. Thnx Jamie for posting the link. – Carlo Kuip Oct 21 '11 at 19:37
1

I do not see your interface, but you are missing some stuff if you want it to be rest.

//Add this to your method    
[WebGet(UriTemplate="/Results")]
public List<Person> GetResults()
{
    List<Person> results = new List<Person>();
    results.Add(new Person("Peyton", "Manning", 35));
    results.Add(new Person("Drew", "Brees", 31));
    results.Add(new Person("Tony", "Romo", 29));

    return results;
}

Now you should be able to call it using http://localhost:52002/MyTestService.svc/Results. Just change the uri tempalte to what you want it to be.

You may also need to change the factory to be the WebServiceHostFactory.

<%@ ServiceHost Service="TestService.MyTestService"
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Jamie
  • 3,094
  • 1
  • 18
  • 28
  • thanks for the reply. I removed the interface when i was following a blog that i posted before. It appeared to be the place for the ServiceContract and OperationContract which is in my MyTestService.svc.cs file, do i have to move that code to an interface file and have it inherit from that? I've done your changes, the mainpage tells me i have no end points but when i add /Results i get the following output – domsbrown Oct 21 '11 at 20:22
  • - - 35 Peyton Manning - 31 Drew Brees - 29 Tony Romo – domsbrown Oct 21 '11 at 20:23
  • That looks like the answer that you want. It is a REST result in XML. Do you want it in JSON? – Jamie Oct 21 '11 at 23:19
  • http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webmessageformat.aspx – Jamie Oct 23 '11 at 14:06
  • The above link tells you how. – Jamie Oct 23 '11 at 14:06
  • thanks alot Jamie!! i've managed to reproduce what adrift has shown me and got that working, i also now have your example for producing REST results in XML. thanks again!! – domsbrown Oct 24 '11 at 08:46
1

I am not a WCF expert, so I cannot tell you what is wrong with your current application, but I can give you instructions for setting up a very simple example using your code, which will return a result in Fiddler.

  1. Create a new ASP.NET Empty Web Application
  2. Add a code file with the following:

Normally I wouldn't put everything in one file, but for simplicity...

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

namespace WebApplication1
{
    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        List<Person> GetResults();
    }

    public class TestService : ITestService
    {
        public List<Person> GetResults()
        {
            List<Person> results = new List<Person>();
            results.Add(new Person("Peyton", "Manning", 35));
            results.Add(new Person("Drew", "Brees", 31));
            results.Add(new Person("Tony", "Romo", 29));

            return results;
        }
    }

    [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;
        }
    }
}

Update the web.config file:

<configuration>

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

    <system.serviceModel>

        <serviceHostingEnvironment>
            <serviceActivations>
                <add relativeAddress="test.svc"
                     service="WebApplication1.TestService"
                     factory="System.ServiceModel.Activation.WebServiceHostFactory" />
            </serviceActivations>
        </serviceHostingEnvironment>

    </system.serviceModel>

</configuration>

You don't need a .svc file, so start the application, making note of the root url used by the ASP.NET Development Server. On the Request Builder tab of fiddler enter your url:

enter image description here

Click the Execute button and you should see your service results:

enter image description here

Hopefully you can use this to figure out what you need to update on your service to get it running. Good luck!

Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127