Sunday, January 26, 2014

Lambdas and Delegates

Anonymous Methods in Action





Understanding Lambda Expressions














Assigning a Lambda to a Delegate
 

Handling Empty Parameters

Delegates that don't accept any parameters can be handled using lambdas:

Delegates in .NET

The .NET framework provides several different delegates that provide flexible options:
  1. Action - Accepts a single parameter and returns no value
  2. Func - Accepts a single parameter and return a value of type TResult

Using Action

Action can be used to call a method that accepts a single parameter of type T:












Using Func

Func supports a single parameter (T) and returns a value (TResult):











Summary

  • Lambdas provide a way to define inline methods using a concise syntax
  • The .NET Framework provides several built-in delegate types such as:
      • Action
      • Func
Please find below link of source code providing example.

What is meant by “N Tier”?

Some Number of Tiers or Layers

      1.      Logical and sometimes Physical separation
2.      May be deployed separately
3.      May be developed separately

Tiers and Layers
  1.     Terms often used interchangeably
  2.      Layer can be applied specifically to the logical structure of the application
  3.     Tier can be applied specifically to the physical structure of the system infrastructure

Logical vs. Physical Separation
                        Logical separation refers to: Code organization
                        Namespaces, classes, folders, and assemblies
                        A software design pattern
                         
                        Physical separation refers to: Infrastructure organization
                        Separate physical processes, machines, devices
                        A software architecture

N Tier Diagram

Evolution of N-Tier Applications

























Evolution of N-Tier Applications


















Evolution of N-Tier Applications












Evolution of N-Tier Applications


Evolution of N-Tier Applications















Evolution of N-Tier Applications














Evolution of N-Tier Applications













Benefits of N-Tier Design
  • Improved Productivity via Reuse
  • Improved Productivity via Team Segmentation
  • Improved Maintainability
  • Looser Coupling
  •   More Physical Deployment Options

Drawbacks and Risks of N-Tier Design
  •   Reduced Performance, especially when physically separated
  •     More Complex Design
  •     More Complex Deployment

Thursday, January 23, 2014

How does MSMQ work?

ChannelFactory over Proxy class in WCF

When to use a proxy?

If you have a service that you know is going to be used by several applications or is generic enough to be used in several places, you’ll want to use the proxy classes.

When to use ChannelFactory?

ChannelFactory class is used to construct a channel between the client and the service without the need of a proxy. In some cases, you may have a service that is tightly bound to the client application. In such a case, you can reference the Interface DLL directly and use ChannelFactory to call your methods using that.

When to use a ChannelFactory over Proxy class

Use of DLL is helpful if the client code is under you control and
  • You’d like to share more than just the service contract with the client such as some utility methods associated with entities.
  • Make the client & the service code more tightly bounded.
  • If you know that your entities will not change much and the client code is less, then a DLL would work better than a proxy.
Proxies have several restrictions such as:
  • They need to have gets and sets
  • Contractors can’t be exposed
  • Methods other than the service contract cannot be exposed
  • Repetition of code
  • Every time that we add/modify a service contract/data contract/message contract we need to re-generate the proxy for the client.

So if you are designing a connected system and using WCF for that, then you can use a shared dll instead of a proxy.
Remember: sharing more than just the service contract with the client goes against the SOA fundamental – loose coupling between the consumers and services.

Saturday, August 31, 2013

Disable a button control during postback


Button disable technique for full .NET postbacks. So, this example is for all of you searching for a non-AJAX solution.
The trick is to use the OnClientClick and UseSubmitBehavior properties of the button control. There are other methods, involving code on the server side to add attributes, but I think the simplicity of doing it this way is much more attractive:
<asp:Button runat="server" ID="BtnSubmit" 
  OnClientClick="this.disabled = true; this.value = 'Submitting...';" 
  UseSubmitBehavior="false" 
  OnClick="BtnSubmit_Click" 
  Text="Submit Me!" />
OnClientClick allows you to add client side OnClick script. In this case, the JavaScript will disable the button element and change its text value to a progress message. When the postback completes, the newly rendered page will revert the button back its initial state without any additional work.
The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior. In this case, the code it injects would be:
__doPostBack('BtnSubmit','')
This is added to the end of our OnClientClick code, giving us this rendered HTML:
<input type="button" name="BtnSubmit" 
  onclick="this.disabled = true; this.value = 'Submitting...';__doPostBack('BtnSubmit','')"
  value="Submit Me!" id="BtnSubmit" />
This gives a nice button disable effect and processing text, while the postback completes.

The above code does not work on the IE8 browse. So; here we have to implement Disable a button control during postback some different way.

Here we are adding one dummy button on the page in div and make that div display style none. onclick on the btnsubmit we are calling one click side method which disable the btnsubmit and dummy button call the server side event of our original submit button. Below is code for that.

