Friday, November 20, 2009

GroupBy and SUM using LINQ in C#

public class Test : IEnumerable<Test>
    {
        public string ColName
        {
            get;
            set;
        }

        public int ColVal
        {
            get;
            set;
        }

        #region IEnumerable<Test> Members

        public IEnumerator<Test> GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion
    }





Code to execute:


List<Test> tests = new List<Test>();

            Test test = new Test();
            test.ColName = "A";
            test.ColVal = 123;
            tests.Add(test);

            test = new Test();
            test.ColName = "A";
            test.ColVal = 10;
            tests.Add(test);

            test = new Test();
            test.ColName = "B";
            test.ColVal = 231;
            tests.Add(test);

            var query1 = from t in tests
                         group t by t.ColName into gr
                         select new
                         {
                             Id = gr.Key,
                             Sum = gr.Sum(r => r.ColVal)
                         };
            foreach (var grp in query1)
            {
                Console.WriteLine("{0}\t{1}", grp.Id, grp.Sum);
            }

Thursday, November 19, 2009

Configuring SSL for WCF services

Please follow these steps for configuring the SSL for WCF services


1.     Create and Install the Certificate on the server for Transport Security
2.     Install the Certificate on the client for Client Authentication
3.     Configure the wsHttpBinding with Certificate Authentication and Transport Security
<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpEndpointBinding">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          security>
        binding>
      wsHttpBinding>
    bindings>
    <client/>
    <services>
      <service behaviorConfiguration="ServiceBehavior"
      name="MyService">
        <endpoint binding="wsHttpBinding"
        bindingConfiguration="wsHttpEndpointBinding"
        name="wsHttpEndpoint" contract="IService" />
        <endpoint address="mex" binding="mexHttpBinding"
        contract="IMetadataExchange"/>
      service>
    services>
      system.serviceModel>
4.     Configure the mex Endpoint to Use wsHttpbinding with Certificate Authentication Configuration
<endpoint address="mex" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding"
 name="mexEndpoint" contract="IMetadataExchange" />
5.     Configure the Virtual Directory to Use SSL and require Client Certificates
a.     Open the IIS
b.    Right the website where the WCF service is hosted
c.     Select the Directory Security tab
d.    Click “Edit Secure Communications”
e.     Click “Require Secure Channel (SSL)” and click “Require Client Certificates”
Now we can open the hosted WCF service over SSL channel.

For client applications to be able to consume this service, its application configuration file should like as shown below;
<configuration>
  <system.serviceModel>
    <client>
      <endpoint
      behaviorConfiguration="ClientCertificateBehavior"
      binding="wsHttpBinding"
      bindingConfiguration="Binding1"
      contract="IMetadataExchange"
      name="https" />
    client>
    <bindings>
      <wsHttpBinding>
        <binding name="Binding1">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" />
          </security>
        </binding>
      </wsHttpBinding>
    <bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ClientCertificateBehavior">
          <clientCredentials>
            <clientCertificate findValue="CN=clienttempcert"
            storeLocation="CurrentUser"
            storeName="My"
            x509FindType="FindBySubjectDistinguishedName" />
          clientCredentials>
        behavior>
      endpointBehaviors>
    behaviors>
  system.serviceModel>
configuration>