Thursday 26 May 2016

Message Contract in WCF? What is MessageContract in WCF?



DataContract has limited control over SOAP message and all the control related to content inside the body of the SOAP message. But sometime scenario comes, when we need more control over SOAP message. So in that scenario we can use Message Contract.

Message contract describe the structure of SOAP message sent to and from the service and enable you to inspect and control most of the details in the SOAP header and body.

Consider a scenario; an authentication code is required to access our service. In this case functionality provided by our service will same but authentication code validity is additional pre-requisite. So in that case, SOAP message header is most reasonable place to store the authentication code while calling the service method.

Example:-
[MessageContract]
public class StudentDetails
{
    [MessageHeader]
    public string EmpId;
    [MessageBodyMember]
    public string StudentName;
    [MessageBodyMember]
    public string Age;
    [MessageBodyMember]
    public string Course;
    [MessageBodyMember]
    public string Address;
}

Note:- If both type of Contract is present in service , then service operation will accept only message contract.

What is DataContact in WCF? Why use DataContract in WCF?



DataContract is a formal agreement between a service and client that abstractly describe the data to be exchanged.
DataContract are used to describe the data types used by service.
DataContract can be explicit or implicit.

It is not necessary to use DataContract if you are using simple/implicit data type i.e. int, string .
DataContract is necessary when you want to use complex data type.

Example:-
[DataContract]
public class CompositeType
{
          bool boolValue = true;
          string stringValue = "Hello ";

          [DataMember]
          public bool BoolValue
          {
                   get { return boolValue; }
                   set { boolValue = value; }
          }

          [DataMember]
          public string StringValue
          {
                   get { return stringValue; }
                   set { stringValue = value; }
          }
}

In the above example if you did not mention DataMember attribute on Property then it will throw error at compile time.

Wednesday 25 May 2016

What is Throttling in WCF?



WCF throttling provides properties that you can use to limit how many instances or sessions are created at the application level.

Throttling is a behavior which has three attributes:-
1. maxConcurrentCalls :- limits total no of calls that currently be in progress across  all   service instance.Default value of maxConcurrentCalls is 16.
2. maxConcurrentInstances :-  limits the number of instanceContext objects that execute at one time across a serviceHost. Default value is Int32.Max i.e. 2147483647.
3. maxConcurrentSessions :- limits the no of sessions a serviceHost Object can accept. Default value is 10.

Throttling properties can be configured either in config file or programmatically.

Configuration File:-
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling
maxConcurrentCalls="10"
maxConcurrentInstances="100"
maxConcurrentSessions="10"/>
        
</behavior>
</serviceBehaviors>
</behaviors>

Programmatically:-

ServiceHost host = new ServiceHost(typeof(MyService));
           ServiceThrottlingBehavior throttle
 = host.Description.Behaviors.Find();
            if (throttle == null)
            {
                throttle = new ServiceThrottlingBehavior();
                throttle.MaxConcurrentCalls = 100;
                throttle.MaxConcurrentSessions = 20;
                throttle.MaxConcurrentInstances = 100;
                host.Description.Behaviors.Add(throttle);
            }

            host.Open();

Thursday 5 May 2016

Property Vs Indexer in c#


Property
Indexer
Identified by its name.
Identified by its signature.
Accessed through a simple name or a member access
Accessed through an element access.
Can be static or instance member
Must be an instance member
A Get accessor of a property has no parameters
A Get Accessor of an indexer has the same formal parameter list as the indexer
A Set accessor of a property contains implicit value parameter.
A Set accessor of an indexer  has the same formal parameter list as indexer, in addition to the value parameter
 

Refernce taken  from MSDN..