wcf rest service 400 error : There might be a typing error in the address
- by Lokesh Kondapalli
I am trying to invoke wcf rest service from url but its showing error like this Error :
Most likely causes:
•There might be a typing error in the address.
•If you clicked on a link, it may be out of date.
** I need JSON responce
Here my code :
Iservice.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 SampleRestSample
{
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
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);
//int bookId = Convert.ToInt32(id);
//using (SampleDbEntities entities = new SampleDbEntities())
//{
// return entities.Books.SingleOrDefault(book => book.ID == bookId);
//}
}
catch
{
throw new FaultException("Something went wrong");
}
return list;
}
}
}
web.config
<?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 solutions?
Thank you in advance.