Monday 14 October 2013

Message Security using Certificate Client Credential in WCF

Message Security using Certificate Client Credential in WCF

In this article I am explaining how to achieve message security using certificate client credential. In my last article I described the message security using user name client credential.

Following are the steps to implement message security using Certificate Client Credential:-

Step 1:-

Go to the IIS Server by typing inetmgr in run

Step 2:-

Click on server certificate



Figure 1

Step 3:-

After opening the server certificate on right panel select the create self-signed certificate



Figure 2

Now certificate creation window will be opened. Give a proper name to the certificate and click ok.


Tuesday 8 October 2013

Duplex Message Exchange Pattern in WCF with Example

Duplex Message Exchange Pattern in WCF

Duplex message exchange pattern is a way in WCF in which client and service both can send messages to each other independently. Duplex message exchange pattern also known as callback.

As both client and service need to be connected to exchange messages this patter is quite complicated and slower than other message exchange pattern.
To implement the duplex we have to create the separate interface and we have to create method which can be either request-reply or one way type.

Following are the steps to implement duplex service in wcf:-

Step 1:-

Open the visual studio->File->New->Website->WcfService

Step 2:-

Create interface which you use to call back from service to the client:-

[ServiceContract]
public interface IDuplexCheck
{
    [OperationContract(IsOneWay=true)]
    void showstatus(string res);

}

Step 3:-

Create the interface for the service in which you define your operation:-

[ServiceContract(CallbackContract=typeof(IDuplexCheck))]

public interface IService
{

    //IsOneWay=False represents the duplex communication
    [OperationContract(IsOneWay=false)]
   
    void dataupdated();
}

Tuesday 10 September 2013

Instance Management in WCF

Instance Management in WCF

Instance management in WCF is the concept in which we can understand the instance creation of service when client request for the service. We have to set the InstanceContextMode in ServiceBehavior. There are three type of instance management available in WCF and these are as follows:-

Per Call

In per call instance management every time we hit the service means when we call method it will simply create the new instance of service and execute the method and give the result to the client. After that it will simply dispose the instance.

There are no overhead on the server in this type instance management as resources deallocate as the user request complete. At every method call new instance will be created in per call.

For example:-

Interface Code is:-

[ServiceContract]
public interface IService
{

          [OperationContract]
          int GetData();

         
}

Service Class code is:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class Service : IService
{
    int x = 0;

          public int GetData()
          {
        x++;
        return x;

          }

         
}

Web config code:-

Add this code inside the System.servicemodel tag

<services>
      <service name="Service">
        <endpoint address="" binding="basicHttpBinding" contract="IService"/>
      </service>
</services>

Sunday 21 July 2013

Message Security in WCF using username client credential

Message Security in WCF

There are two types of security in WCF. One is the security of Data and second is the security of medium through which message travel.

When we talk about the security of data then it is achieved by message security and if we talk about the security of medium through which message travel which is protocol security can be achieved by transport level security.

In this article I defined how to achieve message level security. There of different type of client credential and using this client credential we achieve message security. I am using wsHttpBinding to achieve message level security

Type of client Credential in message security
1.     None
2.     Windows
3.     Username
4.     Certificate
5.     Issued token

In this example I am using client credential username.

Following are the steps to implement the message security using client credential username

Step 1:-

Create a class and inherit usernamepasswordvalidator class in it. This class will be found on System.IdentityModel.Selectors and override the method validate and verify the username and password.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IdentityModel.Selectors;
using System.ServiceModel;

public class Credentioal:UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName == "isha" && password == "isha123")
        { }
        else
        {

            throw new FaultException("Wrong userid and pwd");
        }
    }
}

Step 2

Wednesday 27 March 2013

WCF Lesson 4:-Fault Contract in WCF


Fault Contract in WCF

With the help of fault contract we can describe how to exchange error information from service to client. If we get any error in the service then we can pass this error information from service to client with the help of Fault contract.

For Example:-

Everybody is familiar with the error DivideByZero. It occurs when we try to divide a number with zero, It occurs this exception. Same example I am using here.

I create Error class which will be used to pass to the client at the time of error occur:-

[DataContract]
public class Error
{
    [DataMember]
    public string ErrorMessage;

    [DataMember]
    public string ErrorMethod;

}

WCF Lesson 3:-Message Contract in WCF


Message Contract in WCF

Earlier we discuss two contract i.e. service contract and data contract. Now in this article I will discuss message contract.

In message contract data is sent in the format of SOAP message which in the form of header and body. With the help of message header we can implement security by passing credential in header part.

For Example:-

Create wcf service and add class to implement message contract.

[MessageContract]
public class EmployeeRecord
{
    [MessageHeader]
    public string token;

   
    [MessageBodyMember]
    public string name;
  
   [MessageBodyMember]
    public int empid;
}

Friday 15 March 2013

WCF Lesson 2:- Data Contract in WCF



for any query you can mail me at Malhotra.isha3388@gmail.com.
you can also mail me your wcf problem which you face in your project

Data Contract in WCF

In the previous article I described basic Idea of WCF and Service Contract. Now I am going to describe your data contract.
As we know that WCF exchange information in XML format. Data contract defines which data type is serialized (convert into Xml). Primitive data types like int, string etc. serialized already as they defined in XSD (Xml Schema Definition). But custom data types like classes are not.
Wcf uses serialization engine i.e. Data Contract Serializer by default to serialize and DE serialize the data.
We can use DataContract attribute to serialize the class and DataMember attribute to Serialize its member.

For Example:-

In following example I simply create a class EmpData and declare two members in it. In service I create a simple method which returns the object of EmpData with some information.