Rookie eyes in the WCF (1)

Source: Internet
Author: User
Tags baseuri

WCF This hot-won topic, has been around by the small partners mentioned, then it is exactly what is sacred? Let's take a look at the whole thing !

first, what is it?

Windows communicationfoundation (WCF) is a series of application frameworks developed by Microsoft that support data communication and can be translated into the Windows Communications development platform.

Integration of the original Windows communication of the. Netremoting,webservice,socket mechanism, and the integration of HTTP and FTP-related technologies.

is the best practical way to develop distributed applications on the Windows platform.

Simple boils down to four of the most

1>: The Protocol for network services, that is, what network protocols are used to open client access.

2>: An agreement for a business service that declares what business the service provides.

3> The data type declaration, which is the data portion of the client-to-server communication is consistent.

4> transport security-related definitions. It is. NET Framework, introduced by. NET Framework3.0, is in parallel with Windows Presentation Foundation and Windows Workflowfoundation for next-generation Windows operating systems and WinFX's three major application development class libraries.

second, how to use?

The end of the paper is shallow, then through the practice to feel the role of WCF!

(i) Create a new solution that consists of two projects, one named Server, another Client, and a natural pair.

Create a new two console project, and when you're finished, your vs should look similar to the one shown.

(b) Select the "Server" project , do not make a mistake, in general, we are the first to complete the server side.

(1) Right-click on the "References" of the server Project and select "Add Reference ..." from the shortcut menu, then confirm the selection in the window that opens. NET tab, locate System.ServiceModel in the list, and then click OK, this does not need me to introduce.

(2) Open the Program.cs file for the server Project , first of all, to introduce several namespaces that might be used.

Usingsystem.servicemodel; UsingSystem.ServiceModel.Description;         

(3) defines a service contract that contains a TestMethod method, which is an interface.

[ServiceContract] public Interfaceiservice {     [OperationContract]     

We see that the service contract is an interface, but the ServiceContractAttribute feature is attached, and the methods in the interface attach the OperationContractAttribute feature, as a service operation, Can be called by a client program without exposing the method to the client if the OperationContractAttribute attribute is not added.

(4) define a class and implement the service contract defined above.

Public Classmyservice:iservice {public  string TestMethod ()     {         return "believe yourself to be the best!" ";     

(5) define the server-related parameters in the main entry point and start the service.

Static Voidmain (string[] args) {//Base address URI, must, http scheme uri BaseURI = Newuri ("Http://localhost:8008/Service"); using (ServiceHost host = Newservicehost (typeof (MyService), BaseURI)) {//Add endpoint to server Wshttpbinding Bindi         ng = newwshttpbinding (); Security verification binding is not required here.        Security.mode =securitymode.none; Host.         AddServiceEndpoint (typeof (IService), binding, "my");             To allow VS to generate the client code, the WSDL document, add the following behavior ServiceMetadataBehavior mdbehavior =new ServiceMetadataBehavior () {        Httpgetenabled = true}; Host.          DESCRIPTION.BEHAVIORS.ADD (Mdbehavior); If the service starts smoothly, prompt to process the opened event host. Opened + = (sender, E) =>console.writeline ("The service is started.         "); Start the server try {host.         Open (); } catch (Exception ex) {Console.WriteLine (ex.         Message);         }//In order to let the program do not go down to the end, so add this sentence console.readkey (); Shut down server host.     Close (); } }  

At this point, first confirm that the server project is the currently selected item and then run, and if successful, you will see the content as shown.

This is the congratulations, the server side we have finished, the next round to the client

(iii) Select the client project , right-click on the reference, and choose Add Service Reference from the shortcut menu

in the pop-up window, enter the base address you just defined, which is http://localhost:8008/Service

Remember to use the base address, which is the one you use to create the ServiceHost instance, and do not use endpoint addresses.

Click the "Go" button, after the service is read correctly, enter the name of the namespace you want, and click OK.

At this point, we can write the code on the client.

Static Voidmain (string[] args) {     WS. ServiceClient client = Newws.serviceclient ();    Console.WriteLine (client. TestMethod ());      

Running, our first WCF application is complete.

third, the advantage

(i) Unity

as already described, WCF is the integration of technologies such as Asmx,.net REMOTING,ENTERPRISESERVICE,WSE,MSMQ. Because WCF is written entirely by managed code, the application that develops WCF is not much different from developing other. NET applications, and we can still use WCF to create service-oriented applications like creating object-oriented applications.

(ii) Interoperability

because the most basic communication mechanism for WCF is soap (Simple Object Accessprotocol, easy objects Access Protocol), this guarantees interoperability between systems, even in different contexts. This communication can be based on the. NET-to. NET communication.
(iii) safety and trustworthiness

Ws-security,ws-trust and Ws-secureconversation are added to the SOAP message for user authentication, data integrity verification, data privacy and many other security factors.

(iv) Compatibility

WCF takes full account of compatibility with legacy systems. Installing WCF does not affect legacy technologies such as ASMX and. Netremoting. Even for WCF and ASMX, although both use SOAP, WCF-developed-based applications can still interact directly with ASMX.

Rookie eyes in the WCF (1)

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.