Saturday, May 5, 2012

My Blog Has Moved!

You can now find my blog at ShloEmi.com

After almost 7 years at Blogger it's time to grow.

Hope to see you there,
Shlomi.O

Friday, January 20, 2012

WCF-4 Client/Host quick start/tutorial

For those of you who already know WCF basics and need to start quickly here are guidelines to start a working client-server/service project:

My Quickest Start
  1. Designing Service Contracts
    1. Start simple, grow in time.
  2. Implement the contract
  3. Host the service - Console (starting simple).
    1. http://msdn.microsoft.com/en-us/library/ms731758.aspx
      1. New 'Console Application'
      2. See 'Example Host Code' below...
        1. Add Reference.... Select System.ServiceModel
        2. Reference 'ServiceModel'
        3. Start the service code
          1. Using
          2. Open
          3. Close
  4. Disable WcfSvcHost (if needed)
    1. "Right-click the WCF Service Project in Solution Explorer, choose Properties, and click WCF Options tab. The Start WCF Service Host when debugging another project in the same solution check box is enabled by default."
  5. CLIENT SIDE - Obtain the Service Contract, Bindings, and Addresses
    1. Enable the service
      1. Set the Host as the default startup program and run in without debugger (usually <CTRL>+F5).
    2. 'Add Service Reference...' 
      1. Past the service address in the 'Address' field and 'Go'.
      2. Point the service and 'OK' =results=> a new client proxy code will be generated in your client.
    3. SET YOUR CLIENT APPLICATION AS THE STARTUP PROGRAM :)
    4. Add service usage code to your client (see 'Example Client Code' below...).
  6. Run and relax :-).

    Example Host Code
    Code Snippet
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. using System.ServiceModel;
    7. using System.ServiceModel.Description;
    8. using MyWcfLib;
    9.  
    10.  
    11. namespace MyServer
    12. {
    13.     class Program
    14.     {
    15.         static void Main(string[] args)
    16.         {
    17.             Uri baseAddress = new Uri("http://localhost:9090/hello");
    18.  
    19.             using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
    20.             {
    21.                 // Enable metadata publishing.
    22.                 ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    23.                 smb.HttpGetEnabled = true;
    24.                 smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    25.                 host.Description.Behaviors.Add(smb);
    26.  
    27.                 // Open the ServiceHost to start listening for messages. Since
    28.                 // no endpoints are explicitly configured, the runtime will create
    29.                 // one endpoint per base address for each service contract implemented
    30.                 // by the service.
    31.                 host.Open();
    32.  
    33.                 Console.WriteLine("The service is ready at {0}", baseAddress);
    34.                 Console.WriteLine("Press <Enter> to stop the service.");
    35.                 Console.ReadLine();
    36.  
    37.                 // Close the ServiceHost.
    38.                 host.Close();
    39.             }
    40.  
    41.         }
    42.     }
    43. }

    Example Client Code
    Code Snippet
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. namespace ConsoleApplication1
    7. {
    8.     class Program
    9.     {
    10.         static void Main(string[] args)
    11.         {
    12.             var client = new ServiceReference1.MyServiceClient();
    13.  
    14.             Console.WriteLine(client.GenerateGuid().ToString());
    15.             client.Close();
    16.  
    17.             Console.ReadLine();
    18.         }
    19.     }
    20. }


    MSDN's 'Basic Programming Lifecycle'


    Please, post comments if you find errors.
    I'll fix them ASAP and others can enjoy a cleaner clode.

    Hope it helps...

    Saturday, January 7, 2012

    WPF, XAML: TreeView HierarchicalDataTemplate databinding to unknown XML/DataSource

    Problem description
    • I wish to bind my TreeView to a datasource.
    • The TreeView should build its child nodes dynamically (unknows childs in datasource).
    • Each node should display its data or (if no data) its element name.

    Solution
    You need to use and understand these parts:
    1. HierarchicalDataTemplate
      1. HierarchicalDataTemplate.Triggers
    2. Binding XPath

    Usage Example
    Just copy paste and start playing / reading each part's help.

    XAML
    Code Snippet
    1. <Window x:Class="TreeViewDataBinding.Window1"
    2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4.     Title="Window1" Height="300" Width="300">
    5.     <Window.Resources>
    6.         <HierarchicalDataTemplate x:Key="NodeTemplate" >
    7.  
    8.             <TextBlock x:Name="tbName" Text="?" />
    9.  
    10.             <HierarchicalDataTemplate.ItemsSource>
    11.                 <Binding XPath="child::node()" />
    12.             </HierarchicalDataTemplate.ItemsSource>
    13.  
    14.             <HierarchicalDataTemplate.Triggers>
    15.                 <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
    16.                     <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value}"/>
    17.                 </DataTrigger>
    18.                 <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
    19.                     <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/>
    20.                 </DataTrigger>
    21.             </HierarchicalDataTemplate.Triggers>
    22.         </HierarchicalDataTemplate>
    23.  
    24.         <XmlDataProvider x:Key="xmlDataProvider"/>
    25.     </Window.Resources>
    26.     
    27.     <StackPanel>
    28.         <Button Click="Button_Click">Reload</Button>
    29.         
    30.         <TreeView
    31.             ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}"
    32.             ItemTemplate="{StaticResource NodeTemplate}" />
    33.     </StackPanel>
    34. </Window>

    Code behind
    Code Snippet
    1. using System.Windows;
    2. using System.Windows.Data;
    3. using System;
    4.  
    5. namespace TreeViewDataBinding
    6. {
    7.     public partial class Window1 : Window
    8.     {
    9.         public Window1()
    10.         {
    11.             InitializeComponent();
    12.         }
    13.  
    14.         private void Button_Click(object sender, RoutedEventArgs e)
    15.         {
    16.             var provider = (XmlDataProvider)this.Resources["xmlDataProvider"];
    17.             provider.Source = new Uri(@"Data\standard.xml", UriKind.Relative);
    18.         }
    19.     }
    20. }

    Additional files



    Performance
    I was able to load 100MB of xml data in around ~6 seconds first time, ~4 seconds from the second time.

    Testbench station
    • i7 (8 cores) @ 2.80GHz
    • 4.00GB RAM
    • 64-Bit OS, Win7 Pro.
    CPU & memory usage
    The CPU was at idle when i started, the RAM was at the base line.

    Results

    Resources

    Hope it helps.

    Wednesday, December 28, 2011

    How to change paypal button image in magento...

    Don't even bother finding the solution at Magento Admin Panel or at PayPal :), spent quit a time looking for the solution over there.

    Steps

    1. You need to access the file (using ftp...) located here:
      1. /app/design/frontend/base/default/template/paypal/express/shortcut.phtml
    2. Edit it:
      1. find the line that contains "img src=".
      2. replace the source with your wanted 'Pay with PayPal' image path.
    Results
    I've managed to change it from this one:

    To this one:



    Resources

    Using PayPal express with magento.

    The story
    I was asked to give the user the ability to pay using PayPal services.
    The site is magento store.
    The user will have two option at PayPal:

    1. He have an acount and will pay after he logged into his PayPal account.
    2. He have no account - he is NOT requiered to open a PayPal account to pay, he'll enter his credit credentiols and pay.
    Simple, right? :-)

    I got into some problems, the community help on this matter is not so obvious, after a day of research and experements, which hopefuly you will avoid,  here are the final steps did to satisfy these requierments:


    The solution
    On Magento Admin Panel
     
    • Magento admin panel => Menu => System => Configuration, click.
    • In the left configuration panel => Sales => PayPal, click.
    • "Email Associated with PayPal Merchant Account" => enter your PayPal account email.
    • "Select a PayPal Solution" => "Express Checkout", checked
    • At "Express Checkout Settings" (*2):
      • "Payment Action" : Usually Sale .
      • "Enable PayPal Guest Checkout" => Yes
      • SAVE CONIFG!
    • At "API/Integration Settings":
      • API Authentication Methods => API Signiture.
      • Get Credentials from PayPal, click:
        • Entering PayPay... Enter your PayPal credential...
        • Just get your API credentials through Profile > My selling tools > API Access > Update > Request API credentials > Request API Signature (*1)
      • Enter the API credentioal, appropriattly, to:
        • API Username, Password & Signature.
      • SAVE CONIFG!
    These steps created a fast & secure checout point for my users to buy products without PayPay account.

    Hope it helped.

    Resources

    Saturday, December 24, 2011

    MAGENTO: can't "Log in to Admin Panel"

    Just solved this problem.

    Problem description
    I've instaled magento in my local computer and I can't login to the admin panel.

    Understanding the problem
    Magento doesn't work well with 'localhost' and '127.0.0.1'. After some readings on the net I understood that these addresses won't allow cookies, resolting in admin panel login failure.

    Solution
    Edit 'host' file located at 'C:\Windows\System32\drivers\etc'.
    Add a new line, for example:
    127.0.0.1 shloemi.com # Localhost virtual development site.

    Usage
    You can now acces magento admin pannel using the site shloemi.com.
    Example: http://shloemi.com/Magento/index.php/

    WPF: layouts & layout attributes explained.

    Found some very good articles that explains layouts & layout attributes...

    Alignment, Margins, and Padding Overview
    Alignment, Margins, and Padding Overview

    WPF controls layout explained
    http://www.aspfree.com/c/a/Windows-Scripting/WPF-Control-Layout/

    Using the WPF DockPanel http://blogs.objectsharp.com/blogs/dave/archive/2009/03/13/using-the-wpf-dockpanel.aspx