WCF Easy Tutorial

Source: Internet
Author: User
Tags readline hosting

A lot of examples of WCF learning, it feels good, but because everyone is learning different emphases, some very detailed details such as what each attribute is used to do, the establishment of different types of projects to create a service what difference, etc., are unknown. Finally, an introductory tutorial was found on MSDN. Explain the very basic, very detailed, want to go into every detail, but I thoroughly understand the introduction of every detail, the entire tutorial structure clear, concise code, detailed explanation, worthy of recommendation.

Address: http://msdn.microsoft.com/en-us/library/ms734712.aspx

Do this in 5 parts to explain the creation of a basic based on B/s framework of WCF applications. The service returns the results of the subtraction operation of these two digits, based on the two digits entered.

First step: Define WCF Service Contracts (Create project, add Reference, define interface)
Part II: Introducing WCF Service Contracts (adding specific service functions)
Part III: architecting WCF services, running WCF services (adding URIs, defining service object addresses, Run the Service
Part fourth: Use tools to access services, automatically generate the WCF Service Agent code files
, part fifth: Configure a simple WCF client (using the client to introduce a service agent to access the service through the service agent)
Sixth: Run program how to:define a Windows Communication Foundation Service Contract how to:implement a Windows Communication Foundation service Contract Ho W to:host and Run a Basic Windows communication Foundation Service
How to:create a Windows communication Foundation Client
How to to:configure a Basic Windows communication Foundation Client
How to:use a Windows communication Fo Undation Client

The

establishes a solution first.
Create a console application project called Server under this solution, and then build a console application project called client.
Add a reference to each item individually to System.ServiceModel
Edit each item under Program.cs

Using System; Using System.ServiceModel; Using System.ServiceModel.Description; namespace Microsoft.ServiceModel.Samples {//Define a service contract. [ServiceContract (Namespace = "http://Microsoft.ServiceModel.Samples")] public interface ICalculator {[ OperationContract] Double Add (double n1, double n2); [OperationContract] Double subtract (double n1, double n2); [OperationContract] Double Multiply (double n1, double n2); [OperationContract] Double Divide (double n1, double n2); }//service class that implements the service contract. Added code to write output to the console window. public class Calculatorservice:icalculator {public double Add (double n1, double n2) {Double result = n1 + N2; Console.WriteLine ("Received Add ({0},{1})", N1, N2); Console.WriteLine ("Return: {0}", result); return result; Public double subtract (double n1, double n2) {double result = n1-n2; Console.WriteLine ("Received Subtract ({0},{1})", N1, N2); Console.WriteLine ("Return: {0}", result); return resUlt Public double Multiply (double n1, double n2) {Double result = N1 * N2; Console.WriteLine ("Received Multiply ({0},{1})", N1, N2); Console.WriteLine ("Return: {0}", result); return result; Public double Divide (double n1, double n2) {double result = n1/n2; Console.WriteLine ("Received Divide ({0},{1})", N1, N2); Console.WriteLine ("Return: {0}", result); return result; } class Program {static void Main (string[] args) {//Step 1 of the address configuration procedure:create a URI to SE Rve as the base address. Uri baseaddress = new Uri ("Http://localhost:8000/ServiceModelSamples/Service"); Step 1 of the hosting procedure:create ServiceHost ServiceHost selfhost = new ServiceHost (typeof (CalculatorService), b aseaddress); try {//Step 3 of the hosting Procedure:add a service endpoint. Selfhost.addserviceendpoint (typeof (ICalculator), new WS Httpbinding (), "CalculatorService"); Step 4 of the hosting procedure:enable metadata Exchange. ServiceMetadataBehavior SMB = new ServicEmetadatabehavior (); Smb. Httpgetenabled = true; SELFHOST.DESCRIPTION.BEHAVIORS.ADD (SMB); Step 5 of the hosting Procedure:start (and then stop) the service. Selfhost.open (); Console.WriteLine ("The service is ready."); Console.WriteLine ("Press <ENTER> to terminate service."); Console.WriteLine (); Console.ReadLine (); Close the ServiceHostBase to shutdown the service. Selfhost.close (); catch (Communicationexception CE) {Console.WriteLine ("An exception occurred: {0}", CE. message); Selfhost.abort (); } } } }

Once the server has been created, you can try it again.
This service can be accessed using the command-line tools provided by Microsoft to generate two files for service agent App.config and GeneratedProxy.cs.

path=c:/windows/microsoft.net/framework/v3.5

Datasvcutil.exe/language:csharp/out:generatedproxy.cs/config:app.config http://localhost:8000/ Servicemodelsamples/service

Note: When real server runs, the wrapper class is generated.

Of course, if it is a service that is already running, you can use the VS reference.

Add the two files to the client project.
You can now edit the client code.

Using System; Using System.Collections.Generic; Using System.Text; Using System.ServiceModel; Namespace Servicemodelsamples {class Client {static void Main () {//step 1:create a endpoint address and a instance O f the WCF Client. EndpointAddress epaddress = new EndpointAddress ("Http://localhost:8000/ServiceModelSamples/Service/CalculatorService"); Calculatorclient client = new Calculatorclient (new Wshttpbinding (), epaddress); Step 2:call the service operations. Call the ADD service operation. Double value1 = 100.00D; Double value2 = 15.99D; Double result = client. ADD (value1, value2); Console.WriteLine ("Add ({0},{1}) = {2}", value1, value2, result); Call the subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client. Subtract (value1, value2); Console.WriteLine ("Subtract ({0},{1}) = {2}", value1, value2, result); Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client. Multiply (value1, value2); Console.WriteLine ("Multiply" ({0}, {1}) = {2} ", value1, value2, result); Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client. Divide (value1, value2); Console.WriteLine ("Divide ({0},{1}) = {2}", value1, value2, result); Step 3:closing The client gracefully closes the connection and cleans up. Client. Close (); Console.WriteLine (); Console.WriteLine ("Press <ENTER> to terminate client"); Console.ReadLine (); } } }

Every detail is contained in these two Program.cs files, which you will probably see. It's much clearer than most of the textbooks in the yard, especially for beginners who are as inquisitive as I am. :)
Finally compile the program and try to run it. (two are command-line programs, directly to the compiled directory to find the EXE file run, first run the service, and then run the client).

Reference: http://www.cnblogs.com/xblues/archive/2008/07/11/1240843.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.