I have struggled a lot spent quiet a lot of time but unable to make it work, after spending hours now I am able to see the metadata but unable to successfully call the operation. below are the steps and code.
Now code.
Service contract and DataContract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace JsonWCFTest
{
[ServiceContract]
public interface IJsonService
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "data/{id}")]
Product GetProduct(string id);
}
[DataContract(Name = "product")]
public class Product
{
[DataMember(Name = "id")]
public string Id { get; set; }
}
}
Service.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace JsonWCFTest
{
public class JsonService:IJsonService
{
public Product GetProduct(string id)
{
return new Product {Id = " you have entered " + id};
}
}
}
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="service.svc" service="JsonWCFTest.JsonService"/>
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="JsonWCFTest.JsonService" behaviorConfiguration="jsonTestServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost" />
</baseAddresses>
</host>
<endpoint address="jsonTestEndPoint" behaviorConfiguration="jsonTestEndPointBehavior"
binding="webHttpBinding" contract="JsonWCFTest.IJsonService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="jsonTestServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonTestEndPointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
when I am using this url
http://localhost/service.svc/data/1
it is giving me this below error in internet explorer
Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /service.svc/data/1
You are connecting to the wrong address. With the config file you have set up you should connect to
http://localhost/jsonTestEndPoint/data/1
You need to use BaseAddress
+ EndpointAddress
+ FunctionAddress
to get the full url.
I may be a little rusty, if the above address did not work, then the address will be
http://localhost/service.svc/jsonTestEndPoint/data/1