Skip to main content

Posts

Showing posts with the label WCF

How to host wcf service in IIS using Tcp protocol

In this post I am going to show you how to host WCF service in IIS using TCP protocol.By default, the TCP WCF activation service is not enabled on your machine. You can follow these steps to enable the TCP activation for WCF services:
Go to Control Panel | Programs | Turn Windows features on or off. Expand the Microsoft .Net Framework 3.5.1 node on Windows 7 Check the checkbox for Windows Communication Foundation Non-HTTP Activation on Windows 7
Next,Create the IIS application to host the WCF application using TCP protocol.Follow these steps
Open run command and type inetmgrCreate a new application and point to physical folder.Select the DefaultAppPoll  and make sure your default app pool is a .NET 4.0.X application pool.Right-click on web application, select Manage Application | Advanced Settings, and then add net.tcp to Enabled Protocols.(Make sure you use all lowercase letters and separate it from the existing HTTP protocol with a comma)
Now the service is hosted in IIS using th…

Understand WebMessage Format

In this post I will show how how to use WebMessageBodyStyle parameter attribute in REST Service.WebMessageBodyStyle is an enumeration that specifies whether to wrap parameter and return values.WebMessageBodyStyle contains four member.

I have listed down all the members and there discription in following table.

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace RestLoginService { [ServiceContract] publicinterface ILoginService { [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] AuthResult Login(User user); } }

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using …

How to post array of hashes in wcf

In this post I will show you how to post json array hashes in wcf rest service.Before going to code snippet let's discuss some basic terminology.

What is Array of Hashes
Array of hashes is a data structure in which each record itself contains key/value pairs. Arrays of hashes are used less frequently than the other structures.Below is example of array of hashes
AoH = ( { husband => "barney", wife => "betty", son => "bamm bamm", }, { husband => "george", wife => "jane", son => "elroy", }, { husband => "homer", wife => "marge", son => "bart", }, ); JSON String

When we enclosed the json data into quote then we called this string is JSON string
var jsonString='[{ "x": 10, "y": 20, "z": 30 }, { "x": 12, "y"…

How to stream video in asp.net using WCF rest service

In this post I will show you how to stream video in asp.net using WCF rest service.The techniques used here is as we don't know the exact content lenght of the data so we used Content-Encoding transfer encoding allows a server to maintain an HTTP persistent connection for dynamically generated content. In this case the HTTP Content-Length header cannot be used to delimit the content and the next HTTP request/response, as the content size is as yet unknown. Chunked encoding has the benefit that it is not necessary to generate the full content before writing the header, as it allows streaming of content as chunks and explicitly signaling the end of the content, making the connection available for the next HTTP request/response..Open visual studio and create a simple wcf service.Add a new class and add following code into it

How to read incoming and outgoing message in wcf

In this post,I will show you how to read outgoing and incoming message in wcf.Create a new console application and add following assembly reference in the projectusing System.ServiceModel; using System.ServiceModel.Description;and paste following codeusing System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Description; namespace WCF_MessageInspector { [ServiceContract] publicinterface IHelloService { [OperationContract] string Greet(string name); } [ConsoleServiceBehavior] [ServiceBehavior] publicclass HelloService : IHelloService { publicstring Greet(string name) { return"Hello ," + name; } } class Program { staticvoid Main(string[] args) { string baseUrl = "http://localhost:12345"; ServiceHost host = new ServiceHost(typeof(HelloService), new Uri(b…

Calling cross domain wcf service using Jquery

In this post,I will show you how to call cross domain service using jquery.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.ServiceModel; using System.IO; using System.ServiceModel.Web; using System.Runtime.Serialization.Json; namespace CorssDomainService { [DataContract] publicclass Order { [DataMember] publicint ID { get; set; } [DataMember] public DateTime OrderDate { get; set; } [DataMember] publicstring Name { get; set; } } [ServiceContract] publicinterface IOrderService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "/", ResponseFormat = WebMessageFormat.Json )] string ProcessOrder(); } [ServiceBehavior] publicclass OrderService : IOrderService { [OperationBehavior] publicstring ProcessOrder() { …

How to create self signed certificate using makecert

In this post,I will show you how to create self signed certificate for wcf application.For this,I will used the utility makecert.MakeCert.exe allows you to (for test/dev purposes) generate both a trusted root certificate and a certificate signed by that trusted root certificate for encryption purposes (also for signature purposes.
Open visual studio command prompt (Admin mode) and type following command 
C:\cert>makecert -pe -n "CN=RootCA" -ss My -sr LocalMachine -a sha1 -sky signatu re -r "RootCA.cer" -pe Marks the generated private key as exportable. This allows the private key to be included in the certificate.-Specifies the subject's certificate name. This name must conform to the X.500 standard. The simplest method is to specify the name in double quotes, preceded by CN=; for example, "CN=myName".-ss Specifies the subject's certificate store name that stores the output certificate.sr >localmachine-a Specifies the signature algorithm…

How to upload/Download file from wcf service

In this post,I will show you how to stream (upload and download) data through wcfservice.
Create a new websiteRight click on the website and add new wcf service named FileStream.svcOpen the IFileStream.cs and add following codeusing System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.IO; [ServiceContract] publicinterface IFileStream { [OperationContract] string Upload(Stream inputStream); [OperationContract] Stream Download(string fileId); [OperationContract] string[] GetAvailableFiles(); }
Open FileStream.cs file and add following code inside it
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.IO; using System.Web; // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in cod…

How to:Create wcf application without using config file

In this post i am going to show you how to create wcf application without using config file. Let's start the tutorial.
Open visual studio and create a new console application Right click on the project and add reference to following assemblyusing System.ServiceModel;
Open Program.cs file and paste following code
[ServiceContract]     interface IService     {         [OperationContract]         string HelloWorld(string strMessage);     }     publicclass ServiceImp : IService     {         [OperationBehavior]         publicstring HelloWorld(string strMessage)         {             return ("Hello " + strMessage + "!");         }     }
Now Add following code inside main method

staticvoid Main(string[] args)         {             //Create a URI to serve as the base address             Uri httpUrl = new Uri("http://localhost:8090/MyService");             //Create ServiceHost             ServiceHost host = new ServiceHost(typeof(ServiceImp), httpUrl);����������…

How to consume WCF service using JQuery

Create an ASP.NET Web Application (WCF service)Add a new item, aspx page (Default.aspx)You now have an WCF-service with .svc as extension, and that service implements an Interface. It's in the interface we'll set the attributes needed to return JSONCreate a class, Student as below. The attributes "DataContract" and "DataMember" must be defined for the class and the properties that would be returned through the WCF service [DataContract] publicclass Student { [DataMember] publicint StudentId { get; set; } [DataMember] publicstring StudentName { get; set; } [DataMember] publicint Marks1 { get; set; } [DataMember] publicint Marks2 { get; set; } [DataMember] publicstring EmailAddress { get; set; } } Now add a reference to System.ServiceModel.Web this is for WebGet AttributeAdd the WebGet-attribute as specified below. The BodyStyle Bare means "Both requests and responses are not wrapped" - we really want nothing more than a true JSON-respo…