First, Introduction
WCF, a Windows Communication Foundation abbreviation, is a distributed communications programming framework that Microsoft provides for building service-oriented applications and is an important part of the. NET Framework 3.5. With this framework, developers can build enterprise-level interconnect application solutions that are cross-platform, secure, reliable, and support transactions.
According to Microsoft's official explanation, WCF (the previous version named "Indigo") is a unified framework for building and running service oriented applications using managed code. It enables developers to build a cross-platform, secure, trustworthy, transactional solution that can collaborate with existing systems. WCF is a synthesizer of Microsoft's distributed application development, which integrates. NET platform and distributed systems, such as. NET Remoting, ASMX, WSE, and MSMQ. In the context of communication (communication), it can span processes, across machines, across subnets, corporate networks, and the Internet, and as a host program, it can be asp.net,exe,wpf,windows forms,nt service,com+ As the host. The protocols that WCF can support include tcp,http, cross processes, and customizations, while security patterns include SAML, kerberos,x509, user/password, customization, and many other standards and patterns. That is, under the WCF framework, it is easy to develop an soa-based distributed system, with Microsoft incorporating all of the technical elements associated with it, and mastering WCF as a key to opening the door to SOA.
WCF is an extension of the. NET Framework, which provides a unified framework for creating secure, reliable, transactional services, and WCF consolidates and extends development technologies for existing distributed systems, such as Microsoft. NET Remoting, Web Services, web Services Enhancements (WSE), etc., to develop a unified and reliable system. WCF simplifies the application of the SOA framework, while also unifying technologies such as Enterprise Services, messaging,. NET Remoting, Web Services, WSE, and so on, which greatly facilitates the development and deployment of WCF applications by developers. Also reduces the complexity of WCF application development.
WCF supports a large number of Web service standards, including XML, XSD, SOAP, Xpath, WSDL, and other standards and specifications, so developers can easily migrate to existing standards. WCF can also use attribute attributes for WCF application configuration, which improves the flexibility of WCF applications. WCF follows the client/server model to communicate between applications, and client programs can access services directly from the endpoint side provided by the server side.
WCF is a unified framework for creating distributed transaction processing applications that are both secure and interoperable. In earlier versions of Visual Studio, there were some techniques that could be used to communicate between applications. If you want to share information in a way that enables you to access information from any platform, you should use a Web service (also known as ASMX Web Service). If you want to move data only between the client and the server that is running on the Windows operating system, you should use. NET Remoting. If you need transactional communication, you should use Enterprise Services (DCOM), or if you need a queued model, you should use Message Queuing (also known as MSMQ).
WFC brings together the functionality of all of these technologies into a unified programming model. This simplifies the process of developing distributed applications.
Second, WCF framework model
Three, WCF programming model
The WCF programming model WCF programming model is based on communication between the following two entities: WCF services and WFC clients. The programming model is encapsulated in the System.ServiceModel namespace of the. NET Framework.
WCF Services
A WCF service is based on an interface that defines a contract between a service and a client. It is marked with the ServiceContractAttribute property, as shown in the following code:
<ServiceContract()> _
Public Interface IService1
<OperationContract()> _
Function GetData(ByVal Value As String) As String
End Interface
You can define these functions and methods by using the OperationContractAttribute property to mark the functions and methods exposed by the WCF service. In addition, you can expose serialized data by using the DataContractAttribute property to mark a composite type. This enables data binding in the client. After the interfaces and their methods are defined, they are encapsulated in a class that implements the interface. A single WCF service class can implement multiple service agreements. In order to use WCF services, this service is exposed through a commonly called "endpoint". Only the methods provided by the endpoint can communicate with the service, and you cannot access the service by using a direct reference as you would access other classes.
Endpoints are made up of addresses, bindings, and contracts. The address defines the location of the service, which can be a URL, an FTP address, a network path, or a local path. A binding defines a method for communicating with a service. WCF bindings provide a common model for specifying protocols such as HTTP and FTP, security mechanisms such as Windows authentication or user names and passwords, and more. The contract includes operations exposed by the WCF service class.
Multiple endpoints can be exposed for a single WCF service. This allows different clients to communicate with the same service in different ways. For example, a banking service can provide an endpoint for an employee, another endpoint for external customers, and each endpoint using a different address, binding, and/or contract.
WCF Client
WCF clients are composed of "agents" and endpoints that enable an application to communicate with a WCF service, which matches an endpoint that is defined for a service. The agent builds on clients in the app.config file and includes information about the types and methods exposed by the service. For example, for services that expose multiple endpoints, clients can choose the service that best meets their needs, to communicate over HTTP, and to use Windows authentication (this article is published in the programming portal---www.bianceng.cn).
After you create a WCF client, you can reference the service in code as you would any other object. For example, to invoke the GetData method that was previously displayed, you should write code that is similar to the following:
Dim client As New ServiceReference.Service1Client
Dim returnString As String
returnString = client.GetData("Hello")
MsgBox(returnString)
Four, WCF tools in Visual Studio
To create and test a WCF service
You can quickly create your own services based on the WCF Visual Studio template. You can then use the WCF Service Automation host and the WCF test client to debug and test this service. By using these tools together, you can quickly and easily debug and test, shortening debugging and testing cycles, and not having to commit to the host model at an early stage.
WCF templates
The WCF Visual Studio template provides a base class structure for service development. You can use some WCF templates in the Add New Project dialog box. These templates include WCF Service library projects, WCF Service Web sites, and WCF service item templates. When you select a template, files are added for service contracts, service implementations, and service configurations. All of the required attributes have been added, and a simple "Hello world" service type will be created without the need to write any code. Of course, you will need to add code to provide the functions and methods for the actual service, but the template will provide the basics.
WCF Service Host
When you start the Visual Studio debugger for a WCF service project (by pressing F5), the WCF Service Host tool is automatically started to host the service locally. The WCF Service host enumerates the services in the WCF Service project, loads the configuration of the project, and instantiates the host for each service it finds. By using the WCF Service host, you can test the WCF service without writing additional code during development or submitting it to a specific host.
WCF Test Client
By using the WCF Test Client tool, you can enter test parameters, submit the input to a WCF service, and view the response that the service sends back. Combining a WCF test client with a WCF service host provides a satisfactory service testing experience. When you press F5 to debug a WCF service project, the WCF test client opens and displays a list of service endpoints defined in the configuration file. You can test the parameters and start the service, repeating this process to continuously test and verify your service.
accessing WCF services in Visual Studio
Visual Studio 2008 simplifies the task of creating WCF clients and automatically generates agents and endpoints for services added by using the Add Service Reference dialog box. All required configuration information is added to the app.config file. In most cases, you can use this service simply by instantiating the service. By using the Add Service Reference dialog box, you can enter the address of the service or search for services defined in the solution. The dialog box returns a list of services and the operations provided by those services. By using this dialog box, you can also define namespaces for referencing services in your code. You can customize the configuration of a service by using the Configure Service Reference dialog box. You can change service addresses, specify access levels, asynchronous behavior and message contract types, and configure type reuse.