JavaScript Code:

<script> function disableButton() { $("BtnOk").disabled(); $("BtnDummyOk").disabled(); } </script>

HTML Code:


  <p>
        <asp:Button ID="BtnOk" runat="server" Text="Ok"  OnClientClick="disableButton();" />
    </p>

    <div style="display: none;">
        <asp:Button ID="BtnDummyOk" runat="server" Text="Ok"  OnClick="BtnOk_Click"/>
    </div>

It will solve our purpose of disable button and call server side event of the button. Happy Coding !!!! :)





Tuesday, August 6, 2013

How to: Host WCF in a Windows Service Using TCP

Summary of Steps

  • Step 1: Create a WCF Service
  • Step 2: Configure the WCF Endpoints to Use TCP and Set the Base Address
  • Step 3: Create a Windows Service
  • Step 4: Add the Service Installers to the Windows Service
  • Step 5: Modify the Windows Service to Host the WCF Service
  • Step 6: Install the Windows Service
  • Step 7: Create a Windows Forms Test Client Application
  • Step 8: Add a WCF Service Reference to the Client
  • Step 9: Test the Client and WCF Service

Step 1: Create a WCF Service

In this step, you create a WCF service to test hosting in a Windows service.
  1. In Visual Studio, click File, click New, and then click Project.
  2. In the Add New Project dialog box, in the Templates section, select WCF Service Library.
  3. In the Add New Project dialog box, click OK to create the WCF Service Library project WcfServiceLibrary1.

Step 2: Configure the WCF Endpoints to Use TCP and Set the Base Address

In this step, you modify the WCF configuration so that the endpoints use TCP instead of the default Hypertext Transfer Protocol (HTTP). You then set the base address for your service. Finally, you set httpGetEnabled to false, since you will be running under TCP.
  1. Right-click the App.config file of the WCF Service Library project and then click Edit WCF Configuration.
    If you do not see the Edit WCF Configuration option, on the Tools menu, click WCF Service Configuration Editor. Close the WCF Service Configuration Editor tool that appears. The option should now appear on the App.config context menu.
  2. In the Configuration Editor, in the configuration section, expand Services and then expand Endpoints.
  3. Select the first endpoint. Under Endpoint Properties, change the Binding from wsHttpBinding to netTcpBinding.
  4. Select the second endpoint. Under Endpoint Properties, change the Binding from mexHttpBinding to mexTcpBinding.
  5. Under Service, select the Host node, select the default address under the BaseAddress list, and then click Edit.
  6. Set the base address to the following and then click OK:
    net.tcp://localhost:8523/Service1 
    
    
  7. Under Advanced, expand the tree under Service Behaviors. Select serviceMetadata and change httpGetEnabled from True to False.
  8. Click File and then click Save to save your configuration changes.
  9. In Visual Studio, verify your configuration, which should look as follows:
      
        
          
            
              
                
              
            
            
            
              
                
              
            
          
        
        
          
            
              
              
            
          
        
      
    
    
Ff649818.note(en-us,PandP.10).gifNote:
Notes: 
The port 8523 and Service1 are arbitrary for this example. If you run into a conflict where the port is in use, change it.
If you do not set httpGetEnabled to False, you will get an exception in the Event Log when the service tries to start.

Step 3: Create a Windows Service

In this step, you add a Windows Service project to your solution.
  1. Right-click your solution, cllick Add, and then click New Project.
  2. In the Add New Project dialog box, select Windows, and then select Windows Service.
  3. In the Name field, leave the default name WindowsService1 and then click OK to create a Windows service application.
  4. Copy App.config from your WCF Service Library project to your Windows service project. In the WCF Service Library project, right-click the App.config file, click Copy, and then right-click your Windows service project and click Paste.

Step 4: Add the Service Installers to the Windows Service

In this step, you add service installers to your Windows service.
  1. Right-click Service1.cs and then click View Designer.
  2. Right-click the designer view and then click Add Installer.
    This adds the ProjectInstaller.cs file with two objects, serviceProcessInstaller1 and serviceInstaller1.
  3. In the Design view of ProjectInstaller.cs, right-click serviceProcessInstaller1 and then click Properties.
  4. In the Properties pane, set the Account attribute to NetworkService.
  5. Right-click serviceInstaller1 and then click Properties.
  6. In the Properties pane, set the StartType attribute to Automatic.

Step 5: Modify the Windows Service to Host the WCF Service

