Home > WCF > Expose WCF service with nettcp and JSON protocols

Expose WCF service with nettcp and JSON protocols

Service Exposure

public interface ICommandService
[WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = “HelloPing/{value}”)]
string HelloPing(string value);

[WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = “GetOrderDetail/{OrderId}”)]
List<OrderDetail>GetOrderDetail(string OrderId);

bool GetDataUsingDataContract(string XMLData);

// TODO: Add your service operations here

Configure your service with this configuration

<service name=”WCTServiceTest.CommandService”>
<add baseAddress = “http://localhost:8733/Design_Time_Addresses/WCTServiceTest/CommandService/&#8221; />
<add baseAddress = “net.tcp://localhost:8730/Design_Time_Addresses/WCTServiceTest/CommandService/” />

<!– Service Endpoints –>
<!– Unless fully qualified, address is relative to base address supplied above –>
<endpoint address=”” binding=”basicHttpBinding” contract=”WCTServiceTest.ICommandService”>
<dns value=”localhost”/>
<endpoint address=”” binding=”netTcpBinding” contract=”WCTServiceTest.ICommandService”>
<dns value=”localhost”/>
<endpoint address=”JSON” binding=”webHttpBinding” contract=”WCTServiceTest.ICommandService” bindingConfiguration=”webHttpBindingWithJson” behaviorConfiguration=”Web”>
<dns value=”localhost”/>
<!– Metadata Endpoints –>
<!– The Metadata Exchange endpoint is used by the service to describe itself to clients. –>
<!– This endpoint does not use a secure binding and should be secured or removed before deployment –>
<endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange”/>
<binding name=”webHttpBindingWithJson” crossDomainScriptAccessEnabled=”true” />
<serviceMetadata httpGetEnabled=”True” httpsGetEnabled=”True”/>
<serviceDebug includeExceptionDetailInFaults=”False” />
<behavior name=”Web”>
<webHttp defaultBodyStyle=”Bare” />
<behavior name=”EndPointBehavior”>

use below C# code to call them in your code
NetTcp Calling

void CallNetTCPService()
System.ServiceModel.NetTcpBinding binding = new NetTcpBinding();
EndpointAddress address = new EndpointAddress(“net.tcp://localhost:8730/Design_Time_Addresses/WCTServiceTest/CommandService”);

ChannelFactory channelFactory = new System.ServiceModel.ChannelFactory(binding, address);
ICommandService _clientProxy = channelFactory.CreateChannel();
string x = _clientProxy.HelloPing(“SArvesh”);
List order = _clientProxy.GetOrderDetail(“100”);

JSON Calling (using System.Runtime.Serialization.Json;)

protected void CallJSONWCFService()
//Creating the object of WebClient class
//Creating the object of WebClient class
WebClient objClient = new WebClient();
//Calling GetJsonMessage() method to get result from WCF service using JSON end point
byte[] jsonData = objClient.DownloadData(new Uri(“http://localhost:8733/Design_Time_Addresses/WCTServiceTest/CommandService/JSON/GetOrderDetail/1&#8221;));
//Creating the object of Stream class
Stream stream = new MemoryStream(jsonData);
//Creating the object of DataContractJsonSerializer class
DataContractJsonSerializer objDCJS = new DataContractJsonSerializer(typeof(List<OrderDetail>));
//string data = objDCJS.ReadObject(stream).ToString();
List<OrderDetail> data = (List<OrderDetail>)objDCJS.ReadObject(stream);
catch (Exception ex)

Categories: WCF
  1. No comments yet.
  1. No trackbacks yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: