Click here to Skip to main content
15,887,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
am trying to invoke wcf rest service through url.. but its showing an error like

** I need JSON resoponce for mobile apps

400 error
CSS
Most likely causes:
•There might be a typing error in the address.
•If you clicked on a link, it may be out of date.




Here my code:


Iservice.cs

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


namespace SampleRestSample
{
    // 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 = "Book/{id}", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        List<Prasad> GetBookById(string id);
    }
    [DataContract]
    public class Prasad
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Age { get; set; }
    }

   
}


Service1.svc.cs

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

namespace LoginRestSample
{
    // 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 : SampleRestSample
    {
        List<Prasad> list = new List<Prasad>();
        public List<Prasad> GetBookById(string id)
        {
            try
            {
                Prasad cls = new Prasad();
                cls.Age = "24";
                cls.Name = "prasad";
                list.Add(cls);

            }
            catch
            {
                throw new FaultException("Something went wrong");
            }
            return list;
        }
    }
}


web.config

C#
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfRestSample.SampleRestSample">
        <endpoint address="" behaviorConfiguration="restfulBehavior"
          binding="webHttpBinding" bindingConfiguration="" contract="WcfRestSample.ISampleRestSample" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/SampleRestSample" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restfulBehavior">
          <webHttp automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

</configuration>



any possibilities to handle this...

Thank you in advance.
Posted
Updated 26-Oct-13 22:10pm
v2

Could be here: http://localhost/SampleRestSample
I suppose it does not use port 80, and hence you must indicate the port number, e.g. http://localhost:12345/SampleRestSample
 
Share this answer
 
Comments
Lokesh Kondapalli 28-Oct-13 5:21am    
can i run this restfull service on "https"
Lokesh Kondapalli 28-Oct-13 5:29am    
its working fine with out base address too.... but now i need to run this rest service over https.... its showing some binding errors..
Bernhard Hiller 28-Oct-13 5:39am    
That is a TOTALLY different problem than you described above.
Get your certificates, and install them properly - that is not at all an easy task!
Lokesh Kondapalli 28-Oct-13 5:53am    
yes am already running some of my wcf services over https... and now trying for this...

Write this upon your service class definition...

C#
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : SampleRestSample
    {
     //...
     //Your code...
     //...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900