In this step, you override the OnStart() and OnStop() methods to start and stop the WCF service inside the Windows service process.
  1. Add a reference to System.ServiceModel to your Windows Service project. To do so, in your Windows service project, right-click the References node and then click Add References. In the Add Reference dialog box, select System.ServiceModel and then click OK.
  2. Add a reference to your WCF Service Library project from your Windows service. To do so, in your Windows service project, right-click the References node and then click Add References. In the Add Reference dialog box, select the Projects tab. Select the WCF Service Library project, WcfServiceLibrary1, and then click OK.
  3. Add the following using statements to the Service1.cs file in your Windows service project.
    using System.ServiceModel;
    using WcfServiceLibrary1;
    
    
  4. Select Service1.cs and switch to code view.
  5. Declare an internal static member of ServiceHost type, as follows:
    internal static ServiceHost myServiceHost = null; 
    
    
  6. Override the OnStart method of the Windows service, to open the service host as follows:
    protected override void OnStart(string[] args)
    {
       if (myServiceHost != null)
       {
           myServiceHost.Close();
       }
       myServiceHost = new ServiceHost(typeof(Service1));
       myServiceHost.Open();
    }
    
    
  7. Override the OnStop method of the Windows service, to close the service host as follows:
    protected override void OnStop()
    {
       if (myServiceHost != null)
       {
          myServiceHost.Close();
          myServiceHost = null;
       }
    }
    
    
  8. Verify that your Service1.cs resembles the following:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.ServiceModel;
    using WcfServiceLibrary1;
    
    namespace WindowsService1
    {
        public partial class Service1: ServiceBase
        {
            internal static ServiceHost myServiceHost = null; 
    
            public WCFServiceHost1()
            {
                InitializeComponent();
            }
            protected override void OnStart(string[] args)
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                }
                myServiceHost = new ServiceHost(typeof(Service1));
                myServiceHost.Open();
            }
            protected override void OnStop()
            {
                if (myServiceHost != null)
                {
                    myServiceHost.Close();
                    myServiceHost = null;
                }
            }
        }
    }
    
    
  9. In the Solution Explorer, copy the App.config file from the WCF service project to the Windows service project so that the config file will be in both service binary folders after compiling.
  10. Build your solution and verify that your project produces WindowsService1.exe in your project \bin\debug directory of yourWindowsService1 project.

Step 6: Install the Windows Service

In this step, you install the Windows service and run it from the Services console.
  1. Rebuild the solution and open a Visual Studio command prompt.
  2. Browse to the bin directory of the project where WindowsService1.exe is located.
  3. Run the following command to install the service:
    Installutil WindowsService1.exe 
    
    
  4. Start your service. To do so, click Start, click Run, type services.msc and then click OK. Right-click your service and then clickStart.
Ff649818.note(en-us,PandP.10).gifNote:
If you have modified the service that is already installed, you can uninstall it by using following command:
Installutil /u WindowsService1.exe

Step 7: Create a Windows Forms Test Client Application

In this step, you create a Windows Forms application named Test Client that you will use to test the WCF service.
  1. Right-click your solution, click Add, and then click New Project.
  2. In the Add New Project dialog box, in the Templates section, select Windows Application.
  3. In the Name field, type Test Client and then click OK to create a Windows Forms application.

Step 8: Add a WCF Service Reference to the Client

In this step, you add a reference from your test client to your WCF service
  1. Right-click your Test client project and select Add Service Reference.
  2. In the Add Service Reference dialog box, set the Address to the following and then click OK
    net.tcp://localhost:8523/Service1
    
    
    Ff649818.note(en-us,PandP.10).gifNote:
    net.tcp://localhost:8523/Service1 is the base address that you set in Step 3 above.

Step 9: Test the Client and WCF Service

In this step, you use the test client to ensure that the WCF service is running properly.

  1. In your Client project, drag a button control onto your form.
  2. Double-click the button control to show the underlying code. In the code behind the button click, create an instance of the proxy, and call GetData of your WCF service. When you call the service, your current user security context will automatically be passed to your WCF service. The code should look as follows:
            private void button1_Click(object sender, EventArgs e)
            {
                ServiceReference1.Service1Client myService = new ServiceReference1.Service1Client();
                MessageBox.Show(myService.GetData(123), “My Service”);
                myService.Close();
            }
    
    
  3. Right-click your client project and then click Set as Startup Project.
  4. Run the client application by pressing F5 or Ctrl+F5.
    When you click the button on the form, the message “You entered: 123” should appear.

Tuesday, July 30, 2013

DataContract Serializer and IsReference property

