You are on page 1of 11

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;

Add a reference to System.ServiceModel assembly and HelloService project


3. Right click on HelloServiceHost project and add Application Configuration File.
This should add App.config file to the project. Copy and paste the following XML.
Notice that we have specified 2 endpoints in the configuration. One endpoint uses
basicHttpBinding, which communicates over HTTP protocol using XML messages. This
endpoint will satisfy the requirement of the first client. The other endpoint uses
netTcpBinding, which communicates over TCP protocol using binary messages. This
endpoint will satisfy the requirement of the second client.
<?xml version="1.0" encoding="utf-8" ?>

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

In Part 2, we will discuss implementing


1. A web service to exchange messages in XML format using HTTP protocol for
interoperability.
2. A remoting service to exchange messages in binary format using TCP protocol for
performance.

Along the way, we will get a feel of how different these technologies are.

Part 2 - Creating a remoting service and a web service


Suggested Videos
Part 1 - Introduction to WCF

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.

We have 2 clients and we need to implement a service a for them.


1. The first client is using a Java application to interact with our service, so
for interoperability this client wants messages to be in XML format and the
protocol to be HTTP.
2. The second client uses .NET, so for better performance this client wants
messages formatted in binary over TCP protocol.

To satisfy the requirement of the first client let's create a web service. Web
services use HTTP protocol and XML message format. So interoperability requirement
of the first client will be met. Web services can be consumed by any client built
on any platform.
To create the web service
1. Create an empty asp.net web application project and name it WebServicesDemo.
2. Right click on the project name in solution explorer and add a web service. Name
it HelloWebService.
3. Copy and paste the following code HelloWebService.asmx.cs.

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;
}
}
}

Build the solution.

Creating a client for the Web Service.


1. Right click on WebServicesDemo solution in solution explorer and add a new
asp.net empty web application project and name it HelloWebApplication.
2. Right click on References folder in HelloWebApplication project and select Add
Service Reference option. In Add Service Reference dialog box click the Discover
button. In the namespace textbox type HelloWebService and click OK. This should
generate a proxy class to invoke the HelloWebService.
3. Right click on HelloWebApplication project name in solution explorer and a new
web form. This should add WebForm1.aspx
4. Copy and paste the following HTML on WebForm1.aspx
<table style="font-family:Arial">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Get Message"
onclick="Button1_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Font-Bold="true">
</asp:Label>
</td>
</tr>
</table>
5. Copy and paste the following code in WebForm1.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
HelloWebService.HelloWebServiceSoapClient client = new
HelloWebService.HelloWebServiceSoapClient();

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

In Part 2, we will discuss implementing


1. A web service to exchange messages in XML format using HTTP protocol for
interoperability.
2. A remoting service to exchange messages in binary format using TCP protocol for
performance.

Along the way, we will get a feel of how different these technologies are.

Part 2 - Creating a remoting service and a web service


Suggested Videos
Part 1 - Introduction to WCF

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.

We have 2 clients and we need to implement a service a for them.


1. The first client is using a Java application to interact with our service, so
for interoperability this client wants messages to be in XML format and the
protocol to be HTTP.
2. The second client uses .NET, so for better performance this client wants
messages formatted in binary over TCP protocol.

To satisfy the requirement of the first client let's create a web service. Web
services use HTTP protocol and XML message format. So interoperability requirement
of the first client will be met. Web services can be consumed by any client built
on any platform.
To create the web service
1. Create an empty asp.net web application project and name it WebServicesDemo.
2. Right click on the project name in solution explorer and add a web service. Name
it HelloWebService.
3. Copy and paste the following code HelloWebService.asmx.cs.

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.

Part 3 - Creating a wcf service


Suggested Videos
Part 1 - Introduction to WCF
Part 2 - Creating a remoting service and a web service

This is continuation to Part 2. Please watch Part 2 from WCF video tutorial before
proceeding.

In this video, we will discuss


1. Creating a WCF service
2. Hosting the WCF service using a console application
3. Exposing 2 service endpoints.
4. Creating a windows and a web Client applications.

Let's take the scenario that we discussed in Part 2.


We have 2 clients and we need to implement a service a for them.
1. The first client is using a Java application to interact with our service, so
for interoperability this client wants meesages to be in XML format and the protocl
to be HTTP.
2. The second client uses .NET, so for better performance this client wants
messages formmated in binary over TCP protocol.

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.

Creating the WCF Service:


