WCF

WCF Examples

WCF

WCF Projects

WCF Project

First WCF Service-2nd part
Previous Home Next
A. Developing the Server application

Step 1 : Create the application

  1. Open your development environment and create a new C# console application project named server.
  2. Add reference to the System.ServiceModel.dll , then add reference to the System.ServiceModel namespace
  3. Change the name space to EgyAntiques.Store as the
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace EgyAntiques.Store
{
}

Step 2 : Create the Service Contracts

  1. Define the interface that will serve as a contract for our application , decorate your interface with ServiceContract Attribute
  2. Define 2 methods signature as in the following code , those methods will serve as the contract operations , decorate your methods with the OperationContract attribute

 

[ServiceContract]
public interface IAntiques
{
[OperationContract]
string DisplayAntiquesList();
[OperationContract]
double GetAntiquePrice(int antiqueID);
}

Step 3 : Create the Service Implementation Code

  1. Define AntiquesService Class which will implement the IAntiques interface
  2. Provide Implementation for the 2 methods as in the following code
public class AntiquesService : IAntiques
{
public string DisplayAntiquesList()
{
string str = " 1.A \n 2.B \n 3.c \n 4.D";
return str;
}
public double GetAntiquePrice(int antiqueID)
{
switch (antiqueID)
{
case 1:
return 100.05;
break;
case 2:
return 150.00;
break;
case 3:
return 340.23;
break;
case 4:
return 1000;
break;
default:
return 00.00;
break;
}
}
}

Step 4: Create the Service Host

Add main method to the class ,which will create a new ServiceHost object and open this host , the host will not run untill we specify the endpoints that will allow the other services to communicate with our AntiquesService , this part we will do using configuration files.

static void Main(string[] args)
{
ServiceHost sh = new ServiceHost(typeof(AntiquesService));
try
{
sh.Open();
Console.WriteLine("EgyAntiques Store Service opened successfully");
Console.WriteLine("Press Enter to terminate EgyAntiques Store Service");
Console.ReadLine();
}
finally
{
sh.Close();
}

Step 5: Create the Service Configurations

  1. Build your project and make sure that it successfully built
  2. Open the Service Configuration Editor (Tool come with Windows SDK ) and load the application file , from File Open à Executable - Browse for Server.exe
  3. Click create new service , and specify the service type as "EgyAntiques.Store.AntiquesService"
  4. Specify the Service Contract for the service as "EgyAntiques.Store.IAntiques"
  5. Set the communication mode for HTTP
  6. Set the method of interoperability to Advanced Web service Interoperability , and set the communication Simplex Communication
  7. Left the address field for your endpoint empty in this case the WCF will search for base address for the service and will consider this address as address for the endpoint
  8. Click finish, you should see the following result by now
  9. Now click from the left pane the Host Icon , then click New to add new base address for your service, then set the address for your service to http://r4r-03/service
  10. Expand the Endpoints from the left pane and select your end point , then in the name field write “ep1” this is the name for your endpoint in will be used to reference this endpoint configuration from inside code
B. Publish the Service MetaData
  1. Expand from the left pane the Advanced node , then click Service Behaviors node and click New Service Behavior Configuration from the right pane.
  2. Set the BehaviorConfiguration name to "MetaDataBehavior" , and click add to add Behavior element , then select ServiceMetaData
  3. Select the serviceMetaData from the left pane , on the right pane set the HttpGetEnabled to true and the HttpGetUrl to http://r4r-03/service

  4. Now Select the EgyAntiques.Store.AntiqueService node from the left pane then set the Behavior Configuration in the right pane to your Behavior name "MetaDataBehavior”"
  5. Now you can run your service , it should run ok without any errors , if everything is alright you’ll see this screen
C. Create .net Based Client Application
  1. Open command shell and navigate to the required folder you need the tool to generate the files in , then write the following command and hit enter: Svcutil.exe http://localhost:8000/AntiqueService/?wsdl

    You should see the following result , and if you got to the folder you had specified for the tool you will find 2 files (1) AntiquesService.cs (2) output.config

  2. Now create new console project name it "client", set the name space to "EgyAntiques.Store" , then add reference to the "System.ServiceModel.dll", finally add the generated files to your project "AntiquesService.cs" and rename the configuration file to "app.config" then add it to the project ,the result should look like that
  3. You only need to create proxy object from your generated proxy , then call your service methods from the proxy , here is the code you need to add to the main method of your client:
    Console.WriteLine("Please press enter to get the list of our antiques :");
    Console.ReadLine();
    AntiquesClient client = new AntiquesClient("ep1");
    Console.Write(client.DisplayAntiquesList());
    Console.WriteLine("\n\n\nPlease select your Antique # to know it's price :");
    Console.WriteLine("Your Antique Price is:"+client.GetAntiquePrice
                    (int.Parse(Console.ReadLine())));
    Console.WriteLine("\npress enter to terminate the program ... ");
    Console.ReadLine();
    
  4. Save and compile your application , check if there is errors , if successfully built , run the server application , then run your client application , you should get the following result

CODE FOR SERVER:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace EgyAntiques.Store
{
[ServiceContract]
public interface IAntiques
[OperationContract]
string DisplayAntiquesList();
[OperationContract]
double GetAntiquePrice(int antiqueID);
}
public class AntiquesService : IAntiques
{
public string DisplayAntiquesList()
{
string str = " 1.A\n 2.B \n 3.C \n 4.D";
return str;
}
public double GetAntiquePrice(int antiqueID)
{
switch (antiqueID)
{
case 1:
return 100.05;
break;
case 2:
return 150.00;
break;
case 3:
return 340.23;
break;
case 4:
return 1000;
break;
default:
return 00.00;
break;
}
}
static void Main(string[] args)
{
ServiceHost sh = new ServiceHost(typeof(AntiquesService));
try
{
sh.Open();
Console.WriteLine("EgyAntiques Store Service opened successfully");
Console.WriteLine("Press Enter to terminate EgyAntiques Store Service");
Console.ReadLine();
}
finally
{
sh.Close();
}
}
}
}

CODE FOR CLIENT:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace EgyAntiques.Store

{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please press enter to get the list of our antiques :");
Console.ReadLine();
AntiquesClient client = new AntiquesClient("ep1");
Console.Write(client.DisplayAntiquesList());
Console.WriteLine("\n\n\nPlease select your Antique # to know it's price :");
Console.WriteLine("Your Antique Price is : " + client.GetAntiquePrice
				 (int.Parse(Console.ReadLine())));
Console.WriteLine("\npress enter to terminate the program ... ");
Console.ReadLine();
}
}
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: