Saturday, May 30, 2020

Bind SSL on Port or Self Host WCF Service add SSL on Port

To Create certificate first, Run flowing command on power shell (Administrator mode)

        

Create cert.

New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname "localhost"

Create password on cert.

$pwd = ConvertTo-SecureString -String "Passw0rd" -Force -AsPlainText

Bind certificate to port.

Open Visual Studio Command Prompt in Administrator mode and runn the flowing command.

    netsh http add sslcert ipport=0.0.0.0:21000 certhash=282FD48907C958CF8B63B65385831E9C9E2728A0 
appid={f2f583ee-f065-4237-b095-b2ff5de14c42}

1.The certhash parameter specifies the thumbprint of the certificate.

2.The ipport parameter specifies the IP address and port, and functions just like the -i switch of the Httpcfg.exe tool described.

3.The appid parameter is a GUID that can be used to identify the owning application.

certhash: thumbprint of cert at creation time show on power shell windows or take from cert details or runn MMC to show details

appid: generate new guid

Bind an SSL certificate to a port number and support client certificates

runn the flowing command to bind an SSL certificate to a port number and support client certificates.

    netsh http add sslcert ipport=0.0.0.0:21000 certhash=282FD48907C958CF8B63B65385831E9C9E2728A0 
appid={f2f583ee-f065-4237-b095-b2ff5de14c42} clientcertnegotiation=enable

Delete cert on poart.

    Netsh http delete sslcert ipport=0.0.0.0:21000
        

Referance:

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate
https://www.codeproject.com/Articles/24027/SSL-with-Self-hosted-WCF-Service

Monday, May 25, 2020

The Marco Polo Game in C# || Marco Polo Instructions in c#

Marco Polo Instructions

In the old city of Venice it was important for children to be good at math so they could grow up to be great merchants. One way for the children of Venice to practice their math skills was to play a game called Marco Polo, named after the great explorer from Venice.

The game is simple.

The children sit around in a circle and count the numbers from 1 to 100. However, every time the number that a child is supposed to say is divisible by 4, the child says 'marco' instead of the number. Every time the number is divisible by 7, the child says 'polo' instead of the number. If the number is divisible by both 4 and 7, the child will say 'marcopolo'.

Goal

Your task is to write a small program that will print out all the correct answers for this game from 1 to 100 in a single string, so that children may use a cheat sheet to follow along.

Expected Output

When executed, your function should print a single string of all answers from 1 to 100.

You may use the following output as the expected result for numbers 1 to 30 for your unit test.

1,2,3,marco,5,6,polo,marco,9,10,11,marco,13,polo,15,marco,17,18,19,marco,polo,22,23,marco,25,26,27,marcopolo,29,30

Solution of above question.

Create a class MarcoPoloInstructions and paste the below code in it.

        
            public class MarcoPoloInstructions
            {
                public string PrintNumber(int number)
                {
                    StringBuilder countNo = new StringBuilder();
                    for (int i = 1; i <= number; i++)
                    {
                        if (i % 4 == 0 && i % 7 == 0)
                        {
                         countNo.Append(" marcopolo,");
                        }
                        else if (i % 4 == 0)
                        {
                         countNo.Append(" marco,");
                        }
                        else if (i % 7 == 0)
                        {
                         countNo.Append(" polo,");
                        }
                        else
                        {
                         countNo.Append($" {i},");
                        }
                    }
                    Console.WriteLine(countNo.ToString().TrimEnd(','));
                    return countNo.ToString().TrimEnd(',');
                }
            }
        

Call above code from main program.

    
        public class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("==================Qus: Marco Polo Instructions========================");
                MarcoPoloInstructions marco = new MarcoPoloInstructions();
                marco.PrintNumber(100);       
                Console.Read();
            }
        }
    

For any question or query please comment below.

Click here to download source code.

Read 7-Segment Invoice Numbers From Text File.

User Story Instructions:

The following exercise has one user story to implement. Your goal is to have a working .NET program that implements the use case.

You can assume the input is valid. You don't need to handle unexpected characters or invalid spacing in the input file.

Guidelines:

We are looking for:

  • Working code.
  • Unit Tests.
  • Readable code.
  • Simple code.

Keep your code simple, don't over engineer. Good luck!

User Story 1 - Parse invoice numbers

We have recently decided to digitise our old invoice archives. Since finding a volunteer for such an arduous task was impossible, an employee was selected at random and instructed to type in all invoice numbers into a text file.

Little did we know that the employee we picked is an aspiring ASCII artist. Instead of handing us a file containing a set of numbers, we ended up with 7-segment display representations of the invoice numbers.

This is where you come in. Write a simple .NET program that allows the user to upload a text file of 7-segment invoice numbers, and outputs a list with the parsed invoice numbers.