1. Create a new Class Library Project and name it HelloService.
2. Delete Class1.cs file that is auto-generated.
3. Add a new WCF Service with name = HelloService. This should automatically
generate 2 files (HelloService.cs & IHelloService.cs). Also a reference to
System.ServiceModel assembly is added.
4. Copy and paste the following code in IHelloService.cs file
using System.ServiceModel;
namespace HelloService
{
[ServiceContract(Namespace="http://PragimTech.com/ServiceVersion1")]
public interface IHelloService
{
[OperationContract]
string GetMessage(string name);
}
}
5. Copy and paste the following code in HelloService.cs
namespace HelloService
{
public class HelloService : IHelloService
{
public string GetMessage(string name)
{
return "Hello " + name;
}
}
}

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.

Hosting the WCF service using a console application.

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;
}
}
}

Build the solution.

Creating a client for the Web Service.


1. Right click on WebServicesDemo solution in solution explorer and add a new
asp.net empty web application project and name it HelloWebApplication.
2. Right click on References folder in HelloWebApplication project and select Add
Service Reference option. In Add Service Reference dialog box click the Discover
button. In the namespace textbox type HelloWebService and click OK. This should
generate a proxy class to invoke the HelloWebService.
3. Right click on HelloWebApplication project name in solution explorer and a new
web form. This should add WebForm1.aspx
4. Copy and paste the following HTML on WebForm1.aspx
<table style="font-family:Arial">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Get Message"
onclick="Button1_Click" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Font-Bold="true">
</asp:Label>
</td>
</tr>
</table>
5. Copy and paste the following code in WebForm1.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
HelloWebService.HelloWebServiceSoapClient client = new
HelloWebService.HelloWebServiceSoapClient();

Label1.Text = client.GetMessage(TextBox1.Text);
}

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");
}

private void button1_Click(object sender, EventArgs e)


{
label1.Text = client.GetMessage(textBox1.Text);
}
}
}

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.

Can we use .NET Remoting to build a Web service?


Yes.

<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.

1. Create a new asp.net empty web application and name it HelloWebClient


2. Right click on References folder and select Add Service Reference option. In the
address textbox type http://localhost:8080/ and click on GO button. In the
namespace textbox type HelloService and click OK. This should generate a proxy
class to communicate with the service.
3. Add a new webform. Copy and paste the following HTML in WebForm1.aspx
<div style="font-family:Arial">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Get Message"
onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server" Font-Bold="true"></asp:Label>
</div>
4. Copy and paste the following code in WebForm1.aspx.cs file
protected void Button1_Click(object sender, EventArgs e)
{
HelloService.HelloServiceClient client = new
HelloService.HelloServiceClient("BasicHttpBinding_IHelloService");
Label1.Text = client.GetMessage(TextBox1.Text);
}

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.

5. Rename Class1.cs file to HelloRemotingService.cs. Copy and paste the following


code in HelloRemotingService.cs file.
using System;
namespace HelloRemotingService
{
public class HelloRemotingService : MarshalByRefObject,
IHelloRemotingService.IHelloRemtingService
{
public string GetMessage(string name)
{
return "Hello " + name;
}
}
}
6.
2. Right click on References folder and select Add Service Reference option. In the
address textbox type http://localhost:8080/ and click on GO button. In the
namespace textbox type HelloService and click OK. This should generate a proxy
class to communicate with the service.
3. On Form1, drag and drop a textbox, a button and a label control. Double click
the button to generate the click event handler.
4. Copy and paste the following code in Form1.cs file
private void button1_Click(object sender, EventArgs e)
{
HelloService.HelloServiceClient client = new
HelloService.HelloServiceClient("NetTcpBinding_IHelloService");
label1.Text = client.GetMessage(textBox1.Text);
}

In this video we will discuss


What is WCF
Why should we use WCF

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.

What is a distributed application?


In simple terms a distributed application, is an application where parts of it run
on 2 or more computers. Distributed applications are also called as connected
systems.

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

Why build distributed applications?


There are several reasons for this
1. An enterprise application may need to use the services provided by other
enterprises. For example an ecommerce application may be using Paypal service for
payments.
2. For better scalability. An enterprise web application may have Presentation
tier, Business tier, and Data Access tier, and each tier may be running on a
different machine.

What is an interoperable application?


An application that can communicate with any other application that is built on any
platform and using any programming language is called as an interoperable
application. Web services are interoperable, where as .NET remoting services are
not.

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

Why should we use WCF?


Let's take this scenario
We have 2 clients and we need to implement a service a for them.
1. The first client is using a Java application to interact with our service, so
for interoperability this client wants messages to be in XML format and the
protocol to be HTTP.

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

asp.net web service example

2. To satisfy the second client requirement we end up implementing a remoting


service

Dot Net Remoting Example

You might also like