Monday, April 23, 2012

Programming WCF - Creating a custom WCF Service and a Host

My last post was about Introduction to Windows Communication Foundation. Today I thought about writing something about how to do some nice programming using WCF and I will show you how to create a custom WCF Service and a Service Host using C#. I assume that you all have some knowledge of WCF programming, so I will be not explaining every single line of code here.

First I will start off by creating class library project and I will name it "WCFClassLibrary". In that project, I will create a class named "MyWcfService" and I will also create an Interface which is "IMyWcfService". Now in my "WCFClassLibrary", I will add a reference to the "System.ServiceModel.dll" and I am now going to modify my "IMyWcfService" interface.

I will be using "System.ServiceModel.dll" in my interface and since interfaces that represents WCF contracts needs to have [ServiceContract] attribute, I am modifying the interface as follows.
using System.ServiceModel;
namespace WCFClassLibrary
{
    [ServiceContract]
    interface IMyWcfService
    {
        [OperationContract]
        string MyTestMethod(string sHelloWorld);
    }
}
Then in my "MyWcfService" class, I am going to implement the IMyWcfService interface.
namespace WCFClassLibrary
{
    public class MyWcfService : IMyWcfService
    {
        public string MyTestMethod(string sHelloWorld)
        {
            return sHelloWorld;
        }
    }
}
Now I am done with implementing the service. Next what I have to do is implement the host application to host my service. For that I will create a console application. For that I will add a new Console Application project and I will name it "MyWcfHostConsoleApplication". I will add a reference to the "System.ServiceModel.dll" and "WCFClassLibrary.dll" project. Then I will add a Application configuration file and it's name is "App.config". So it's time modify the "App.config" file. Within,
<configuration></configuration>
I will start writing following code.
<system.serviceModel>
    <services>
        <service name="WCFClassLibrary.MyWcfService">
            <endpoint address="" binding="basicHttpBinding" contract="WCFClassLibrary.IMyWcfService">
            </endpoint>

            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/MyWcfService"/>
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>
Now, I have to write code for hosting my service from inside my Main method. For that I will modify my Main method as follows.
using System;
using System.ServiceModel;
using WCFClassLibrary;

namespace MyWcfHostConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost serviceHost = new ServiceHost(typeof(MyWcfService)))
            {
                serviceHost.Open();
                Console.ReadLine();
            }
        }
    }
}
Now when I type "http://localhost:8080/MyWcfService" and hit enter in by browser, what I will see is this,

Metadata publising for this service is currently disabled.

You will see that "Metadata  publishing for this service us currently disabled.". To enable Metadata  publishing what you will have to do is again modify the "App.config " as follows.
<system.serviceModel>
    <services>
        <service name="WCFClassLibrary.MyWcfService" behaviorConfiguration="MyWcfServiceMEXBehavior">
            <endpoint address="" binding="basicHttpBinding" contract="WCFClassLibrary.IMyWcfService">
            </endpoint>

            <!--MEX endpoint-->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
            </endpoint>

            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/MyWcfService"/>
                </baseAddresses>
            </host>
        </service>
    </services>

    <!--A behavior definition for MEX-->
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyWcfServiceMEXBehavior">
                <serviceMetadata httpGetEnabled="True"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>
Now again when I host and I type "http://localhost:8080/MyWcfService" and hit enter in by browser, I can see this.

MyWcfService
That's all. Now I can add a Service Reference to this service from my client applications and I can consume this service.

Appreciate your feedback.

Happy Coding.

Regards,
Jaliya

Sunday, April 22, 2012

Introduction to Windows Communication Foundation

Today I am going to write about Windows Communication Foundation (WCF). I am pretty sure you all must have used WCF services already in your applications to build  the application communication. Since WCF is a topic of itself I will be only covering the basics here and it's up for you to discover the inner nice things which are in topics I am covering here.

Windows Communication Foundation (WCF) code named "Indigo" is an API which was introduced with .NET 3.0 and it is specifically designed for the process of building distributed systems.

A Distributed system can be two networked computers or even two executables in same computer that needs to exchange data. Let's call these two ends a client and a service computer. So these client and service computers sometimes can be running same operating system using the same programming language or may be different.

Prior to .NET 3.0 there were several distributed APIs used by windows software developers.
  • DCOM
  • COM+/Enterprise Services
  • MSMQ(Microsoft Message Queuing)
  • .NET Remoting
  • XML Web Services

XML Web Services

XML Web Services are what we used before WCF come to the picture. Web services are components on a Web server that a client application can call by making HTTP requests across the Web. Unlike traditional brower-based web applications, a web services provides a way to expose the functionality of remote components using standard web protocols.

The initial release of .NET has been released with the "System.Web.Services" namespaces with superior support for XML Web Services. In most cases the programmer just had to apply [WebMethod] attribute to each public method which he needs to expose to outside world.

These Web Services encodes it's data in simple XML and can be accessed using simple HTTP. Of course, XML Web Services is not a perfect distributed API for in-house applications where you could use a TCP based protocol and binary based formatting of data.

