inforakesha

A fine WordPress.com site

WCF REST

Leave a comment

WCF REST STEP BY STEP

 

1.Installed a WCF REST Template from VS extension.

 

2.Create a Interface like below:

 

[ServiceContract]

	publicinterfaceIRestServiceDemo
	{
	[OperationContract]
	[WebGet(UriTemplate="xml")]
	stringXMLData();
	}
-Here you can use WebGet or WebInvoke for more options.

3.Include the using System.ServiceModel; in this [ServiceContract] and

[operation contract], etc classes are.

 

4.Include using System.ServiceModel.Web; In this WebGet and WebInvoke classes are.

 

5. Implement the Interface in a class as

 

public classRestServiceDemo:IRestServiceDemo

	{
	#region"IRestServiceDemoImplementationhere"

	publicStringXMLData()
	{
	return"YouRequestedproduct";
	}

	#endregion
	}

 

6.Create if not Global.asax file

 

privatevoid RegisterRoutes()

	{

	RouteTable.Routes.Add(newServiceRoute("RestServiceDemo",newWebServiceHostFactory(),
	typeof(RestServiceDemo)));
	}
-here ou have to add the service class in RouteTable.Routes.Add....
7.	<?xmlversion="1.0"?>
<configuration>

<connectionStrings>
<addname="SchoolAdministrationv1ConnectionString"connectionString="DataSource=.;InitialCatalog=SchoolAdministrationv1;PersistSecurityInfo=True;UserID=sa;Password=sa123"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilationdebug="true"targetFramework="4.0"/>
</system.web>

<system.webServer>
<!--<modulesrunAllManagedModulesForAllRequests="true">
<addname="UrlRoutingModule"type="System.Web.Routing.UrlRoutingModule,
System.Web,

Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"

/>
</modules>-->
</system.webServer>

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behaviorname="">
<serviceMetadatahttpGetEnabled="true"/>
<serviceDebugincludeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironmentaspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
ConfiguretheWCFRESTservicebaseaddressviatheglobal.asax.csfileandthedefaultendpoint
viatheattributesonthe<standardEndpoint>elementbelow
-->
<standardEndpointname=""helpEnabled="true"automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>

</configuration>

  1. Hosting

if GET

WebRequestreq=WebRequest.Create(@"http://localhost:1422/RestServiceDemo/xml");

req.Method="GET";

HttpWebResponseresp=req.GetResponse()asHttpWebResponse;
if(resp.StatusCode==HttpStatusCode.OK)
{
using(StreamrespStream=resp.GetResponseStream())
{
StreamReaderreader=newStreamReader(respStream,Encoding.UTF8);
Console.WriteLine(reader.ReadToEnd());
}
Console.WriteLine("Success");
}
else
{
Console.WriteLine(string.Format("StatusCode:{0},StatusDescription:{1}",resp.StatusCode,resp.StatusDescription));
}

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s