Previous | Home | Next |
Step 1 : Create the application
- Open your development environment and create a new C# console application project named server.
- Add reference to the System.ServiceModel.dll , then add reference to the System.ServiceModel namespace
- 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
- Define the interface that will serve as a contract for our application , decorate your interface with ServiceContract Attribute
- 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
- Define AntiquesService Class which will implement the IAntiques interface
- 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
- Build your project and make sure that it successfully built
- Open the Service Configuration
Editor (Tool come with Windows SDK ) and load the application file , from File
Open ą Executable - Browse for Server.exe
- Click create new service , and specify the service type as "EgyAntiques.Store.AntiquesService"
- Specify the Service Contract for the service as "EgyAntiques.Store.IAntiques"
- Set the communication mode for HTTP
- Set the method of
interoperability to Advanced Web service Interoperability , and set the
communication Simplex Communication
- 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
- Click finish, you should see the following result by now
- 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
- 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
- Expand from the
left pane the Advanced node , then click Service Behaviors node and
click New Service Behavior Configuration from the right pane.
- Set the BehaviorConfiguration name to "MetaDataBehavior" , and click add to add Behavior
element , then select ServiceMetaData
- Select the serviceMetaData
from the left pane , on the right pane set the HttpGetEnabled to true and the
HttpGetUrl to http://r4r-03/service
- 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”"
- Now you can run
your service , it should run ok without any errors , if everything is alright
you’ll see this screen
- 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
- 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
- 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();
- 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 |