Saturday, July 5, 2014

Class vs Structure

Structures:
These are also user defined types similar to a class under which you can define all the members what we can define under class.

class
structure
it is reference type
it is value type
it wasn’t faster in access than a structure
these are faster in access
to create a object we required to use a new operator which is mandatory
we can create the object of it without using new operator also
get its memory allocated on managed heap which provides automatic memory management with the help of garbage collector
Gets it memory allocated on stack which does not provides dynamic memory allocation. garbage collector can’t come into picture
it supports inheritance
it does not support inheritance
a programmer can define a default constructor or will be defined by the compiler if  no constructor exists in the class
a default constructor can only be defined by the compiler a programmer can never do it.
can contains variables declared and initialized
can contain variable declaration but can’t be initialized should be initialized either using object of the structure or constructor of the structure.




Structure Example

namespaceConsoleApplication2
{
    structmystruct
    {
        intx;
        publicmystruct(int x)
        {
            this.x = x;
        }
        publicvoid Test()
        {
            Console.WriteLine("Method" + x);
        }
        publicstatic voidMain()
        {
            mystructm1;
            m1.x = 100;
            m1.Test();
            mystructm2 = new mystruct(200);
            m2.Test();
            Console.ReadKey();
        }
    }
}


Output:
Method 100
Method 200

Delegates

Delegates

This were pointer to Methods whenever we call a method to execute a method it creates a stack and destroys .Each time the method is called a stack is created and destroyed.
If u wants to call a method for multiple times without multiple stacks getting created and destroyed make use of a “Delegate”.

Is a type which holds the method(s) reference in an object?
It is also referred as type safe function.       Type safe means same parameter type and same return type

Advantages
Used to call a method asynchronously.
Effective use of delegates approves the performance of application.

Declaration
Syntax:
Public delegate type of delegate, delegatename ()
Eg:
Public delegate int mydelegate(int delvar1,int delvar2)

Delegates:
Public void Add(int x, int y)
{
Console.WriteLine(x+y);
}
Public delegate void AddDel(int x, int y)

Public string SayHello(string name)
{
return “Hello” +name;
}
Public delegate string sayDel(string str)
Creating objects of Delegates
AddDel ad=new AddDel(Add);
sayDel sd=new sayDel(SayHello);

Delegates are of two types
Single cast delegates
Multi cast delegates

Single cast delegate:

If a delegate is used for calling only a single method than it is known as single cast delegate.



output


Multi cast Delegate

If a delegate is used for calling more than one method than it is called as Multi Cast Delegate.
If you want to add a method to the invocation list  of a delegate object, you simple make use of the overloaded+=operator and if you want to remove a method from invocation list make use of overloaded –

Note:
If you want to create a multi cast delegate with return type you will get the return type of the last method in the invocation list.





Server.Transfer and Respone.Redirect

server.transfer
Response.Redirect

does not send any message to the browser but rather redirects the user directly from the server itself
it sends message to the browser saying  it to  move to some different page.
so in server .transfer there is no round trip
has round trip and hence put loads on server
using server.transfer you can not redirect to different server
example if your server is www.yahoo.com and cannot using server.transfer to move to www.microsoft.com
 but yes , you can move to www.yahoo.com/travels i.e within the websites
using response.redirect  you can redirect to different server




File System vs Http

File system
HTTP
it is used for website development
it is used for website development and hosting
it will uses ASP.NET development server
it will uses iis web server
virtual directory will not be created
virtual directory will be created in IIS Server
for each website different port no will be used
for, the website run through http protocol same port no will be used.

Friday, July 4, 2014

WCF WITH WINDOWS HOSTING

File ->New-> Project-WCF Service Library

Delete the files IService and Service

Go to App.Config and Delete the Following lines which is written in
<services>
</services>


Add New Item and Add WCF Service name as Jobs.cs


And click on Add , then two files will be created.
IJobs.cs
Jobs.cs

Add New Item->Job.cs



Write the following code in IJobs.cs



Write the following code in Job.cs



Write the following code in Jobs.cs



Click on Start Debugging




Now double on GetJobs() to check the method is working or not.

 The following screen appears, Next click on Invoke




And check the check box and click on Ok .



And click on the button beside View and see the data.





Copy the Url of the Service Application
.
Client application

Select  windows application

First add the service Reference


And paste the URL



And click on Go button

And click on Ok Button.

And design the form as follows




Write the following code in Form1.cs

private void button1_Click(object sender, EventArgs e)
        {
            ServiceReference1.JobsClient obj = new ServiceReference1.JobsClient();
            DataSet ds = new DataSet();
            ds = obj.GetJobs();
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ServiceReference1.JobsClient obj = new ServiceReference1.JobsClient();
            ServiceReference1.Job jobobj = obj.GetJobInfo(int.Parse(textBox1.Text));
            textBox2.Text = jobobj.employeename;
            textBox3.Text = jobobj.address;
        }

Run  and see the output




Windows Hosting in WCF
File-new Project-windows Service


Next Right Click on Add Installer





We have to add the reference of our WCF Service Library DLL in this service.

To do this, right click on the project in Solution Explorer and select Add Reference. For adding the dll in reference, select browse tab in the window that appears, and navigate to the folder where our WCF service library is located. In that location, the dll can be found inside the bin-> debug folder. When this dll is added, it is imported into the Windows service app.



Similarly we need reference of System.ServiceModel namespace because it is not available by default in Windows Service.

We will get two processes

Service process installer
Service installer



Service.cs
public partial class Service1 : ServiceBase
    {
        ServiceHost sHost;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            sHost = new ServiceHost(typeof(WcfServiceLibrary3.Jobs));
            sHost.Open();
        }

        protected override void OnStop()
        {
            sHost.Close();
        }
    }

Registering the Service with Windows OS

Go to the design view of the Windows service, right click and click on Add Installer option.

When this is clicked, we get two processes – ServiceProcessInstaller and ServiceInstaller.
Click on ServiceProcessInstaller and set its account property to local system.



Now click on Service Installer properties



Now build the service once again. This will create an executable file.
Copy the path of this exe file location and open the containing folder from command prompt.
In that path write the command
InstallUtil WindowsService.exe
Which registers the service with Windows OS?
Once it is registered with Windows, we can see it in the services list.

(Control Panel > Administrative Tools > Services)




Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...