Expose WCF Service as SOAP and REST
1) Create WCF Service application
2) Add interface file+Class file+ .svc file
Interface file: interface1.cs
Class file: class1.cs
Service file: Service2.svc
3) interface1.cs
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
using System.ServiceModel.Web;
using System.Runtime.Serialization;
namespace WcfService1
{
[ServiceContract]
interface Interface1
{
[WebGet]
[OperationContract]
Person GetPerson();
}
[DataContract]
public class Person
{
[DataMember] public int id{get;set;}
[DataMember] public String Name{get;set;}
[DataMember] public DateTime Dob{get;set;}
}
}
interface Interface1
{
[WebGet]
[OperationContract]
Person GetPerson();
}
[DataContract]
public class Person
{
[DataMember] public int id{get;set;}
[DataMember] public String Name{get;set;}
[DataMember] public DateTime Dob{get;set;}
}
}
}
4) Class1.cs
namespace WcfService1
{
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class Class1:Interface1
{
public Person GetPerson()
{
return new Person { id = 102, Dob = DateTime.Now.AddYears(-40), Name = "Peter"};
}
}
}
{
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class Class1:Interface1
{
public Person GetPerson()
{
return new Person { id = 102, Dob = DateTime.Now.AddYears(-40), Name = "Peter"};
}
}
}
5) Service2.svc
<%@
ServiceHost
Service="WcfService1.Class1"
%>
ServiceHost
Service="WcfService1.Class1"
%>
6) In Web.config
<system.serviceModel>
<services>
<service name="WcfService1.Class1">
<endpoint binding="webHttpBinding"
contract="WcfService1.Interface1"
behaviorConfiguration="restBehaviour"
<services>
<service name="WcfService1.Class1">
<endpoint binding="webHttpBinding"
contract="WcfService1.Interface1"
behaviorConfiguration="restBehaviour"
address="/web"
></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehaviour">
<webHttp helpEnabled="true" faultExceptionEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehaviour">
<webHttp helpEnabled="true" faultExceptionEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
6) For SOAP Clients
Just run http://localhost:port/Service2.svc
For Rest Services
http://localhost:port/Service2.svc/web
User gets help page and list of operations, in this case GetPerson
No comments:
Post a Comment