Windows Communication Foundation

WCF is a distributed API represented primarily by "System.ServiceModel" namespace. Using WCF, you can expose services to callers using wide variety of techniques. For example, if you build an in-house application where all connected machines are windows based, you can use various TCP protocols to ensure the fastest possible performance. You can also expose this same service with the XML Web Service based protocol to allow external callers to leverage its functionality, regardless of the programming language or the operating system.

The Basic Composition of a WCF Application

WCF applications mainly contains three main assemblies.
  1. WCF Service assembly - This dll contains the classes and interfaces that represent the overall functionality that we want to expose to external callers.
  2. WCF service host - This is the entity that hosts the service assembly.
  3. WCF client - This is the application that consumes the service through an intervening proxy.

The ABC's of WCF

The hosts and clients communicate with each other through these ABC's which I am going explain in a second.
  • Address - Describes the location of the service.
  • Binding - WCF ships with a many different bindings that specify network protocols, encoding mechanisms, and transport layer.
  • Contract - Provides a description of each method exposed from the WCF service.

     WCF Contracts

Understanding the role of a contract is the key to building a WCF service. A contract is a standard way of describing what a service can do. Meanwhile, it is platform independent. In WCF there are 4 types of contracts,
  1. Service Contract
  2. Data Contract
  3. Fault Contract
  4. Message Contract

     WCF Bindings

Bindings specify how a Windows Communication Foundation (WCF) service endpoint communicates with other endpoints.


     WCF Address

Once we have establish the contracts and bindings, final stage is to specify the address for the WCF service. From a high level, WCF addresses can specify the following bits of information.
  • Scheme - Transport protocol.
  • MachineName - The full qualified domain of the machine.
  • Port - This is optional in many cases, for example the default for HTTP binding is port 80.
  • Path - The path to the WCF service.
So the address would be the in the format of,
Scheme://<MachineName>[:Port]/Path

     WCF Endpoints

All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service.


So that's the basics of some theoretical things in WCF. If you got all these things hard to understand, try apply these with some practical programming of WCF. I am pretty sure you will be fine then.

Appreciate your feedback.

Happy Coding.

Regards,
Jaliya

Sunday, April 15, 2012

Installed Microsoft Lync Server 2010

Last week for the first time in my life I have successfully installed and configured Microsoft Lync Server 2010. Lync was not at all my subject, but it was a pretty nice experience. I must say Thank You to one of my good old friend for helping me to make it happen. Thanks Kosala Kuruppu.

Happy Coding.

Regards,
Jaliya

Tuesday, April 3, 2012

Visual Studio Application Lifecycle Management(ALM)

Visual Studio ALM or Visual Studio Application Lifecycle Management is a collection of integrated software development tools developed by Microsoft. These tools include IDEs, testing tools, source control, work items, collaboration, metrics, and reporting tools.

If you consider Visual Studio editions, there are mainly four versions of Visual Studio. Visual Studio Test Professional, Visual Studio Professional, Visual Studio Premium and Visual Studio Ultimate.

Visual Studio Test Professional is mainly for Quality Assuarance guys and there is nothing interesing for developers there. Visual Studio Professional is the most basic edition among the three developer editions. Average powered edition is Visual Studio Premium. If you have Visual Studio Ultimate, then you have like you have got everything. It is the most powered, most featured Visual Studio edition you can ever have.

You can use Visual Studio ALM features in Visual Studio Premium or Visual Studio Ultimate in combination with Visual Studio Team Foundation Server. And the sad thing is Visual Studio Test Professional and Visual Studio Professional is not supported for Visual Studio ALM.

What is Visual Studio Team Foundation Server

Microsoft Visual Studio Team Foundation Server (TFS) is the collaboration platform at the core of the Visual Studio solution for application lifecycle management. TFS is mainly responsible for source control, data collection, reporting, and project tracking, and is intended for collaborative software development.

What we can do with Visual Studio ALM
  • Manage application's lifecycle by using the suite of tools in Visual Studio Premium and Visual Studio Ultimate in combination with Visual Studio Team Foundation Server.
  • Better understand customer needs and more effectively design, implement, and deploy code.
  • Plan and track the project. Enact processes and monitor their quality of the team.
  • Design functionality, either on top of existing assets or from scratch, by using architectural diagrams.
  • Write, unit test, debug, analyze, and profile the application by using tools that are integrated with the rest of the application lifecycle so that team can understand how their progress contributes to the project. Use version control to manage the source code and other files.
  • Build the application by using an integrated build system so that the team can ensure that quality gates have been met and verify that requirements have been fulfilled in each build.
  • Test the application by running manual or automated tests, including performance and stress tests. Manage testing systematically so that the team knows the software quality on any given day.
  • Deploy into virtual environments to enable more sophisticated development and testing.

Team Foundation Server & Visual Studio

Team Foundation Server & Visual Studio
For more information visit,

Happy Coding.

Regards,
Jaliya