Professional Documents
Culture Documents
These are 2 different technologies, and have complete different programming models.
So the developers have to learn different technologies.
So to unify and bring all these technologies under one roof Microsoft has come up
with a single programming model that is called as WCF - Windows Communication
Foundation.
With WCF,
You implement one service and we can configure as many end points as want to
support all the client needs. To support the above 2 client requirements, we would
configure 2 end points. In the endpoint configuration we can specify the protocols
and message formats that we want to use.
WCF Example
Along the way, we will get a feel of how different these technologies are.
In this video we will discuss creating a simple remoting and a web service. This is
continuation to Part 1. Please watch Part 1 from WCF video tutorial before
proceeding.
using System.Web.Services;
namespace WebServicesDemo
{
[WebService(Namespace = "http://pragimtech.com/WebServices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class HelloWebService : System.Web.Services.WebService
{
[WebMethod]
public string GetMessage(string name)
{
return "Hello " + name;
}
}
}
Label1.Text = client.GetMessage(TextBox1.Text);
}
The ASP.NET web application is now able to communicate with the web service. Not
just asp.net, a JAVA application can also consume the web service.
These are 2 different technologies, and have complete different programming models.
So the developers have to learn different technologies.
So to unify and bring all these technologies under one roof Microsoft has come up
with a single programming model that is called as WCF - Windows Communication
Foundation.
With WCF,
You implement one service and we can configure as many end points as want to
support all the client needs. To support the above 2 client requirements, we would
configure 2 end points. In the endpoint configuration we can specify the protocols
and message formats that we want to use.
WCF Example
Along the way, we will get a feel of how different these technologies are.
In this video we will discuss creating a simple remoting and a web service. This is
continuation to Part 1. Please watch Part 1 from WCF video tutorial before
proceeding.
If the client is not built using .NET platform, will they be able to consume a .NET
Remoting web service?
They can, but we need to be very careful in choosing the data types that we use in
the service, and client-activated objects and events should be avoided. But keep
in mind .NET Remoting is not meant for implementing interoperable services.
If your goal is to build interoperable services use ASP.NET Web Services. But with
introduction of WCF, both .NET Remoting and ASP.NET Web Services are legacy
technologies.
This is continuation to Part 2. Please watch Part 2 from WCF video tutorial before
proceeding.
In Part 2,
To meet the requirement of the first client, we implemented a web service and to
meet the requirement of the second client we implemented a remoting service.
In this video, we will create a single WCF service, and configure 2 endpoints to
meet the requirements of both the clients.
That's it we are done implementing a WCF Service. The next step is to host the
service using a console application. A WCF service can also be hosted in a Windows
application, or Windows Service or IIS. We will discuss these hosting options in a
later video session.
1. Right click on HelloService solution in Solution Explorer and add a new Console
Application project with name = HelloServiceHost
2.
using System.Web.Services;
namespace WebServicesDemo
{
[WebService(Namespace = "http://pragimtech.com/WebServices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class HelloWebService : System.Web.Services.WebService
{
[WebMethod]
public string GetMessage(string name)
{
return "Hello " + name;
}
}
}
Label1.Text = client.GetMessage(TextBox1.Text);
}
The ASP.NET web application is now able to communicate with the web service. Not
just asp.net, a JAVA application can also consume the web service.
Now we need to host the remoting service. To host it let's use a console
application. A windows application or IIS can also be used to host the remoting
service. Right click on IHelloRemotingService solution in solution explorer and add
new Console Application project, and name it RemotingServiceHost.
7. Add a reference to IHelloRemotingService and HelloRemotingService projects and
System.Runtime.Remoting assembly.
8. Copy and paste the following code in Program.cs file
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemotingServiceHost
{
class Program
{
static void Main()
{
HelloRemotingService.HelloRemotingService remotingService =
new HelloRemotingService.HelloRemotingService();
TcpChannel channel = new TcpChannel(8080);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(HelloRemotingService.HelloRemotingService), "GetMessage",
WellKnownObjectMode.Singleton);
Console.WriteLine("Host started @ " + DateTime.Now.ToString());
Console.ReadLine();
}
}
}
9. Now we need to create the client for our remoting service. Let's use windows
application as the client. Right click on IHelloRemotingService solution in
solution explorer and add new windows application.
10. Add a reference to IHelloRemotingService project and System.Runtime.Remoting
assembly.
11. Drag and drop a textbox, button and a label control on Form1 in the windows
application.
12. Double click the button to generate the click event handler. Copy and paste the
following code in Form1.cs.
using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Windows.Forms;
namespace HelloRemotingServiceClient
{
public partial class Form1 : Form
{
IHelloRemotingService.IHelloRemtingService client;
public Form1()
{
InitializeComponent();
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
client =
(IHelloRemotingService.IHelloRemtingService)Activator.GetObject(
typeof(IHelloRemotingService.IHelloRemtingService),
"tcp://localhost:8080/GetMessage");
}
By this point you may have already realized how different web service and remoting
programming models are. In Part 3, we will discuss implementing a single WCF
service that can satisfy the requirements of both the clients.
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="HelloService.HelloService"
behaviorConfiguration="mexBehaviour">
<endpoint address="HelloService" binding="basicHttpBinding"
contract="HelloService.IHelloService">
</endpoint>
<endpoint address="HelloService" binding="netTcpBinding"
contract="HelloService.IHelloService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/" />
<add baseAddress="net.tcp://localhost:8090"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
4. Copy and paste the following code in Program.cs file
using System;
namespace HelloServiceHost
{
class Program
{
static void Main()
{
using(System.ServiceModel.ServiceHost host = new
System.ServiceModel.ServiceHost(typeof(HelloService.HelloService)))
{
host.Open();
Console.WriteLine("Host started @ " + DateTime.Now.ToString());
Console.ReadLine();
}
}
}
}
Build the solution. Set HelloServiceHost as startup project and run it by pressing
CTRL + F5 keys.
Now let's build a web application that is going to consume the WCF service using
the endpoint with basicHttpBinding. basicHttpBinding communicates over HTTP
protocol using XML messages.
Now let's build a windows application that is going to consume the WCF service
using the endpoint with netTcpBinding. netTcpBinding communicated over TCP protocol
using binary messages.
1. Create a new Windows Forms application and name it HelloWindowsClientTo satisfy
the requirement of the second client let's create a .NET remoting service.
Creating a remoting service
1. Create a new Class Library project and name it IHelloRemotingService.
2. Rename Class1.cs file to IHelloRemotingService.cs. Copy and paste the following
code in IHelloRemotingService.cs file.
namespace IHelloRemotingService
{
public interface IHelloRemtingService
{
string GetMessage(string name);
}
}
3. Right click on IHelloRemotingService solution in solution explorer and add new
class library project, and name it HelloRemotingService.
4. We want to use interface IHelloRemotingService in HelloRemotingService project.
So add a reference to IHelloRemotingService project.
What is WCF?
WCF stands for Windows Communication Foundation and is part of .NET 3.0. WCF is
Microsoft platform for building distributed and interoperable applications.
Examples:
A web application running on one machine and a web service that this application is
consuming is running on another machine.
Distributed System
An enterprise web application may have the following tiers, and each tier may be
running on a different machine
1. Presentation tier
2. Business tier
3. Data Access tier
Connected System
Web services can communicate with any application built on any platform, where as a
.NET remoting service can be consumed only by a .net application.
What technology choices did we have before WCF to build distributed applications?
Enterprise Services
Dot Net Remoting
Web Services
2. The second client uses .NET, so for better performance this client wants
messages formatted in binary over TCP protocol.
Without WCF
1. To satisfy the first client requirement we end up implementing an ASMX web
service, and