WCF Simple to use

Source: Internet
Author: User

1. The following is a simple example of a service to understand WCF
1. New project, name IBLL, solution name Wcfdemo, template Select Class Library
2. Modify the Class1.cs file name as IUserInfoService.cs
3. Add Reference System.ServiceModel
4. Modify the IUserInfoService.cs code as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.ServiceModel;usingSystem.Text;usingSystem.Threading.Tasks;namespaceibll{[ServiceContract] Public InterfaceIuserinfoservice {[OperationContract]intADD (intAintb); }}

We define a Iuserinfoservice interface, and notice that the ServiceContract feature is declared on the interface, that is, the service contract, which indicates that the interface is a service. The OperationContract attribute is declared on the method, which means that the method is a service method of Iuserinfoservice, and the client can call the method remotely

5. New project, name BLL, template Select Class Library, modify Class1.cs filename called UserInfoService.cs, reference project IBLL, code as follows:

usingIBLL;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebll{ Public classUserinfoservice:iuserinfoservice { Public intADD (intAintb) {returnA +b; }    }}

OK, to this our service code has been written to complete, below we must provide a running host for the service, through the host program to start our services.
6. Create a new project under the same solution with the name Wcfhost and the console application type

Add a reference to the 7.WcfHost project, reference the project IBLL, and then add the reference: System.ServiceModel

8. Modify the Program.cs code as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.ServiceModel;usingSystem.Text;usingSystem.Threading.Tasks;namespacewcfhost{classProgram {Static voidMain (string[] args) {            using(ServiceHost host =NewServiceHost (typeof(BLL. Userinfoservice)) {host.                Open (); Console.WriteLine ("Service is started");                Console.readkey (); Host.            Close (); }        }    }}

Above, we have implemented the service and provided a running host for the service, that is, the contract part has been completed, below we specify the address and binding for the service
9. Modify the app. config content as follows:

<?xml version="1.0"encoding="Utf-8"?><configuration> <system.serviceModel> <services> <service name="BLL. Userinfoservice"behaviorconfiguration="behaviorconfiguration">< Objects!--Services--"http://localhost:8000/"/><!--Service IP and port numbers--</baseAddresses> ""binding="BasicHttpBinding"contract="IBLL. Iuserinfoservice"></endpoint><!--Contract: Service contract-</service> </services> <behaviors> <serviceBehaviors> <behavior name="behaviorconfiguration"> <servicemetadata httpgetenabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.servicemodel></conf Iguration>

10. Set the Wcfhost project as the startup project and start debugging. After the console display service is started, open the browser input service address: Http://localhost:8000/, the browser will open our service page, which means that our service has been successfully started, the client can access our services through this address.
Below, we will create a client to access our services
11. Create a new project under the same solution named WCFClient, type Console application, add Service Reference address: http://localhost:8000/, client code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacewcfclient{classProgram {Static voidMain (string[] args) {servicereference1.userinfoserviceclient Client=Newservicereference1.userinfoserviceclient (); Console.WriteLine (client. ADD (3,4));        Console.readkey (); }    }}

12. Start Wcfhost, start WCFClient, (Remember to find the Bin/debug. EXE to run as Administrator

WCF Simple to use

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.