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

Expose WCF service with nettcp and JSON protocols

Service Exposure

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

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

[OperationContract]
bool GetDataUsingDataContract(string XMLData);

// TODO: Add your service operations here
}

Configure your service with this configuration

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

</baseAddresses>
</host>
<!– Service Endpoints –>
<!– Unless fully qualified, address is relative to base address supplied above –>
<endpoint address=”” binding=”basicHttpBinding” contract=”WCTServiceTest.ICommandService”>
<identity>
<dns value=”localhost”/>
</identity>
</endpoint>
<endpoint address=”” binding=”netTcpBinding” contract=”WCTServiceTest.ICommandService”>
<identity>
<dns value=”localhost”/>
</identity>
</endpoint>
<endpoint address=”JSON” binding=”webHttpBinding” contract=”WCTServiceTest.ICommandService” bindingConfiguration=”webHttpBindingWithJson” behaviorConfiguration=”Web”>
<identity>
<dns value=”localhost”/>
</identity>
</endpoint>
<!– 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”/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name=”webHttpBindingWithJson” crossDomainScriptAccessEnabled=”true” />
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled=”True” httpsGetEnabled=”True”/>
<serviceDebug includeExceptionDetailInFaults=”False” />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name=”Web”>
<webHttp defaultBodyStyle=”Bare” />
</behavior>
<behavior name=”EndPointBehavior”>
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>

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()
{
try
{
//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)
{
}
}

Advertisements
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: