Posts filed under 'Distributed application'
Some Error messages when trying out WCF
Some error messages (e.g. InvalidOperationException, configuration binding extension not found, etc.) that I found when I was trying WCF. I listed in here in hope that it would be useful later if entercountered.
Continue Reading 3 comments August 19, 2008
WCF And MSMQ Part 1
Messaging technology allows a client and a server to send a message to each other, and they do not have to run at the same time. One party can send a message to another party at anytime. A crash on one side does not effect the another side.
In this tutorial, I am going to implement a simple WCF client-server application without any consideration on security. We are going to use MSMQ as a transport mechanism. I use Microsoft Visual Studio 2008. Please read Queuing in WCF msdn page for further information. Note: you have to install MSMQ on your computer.
Please refer to this msn page for what is WCF (Windows Communication Foundation) and what does it solves.
Please refer to this msdn page for various WCF technical term.
Each WCF service has an endpoint. An endpoint consists fo address, binding and contract.
- An address is usually a URI which specifies where the endpoint of the service is.
- A binding defines the transport mechanism between a client and a server. There are some predefined bindings avaiable. In this tutorial, we are going to use MSMQ as a transport mechanism, so we are interested in NetMSMQBinding.
- A contract is your service, and it is implemented in C# in this tutorial.
Steps:
1. Create a server project
Create a server project by simply select a C# console application from “New Project” dialog. Let’s name the solution name as “wcf_tutorial” and the server project name as “WCFServer1.”
Please refer to “How to Add or Remove References in Visual Studio” msdn page for how to add a reference.
You need to add two references which are System.messaging (found in .NET tab when opening up the “Add Reference” dialog) and System.ServiceModel.dll (found in “C:\WINDOWS\Microsoft.NET\Framework\v3.0\Windows Communication Foundation” in Windows folder, click on “Browse” tab in the “Add Reference” dialog).
2. Create server source files
IWork is an interface which will be configure as a contract in a binding configuration. WorkService is the implementation of this contract. The server does nothing. It just receives a string from a client.
Please refer to How to: Define a Windows Communication Foundation Service Contract msdn page for how define a contract. Please remember than a MSMQ is a messaging technology which is anynchronous communication. There is no respond back from a server like web service. Thus, the function has no return type which is “void.”
- Please refer to Build a Queued WCF Response Service for more information on WCF and MSMQ.
- Please refer to How to: Implement a Windows Communication Foundation Service Contract msdn page for how to implement a contract.
//////Program.cs//////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Messaging;
using System.Configuration;
namespace WCFServer1
{
[ServiceContract(Namespace = "http://sukasom.wordpress.com")]
interface IWork
{
[OperationContract(IsOneWay = true)]
void submitWork(String str1);
}
public class WorkService : IWork
{
public void submitWork(String str1)
{
Console.WriteLine(“Server has received a work : “ + str1);
}
}
class Program
{
static void Main(string[] args)
{
string queueName = ConfigurationSettings.AppSettings["queueName"];
if (!MessageQueue.Exists(queueName))
MessageQueue.Create(queueName, true);
using (ServiceHost serviceHost = new ServiceHost(typeof(WorkService)))
{
serviceHost.Open();
Console.WriteLine(“The service is ready.”);
Console.WriteLine(“Press <ENTER> to terminate service.”);
Console.ReadLine();
}
}
}
}
Put the above code in the Program.cs. The server part is modified from How to: Host a WCF Service in a Managed Application msdn page.
3. Configure server configuration file
You need to configure the configuration file. Add an App.config file into your project. Please refer to Specifying an Endpoint Address msdn page for more information on address configuration. My configuration file is modified from NetMSMQBinding msdn page.
///////////////App.config /////////////////////////
<?xml version=”1.0” encoding=”utf-8” ?>
<configuration>
<appSettings>
<add key=”queueName” value=”.\private$\ServiceModelSamples” />
</appSettings>
<system.serviceModel>
<services>
<service name=”WCFServer1.WorkService” behaviorConfiguration=”Behavior1“>
<endpoint address=”net.msmq://localhost/private/ServiceModelSamples“
binding=”netMsmqBinding” bindingConfiguration=”test“
contract=”WCFServer1.IWork” />
</service>
</services>
<!– Following is necessary for getting information about the service through http with the svcutil.exe tool. –>
<behaviors>
<serviceBehaviors>
<behavior name=”Behavior1“>
<serviceMetadata httpGetEnabled=”true” httpGetUrl=”http://localhost:8000/Hello/” />
</behavior>
</serviceBehaviors>
</behaviors>
<!– For first example, the security is just disabled. –>
<bindings>
<netMsmqBinding>
<binding name=”test“>
<security mode=”None“>
</security>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
</configuration>
In the “service” element, an endpoint is defined with an address, a binding and a contract. This WCF sample application uses MSMQ as a transport mechanism, so the binding is the NetMsmqBinding.
The contract specifies the interface which is WCFServer1.IWork. The security is turned off in this tutorial through the bindingConfiguration named “test.”
A client needs to be able to access the metadata of the service, so it is defined through the behaviorConfiguration named “Behavior1″ where the httpGetEnabled attribute is set to true. It means that the metadata can be retrieved through the http through the URL set in the httpGetUrl attribute which is “http://localhost:8000/Hello/” in this tutorial. This URL allows the client to get a metadata about the service from the correct location. You can open up the “http://localhost:8000/Hello/“ on the browser as shown below.
More info on address and httpGetEnabled can be found in WCF Part 6 : Address blog.
4. Implement the client project.
Start a visual studio 2008 and add a new C# console project as shown in the figure below. The name of the project is “WCFClient1.” Just like the server part, add the reference to System.ServiceModel.dll. More info on accessing service through a client at Accessing Services Using a WCF Client msdn page.
5. Generate a client proxy.
The ServiceModel Metadata Utility Tool (Svcutil.exe) is needed to generate the proxy file for a client. The proxy file allows you to treat a remote service as a local service. You simply instantiate a remote object and call a function on it. This is much simple compared with some old distributed technologies.
Please refer to ServiceModel Metadata Utility Tool (Svcutil.exe) msdn page for the manaul of Svcutil.exe.
Please start the server first then type the following command in the command prompt.
“C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcUtil.exe” http://localhost:8000/Hello/
The URI is the URI that is defined in the App.config in the server part. After running the above command, you will get two files which are WorkService.cs and output.config.
Please rename the output.config to App.config. Copy those two files into your project directory, and add those files into your project.
Following is the code for the client:
////////////Program.cs in the client part/////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WCFClient1
{
class Program
{
static void Main(string[] args)
{
WorkClient wc = new WorkClient();
wc.submitWork(“hello work 1″);
}
}
}
Calling a server is very simple, instantiating the proxy object and calling a function on it. You can see that there is a class called WorkClient defined in the WorkService.cs. Please run the server first. If the queue has not been created, it will be created. When the queue is created, you don’t have to run the server before running the client. You can run the client first, and the message from the client will be stored in the queue. The message will be sent to the server when the server is started again.
You can take a look Tom Hollander’s blog for another good tutorial on this topic.
Here is the complete source code.
The book from [Lowy] is also good.
1 comment August 18, 2008
Article on WCF performance
Interesting article from Microsoft about comparing performances between WCF (Windows Communication Foundation) and existing .NET distributed technology.
Add comment August 15, 2008
Real-time data for Thai Police through SMS by AIS
I have known that there some companies providing a wireless network solution in some cities in US, so police force can use their laptops in their cars to query real time data related with crime on streets. Obviously, the solution is probably pricey for a developing country.
Interestingly, AIS, cellphone service operator, has provided a service for police to get real time data through SMS. For example, police can send a number of car license plate to the server, and the server sends SMS back telling whether the is stolen or not. I think that SMS sounds great because they can make use of existing telecom infrastructure and its area coverage is quite large.
source : http://www.matichon.co.th/news_detail.php?id=44617&catid=14
Add comment August 7, 2008
Integrating Ubicomp technology into Emergency Response
I have seen some papers presenting ideas of using information technology especially the ubicomp in the prcoess of an emergency response. It is really interesting.
The first paper, Secure Transmission of Pre-clinical Ultrasound Video Data at the Scene of a Mass Casualty Incident, presents e a portable ultra-sound device for a medical team. I think that the device can send an image back to the hospical through UMTS (3G) for further real-time analysis.
The second paper, Envisioning Collaboration at a Distance for the Evacuation of Walking Wounded , implements a prototype handeld for an emergency responder which can transfer information eg. images to an off-site team who is on a way or in the operation center. It can help off-site team to have a better understanding of the situation without bothering the onsite team which needs to focus on working. One interesting feature is that an emergency responser can tag a victim with a tag (probably RF-ID) and add necessary information regarding the victim’s status on the tag. The response team could have an overall picture of the victims through these distributed tags from the operation center.
The third paper, Designing for material practices of coordinating emergency teamwork, that I read is similar to the second one. They implement 3 prototypes. The first deivce is for the victim who can walk. The responder put the first device on the victim to guide him to a safe zone. The second device is a hanhled device like the first device, and it is used for the responder to give him or her an overview of the situation. The third device is the big screen in the operation center to gice an overview of the situation.
I really like the idea of using technology to improve the emergency process. Paper 4 ,Handy Navigation in Ever-Changing Spaces—an Ethnographic Study of Firefighting Practices, where it study about how ubicomp can be used by firefighters to help navigating, gives a strong point that technology should be used for augmenting but not for replacing current practice (prabably including human inteligence too).
Add comment June 29, 2008







