मुख्य सामग्रीवर वगळा

Create WCf service using 2 end points

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

टिप्पण्या

या ब्लॉगवरील लोकप्रिय पोस्ट

How To Customize Password Policy in ASP.Net Identity   Nimit Joshi   Jan 17  2014   Article 3 9 132.8k facebook twitter linkedIn google Plus Reddit Expand Download Free Office API Introduction Microsoft has released a new version of ASP.NET Identity, 2.0.0-alpha 1. You can refer to  ASP.NET Identity Preview  to get the idea of this release. In that context, we are here today to create a MVC application using ASP.NET Identity to customize the password to provide better security to the application. You'll learn here to modify the minimum length of the password and to make the entering of numeric and special characters in the password mandatory and disallow recently used passwords. By default the ASP.NET Identity requires that the minimum length of the password is 6 characters and here we change it to 8. We'll work on the Visual Studio 2013 version and you must update all the assemblies rela...

WCF Windows communication foundation

WCF:Why WCF Actually, WCF is going to unify all in one : web services : enterprise service : Remoting 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.     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     Why build distributed applications? There are several reasons for this 1.  An enterprise application ma...

Dependency Inversion Principle, Dependency Injection and Inversion Of Control

Dependency Inversion Principle, Dependency Injection and Inversion Of Control I’m a big fan of Dependency Injection pattern since it allows me to create more testable, maintainable and loosely coupled components. While it is easy to apply this pattern if you’ve seen some code samples you need more time to understand principles and terms around this pattern. In this article I will try to illustrate differences between those terms and explain how they come to work together. I will use well known car/engine example in this article but any example with dependency will do. All examples are written in C# language. Let’s first define few example classes: public class Engine { } public class Car { private Engine _engine ; public Car ( ) { _engine = new Engine ( ) ; } } n the example above we can see that Car (higher level component) depends on Engine (lower level component). At some point we may need to make car more reusable, for examp...