1. Create a WCF Service library project
- Open Visual Studio > File > New > Project
- Select WCF Service Library template under Visual C# > WCF templates
- Change the name to ChatServiceLibrary and click OK
- Visual studio will add reference to System.ServiceModel and System.Runtime.Serialization and will add some files with sample code.
- Delete IService1.cs and Service1.cs and add a new class called ChatService
1.1 Create the Data Contract
- Open ChatService.cs file that you created.
- Add using System.ServiceModel;
- Add using System.Runtime.Serialization;
- Add the following code above ChatService class
[DataContract]
public class ChatMsg
{
public ChatMsg() { }
public ChatMsg(string n, string m){ this.User = n; this.Msg = m;}
[DataMember]
public string User;
[DataMember]
public string Msg;
}
1.2 Create the Service Contract
- Add the following code
[ServiceContract]
public interface IChatService
{
[OperationContract(IsOneWay=true)]
void Notify(ChatMsg msg);
}
[ServiceContract(CallbackContract = typeof(IChatService))]
public interface IChatAdmin
{
[OperationContract(IsOneWay = true)]
void Register(string name);
[OperationContract(IsOneWay = true)]
void Submit(ChatMsg msg);
}
1.3 Service Implementation
- Add the following code in the ProductService.cs class
public class ChatService : IChatService
{
public void Notify(ChatMsg m)
{
Console.WriteLine("{0}: {1}", m.User, m.Msg);
}
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class ChatAdminService : IChatAdmin
{
Dictionary<string, IChatService> clients = new Dictionary<string, IChatService>();
public void Register(string user)
{
IChatService client = OperationContext.Current.GetCallbackChannel<IChatService>();
if (client != null)
clients.Add(user, client);
Console.WriteLine("{0}has joined.", user);
}
public void Submit(ChatMsg m)
{
foreach (string key in clients.Keys)
{
try
{
if (!m.User.Equals(key))
clients[key].Notify(m);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
1.4 Service endpoints
- Updated the name of the service in App.config as given below:
<service name="ChatService.ChatAdminService">
- Updated the contract in the endpoint of the service in App.config as given below:
<endpoint address="" binding="wsDualHttpBinding" contract ="ChatService.IChatAdmin">
1.5 Test Service
- Build the Service.
2. Create a Service Host
- Add a new project to the current solution: Right click Solution on Solution Explorer > Add > Add New Project > Select Console Application template under Visual C# > Windows templates
- Change the project name to Host and click OK
- In Solution Explorer, under Host project, right click References and select Add Reference...
- In the Reference Manager window, add reference to System.ServiceModel and to ChatServiceLibrary solution
- Add using System.ServiceModel;
- Add using ProductServiceLibrary;
- Add the following code in Program.cs file
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(ChatAdminService));
try
{
host.Open();
Console.ReadKey();
host.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
host.Abort();
}
Console.ReadKey();
}
}
2.1 Configure Service and Service Endpoints
- Right click on App.config > Edit WCF Configuration > under services summary click Create a New Service...
- click browse, go out of the current folder, select ChatServiceLibrary > bin > Debug > ChatServiceLibrary.dll > ChatServiceLibrary.ChatService. Click next.
- Keep the default ChatServiceLibrary.IChatAdmin contract and click next.
- Keep the default selection HTTP and click next.
- Select Advanced Web Services interoperability and select Duplex Communication click next
- For address type http://localhost:8733/Design_Time_Addresses/ChatServiceLibrary/ChatService/duplex and click Next and then click Finish
- Save and Exit Microsoft Service Configuration Editor
- Right click on the Host project and Set as StartUp project.
- Right click on ChatServiceLibrary project > properties > WCF Options > Uncheck Start WCF Service Host when debugging another project in the same solution
- Run Service Host
3. Create Chat Client
- Add a new project to the current solution: Right click Solution on Solution Explorer > Add > Add New Project > Select Console Application template under Visual C# > Windows templates
- Change the project name to Client and click OK
- In Solution Explorer, under Client project, right click References and select Add Service Reference...
- In the Program.cs file, add using System.ServiceModel; and using ChatServiceLibrary;
- Add the following code
class Program
{
static void Main(string[] args)
{
Console.Write("user name:");
string user = Console.ReadLine();
DuplexChannelFactory<IChatAdmin> factory = new DuplexChannelFactory<IChatAdmin>(new ChatService(), "ChatMgr");
IChatAdmin admin = factory.CreateChannel();
admin.Register(user);
Console.WriteLine("{0} joined.\n",user);
string msg = Console.ReadLine();
while (!msg.Equals("exit"))
{
admin.Submit(new ChatMsg(user, msg));
msg = Console.ReadLine();
}
}
}
{
static void Main(string[] args)
{
Console.Write("user name:");
string user = Console.ReadLine();
DuplexChannelFactory<IChatAdmin> factory = new DuplexChannelFactory<IChatAdmin>(new ChatService(), "ChatMgr");
IChatAdmin admin = factory.CreateChannel();
admin.Register(user);
Console.WriteLine("{0} joined.\n",user);
string msg = Console.ReadLine();
while (!msg.Equals("exit"))
{
admin.Submit(new ChatMsg(user, msg));
msg = Console.ReadLine();
}
}
}
3.1 Add endpoint
- Right click on App.config > Edit WCF Configuration > under Configuration select Client, under Client section > click Create a New Client...
- select the option Manually
- click browse, go out of the current folder, select ChatServiceLibrary > bin > Debug > ChatServiceLibrary.dll > ChatServiceLibrary.IChatAdmin. Click next.
- Keep the default ChatServiceLibrary.IChatAdmin contract and click next.
- Keep the default selection HTTP and click next.
- Select Advanced Web Services interoperability and select Duplex Communication click next
- For address type http://localhost:8733/Design_Time_Addresses/ChatServiceLibrary/ChatService/duplex and click Next
- Type ChatMgr as the name and then click Next, click Finish
- Save and Exit Microsoft Service Configuration Editor.
4. Testing the Application
- Set the Host project as the startup project
- Press F5 or Start button
- While the Host application is running, go to solution explorer > right click Client > debug > Start New Instance
- Repeat the step 3 two more times.
- Now you should have 4 console windows. One host and three clients. Test the application.
Comments
Post a Comment