Creating and Consuming the WCF Service

This session show how to create the WCF service:


this project developed in the VS 2008 in Windows 7 platform




Direct Link

This consist of 3 projects



I WcfServiceLibrary1
---------------------

1.IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary1
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);

[OperationContract()]
string SayHello();

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
}

// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}

2. Service1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Net;
namespace WcfServiceLibrary1
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

public string SayHello()
{
return string.Format("Hi, Hello from {0}",Dns.GetHostName().ToString());
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}



II WCF_Host_Application

Add the References
* System.ServiceModel
* WcfServiceLibrary1.dll

Design the from



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace WCF_Host_Application
{
public partial class Form1 : Form
{
ServiceHost vHost;
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//start

try
{
vHost = new ServiceHost(typeof(Service1));
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

vHost.AddServiceEndpoint(typeof(IService1), b, new Uri("net.tcp://localhost/myservice/"));
vHost.Open();
btn_start.Enabled = false;
btn_stop.Enabled = true;
lbl_info.Text = "Service started";

MessageBox.Show("Service started");
}
catch (Exception ex)
{
lbl_info.Text = ex.Message.ToString();
}
}

private void btn_stop_Click(object sender, EventArgs e)
{
vHost.Close();
btn_start.Enabled =true ;
btn_stop.Enabled = false;
lbl_info.Text = "Service stoped";
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}


III. WCF Client

Add the References
* System.ServiceModel
* WcfServiceLibrary1.dll


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace WCF_Client
{
class Program
{
static void Main(string[] args)
{
IService1 vService;
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;


EndpointAddress vEndPoint = new EndpointAddress("net.tcp://192.168.1.11/myservice/");
ChannelFactory cf = new ChannelFactory(b, vEndPoint);

vService = cf.CreateChannel();
try
{
Console.WriteLine("Result from {0} : {1} ", vEndPoint.Uri.ToString(), vService.SayHello());
}
catch (Exception ex)
{
Console.WriteLine("Error occured while connection establishment on " + vEndPoint.Uri.ToString());
}

vEndPoint = new EndpointAddress("net.tcp://192.168.1.12/myservice/");
cf = new ChannelFactory(b, vEndPoint);

vService = cf.CreateChannel();
try
{
Console.WriteLine("Result from {0} : {1} ", vEndPoint.Uri.ToString(), vService.SayHello());
}
catch (Exception ex)
{
Console.WriteLine("Error occured while connection establishment on " + vEndPoint.Uri.ToString());
}

Console.ReadLine();

}
}
}


Thats all

now Run the "WCF_Host_Application" and press Start

Now Run the "WCF_Client"


Enjoy Programming

Comments

Anonymous said…
It is then open to another player to proceed the bank, starting with the same amount and dealing from the rest of the pack used by their predecessor. The outgoing banker takes the place beforehand occupied by their successor. A player going bank could both do so on a single hand, in the ordinary course, or a cheval, i.e. on two arms individually, 바카라사이트 one-half of the stake being performed upon each hand. A player going bank and shedding could again go bank, and occasion that they} again lose, could go bank a third time, however not further.

Popular posts from this blog

Know more about Azure Website KUDU?

How to check the Hybrid Connection in Azure Website (Webapp)

A Technical Talk on Microservices and Cloud Deployment