In .net Framework 3.5 SP1, DataContractSerializer supports by-ref object graph serialization by using the standard xsd:ID/xsd:IDREF attributes. 
You can set the IsReference=true on your DataContract definition and serializer will generate XML elements with IDs/IDREFs attributes and will link them together rather embedding them inside each other(default behavior). 
Also if you examine the XSD generated by WCF as part of the metadata export, it will also contain the standard ID/IDREF xml schema attributes. Because of this, xml can be correctly parsed and understood by any framework in a standard way. 
This change will enable serialization of object graphs having circular references (which wasn’t possible previously – at least not without writing custom code) and will also reduce the size of the serialized xml. 
Let’s examine this change using the following DataContract definition: 
    [DataContract] 
    public class Employee 
    { 
        [DataMember] 
        public string Name { getset; } 
        [DataMember] 
        public Employee Manager { getset; } 
    } 
    [DataContract] 
    public class Department 
    { 
        [DataMember] 
        public List<Employee> Staff { getset; } 
        [DataMember] 
        public string DeptName { getset; } 
    } 
Now if we serialize following Department object using DataContractSerializer 
        var kenny = new Employee() { Name = “Kenny” }; 
        var bob = new Employee() { Name = “Bob”, Manager = kenny }; 
        var alice = new Employee() { Name = “Alice”, Manager = kenny }; 
        var ahmed = new Employee() { Name = “Ahmed”, Manager = kenny }; 
   
        var dept = new Department() { DeptName = “RandD”, Staff = new List<Employee>() { kenny, bob, alice, ahmed } };         
        DataContractSerializer dcs = new DataContractSerializer(typeof(Department));         
        var ms = new MemoryStream(); 
        dcs.WriteObject(ms, dept); 
        ms.Seek(0, SeekOrigin.Begin); 
          
        var sr = new StreamReader(ms); 
        var xml = sr.ReadToEnd(); 
We will get this xml. 
      <DeptName>RandD</DeptName> 
      <Staff> 
            <Employee> 
                  <Manager i:nil=true /> 
                  <Name>Kenny</Name> 
            </Employee> 
            <Employee> 
                  <Manager> 
                        <Manager i:nil=true /> 
                        <Name>Kenny</Name> 
                  </Manager>  
                  <Name>Bob</Name> 
            </Employee> 
            <Employee> 
                  <Manager> 
                        <Manager i:nil=true /> 
                        <Name>Kenny</Name> 
                  </Manager>  
                  <Name>Alice</Name> 
            </Employee> 
            <Employee> 
                  <Manager> 
                        <Manager i:nil=true /> 
                        <Name>Kenny</Name> 
                  </Manager>  
                  <Name>Ahmed</Name> 
            </Employee> 
      </Staff> 
</Department> 
You can see manager Kenny is included in all Employee objects, essentially a by-value inclusion.  Now if we change the declaration of Employee class to following: 
    [DataContract(IsReference = true)] 
    public class Employee 
    { 
         [DataMember] 
        public string Name { getset; } 
        [DataMember] 
        public Employee Manager { getset; } 
    } 
With above change, you will get following different xml. 
      <DeptName>R&D</DeptName> 
      <Staff> 
            <Employee z:Id=i1 xmlns:z=http://schemas.microsoft.com/2003/10/Serialization/> 
                  <Manager i:nil=true />  
                  <Name>Kenny</Name> 
            </Employee> 
            <Employee z:Id=i2 xmlns:z=http://schemas.microsoft.com/2003/10/Serialization/> 
                  <Manager z:Ref=i1 />  
                  <Name>Bob</Name> 
            </Employee> 
            <Employee z:Id=i3 xmlns:z=http://schemas.microsoft.com/2003/10/Serialization/> 
                  <Manager z:Ref=i1 />  
                  <Name>Alice</Name> 
            </Employee> 
            <Employee z:Id=i4 xmlns:z=http://schemas.microsoft.com/2003/10/Serialization/> 
                  <Manager z:Ref=i1 />  
                  <Name>Ahmed</Name> 
            </Employee> 
      </Staff> 
</Department> 
In attribute-free (POCO) world: 
you can use a different ctor, taking a boolean flag, to toggle by-val/by-ref serialization. 
      DataContractSerializer(Type type, IEnumerable knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, IDataContractSurrogate dataContractSurrogate)
To enable circular references for operation or service scope, you can use custom behaviors etc. Essentially you need the ability to hook into serializer instantiation process and create the instance using above overload: 
   1. Subclass DataContractSerializerOperationBehavior 
   2. Ovverride CreateSerializer method 
   3. Create a new DCS instance passing true to preserveObjectReferences param. 
class DataContractSerializerOperationBehaviorEx : DataContractSerializerOperationBehavior 
public DataContractSerializerOperationBehaviorEx(OperationDescription operation):base(operation) 
public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes) 
return new DataContractSerializer(type, name, ns, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, truethis.DataContractSurrogate); 
public override XmlObjectSerializer CreateSerializer(Type type, System.Xml.XmlDictionaryString name, System.Xml.XmlDictionaryString ns, IList<Type> knownTypes) 
return new DataContractSerializer(type, name, ns, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, truethis.DataContractSurrogate); 
}