Input:

  • A text file containing several hundreds of invoice numbers in the following form:

          _  _     _  _  _  _  _  (line 1)
        | _| _||_||_ |_   ||_||_| (line 2)
        ||_  _|  | _||_|  ||_| _| (line 3)
                                  (line 4)
          _  _  _  _  _  _     _  (line 5)
      |_||_|| ||_||_   |  |  ||_  (line 6)
        | _||_||_||_|  |  |  | _| (line 7)
                                  (line 8)
    

    Invoice number format:

    • Each invoice number is constructed of 9 digits [0..9]
    • Invoice number is written using _ and | characters.
    • Invoice number input takes 4 lines.
    • The first 3 lines contain 27 characters.
    • The fourth line is blank.

    Note: You can use input_user_story_1.txt to feed your program.

Output:

  • A text file with the parsed invoice numbers. One number per row.

    Example:

      123456789
      490867715
    

    Note: You can use output_user_story_1.txt to test that the output file generated by your program is correct.

User Story 2 - Top Secret

During your in-person interview, you will be asked to modify your code to handle User Story 2. So be sure your code can be easily modified. More details will be given during the interview.

Input:

  • Example:

    
          _  _     _  _  _  _  _  (line 1)
        | _| _||_||_ |_   ||_||_| (line 2)
        ||_  _|  | _||_|  ||_| _| (line 3)
                                  (line 4)
          _  _  _  _  _  _     _  (line 5)
      |_||_|| ||_||    |  |  ||_  (line 6)
        | _||_||_||_|  |     | _| (line 7)
                                  (line 8)
    

Output:

  • Example:

    
      123456789
      4908?7?15 ILLEGAL
    
    

Solution of user story in c# code

Use below code for solution. Create a class ParseInvoiceNumbers and paste below code in it.

    
        public class ParseInvoiceNumbers
        {
                    public List ReadInvoiceFile()
                    {
                        string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"TestData\input_user_story_1.txt");
                        string[] files = File.ReadAllLines(path);
                        return Converrt7SegmentToNumber(files);
                    }

                   private List Converrt7SegmentToNumber(string[] list)
                    {
                          List invoiceList = new List ();
                                for (int i = 0; i < list.Length; i += 4)
                                {
                                    string invoiceNo = "";
                                    string line = list[i];
                                    string line2 = list[i + 1];
                                    string line3 = list[i + 2];
                                    int sum = 0;
                                    int counter = 0;
                                    bool isLeagal = true;

                                    for (int j = 0; j < line.Length;)
                                    {
                                            counter++;
                                            if (!string.IsNullOrWhiteSpace(line[j].ToString()))
                                            sum += (int)Math.Pow(2, counter);
                                            if (!string.IsNullOrWhiteSpace(line2[j].ToString()))
                                            sum += (int)Math.Pow(2, (counter + 2));
                                            if (!string.IsNullOrWhiteSpace(line3[j].ToString()))
                                            sum += (int)Math.Pow(2, counter + 5);
                                            j++;

                                            if (j > 0 && j % 3 == 0)
                                            {
                                                int res = ReturnNumber(sum);
                                                if (res < 0)
                                                isLeagal = false;
                                                Console.Write((res < 0) ? "?" : res.ToString());
                                                invoiceNo += (res < 0) ? "?" : res.ToString();
                                                sum = counter = 0;
                                            }

                                      }

                                        if (!isLeagal)
                                        {
                                            Console.Write(" ILLEGAL");
                                            invoiceNo += " ILLEGAL";
                                        }
                                        invoiceList.Add(invoiceNo);
                                        isLeagal = true;
                                        Console.WriteLine();
                                }

                                return invoiceList;
                        }

                       private int ReturnNumber(int product)
                       {
                                switch (product)
                                {
                                    case 492:
                                    return 0;
                                    case 288:
                                    return 1;
                                    case 244:
                                    return 2;
                                    case 436:
                                    return 3;
                                    case 312:
                                    return 4;
                                    case 412:
                                    return 5;
                                    case 476:
                                    return 6;
                                    case 292:
                                    return 7;
                                    case 508:
                                    return 8;
                                    case 444:
                                    return 9;
                                    default:
                                    return -1;
                                } 
        
                     }
           }

    

Call ParseInvoiceNumbers form the main program.


        public class Program
        {
            static void Main(string[] args)
            {
              ParseInvoiceNumbers invoice = new ParseInvoiceNumbers();
              invoice.ReadInvoiceFile();
              Console.Read();
            }
        }
    

Clicke here to downloda source code.

For any question please comment below.

Bind SSL on Port or Self Host WCF Service add SSL on Port

To Create certificate first, Run flowing command on power shell (Administrator mode) Create cert. New-Sel...