Programming WCF services translation notes (2)

Source: Internet
Author: User
Tags msmq

Chapter 1st of programming WCF services introduces the basic knowledge of WCF.

In WCF, the most important concept is service ). The definition of a service in the book is "a service is a set of functions exposed ". Although most services exist in the form of contract interfaces, the definition of services is generally similar to the idea of "interface-oriented programming", but more importantly, we must understand the meaning of services. The traditional programming method can encapsulate the business logic as an object when understanding the business logic. This object provides business-related functions; however, WCF programming focuses more on how to provide business services and the consumer-client of services.
Therefore, in the idea of service-oriented programming, the role of calling services has undergone fundamental changes. Take the hotel reservation business as an example. If it is a traditional object-oriented programming, when writing business cases, call the role of the hotel reservation object as Customer ,:
 
For WCF Service programming, the provider and consumer of the service must be considered first. For the hotel reservation business, the service provider defines and releases the hotel reservation service, and the client calls the service ,:

 
The most important thing about WCF is that it allows clients to communicate with services across execution boundaries. On the same machine, the client can call services in the same application domain, or call services across application domains or even processes in the same process ." Clients can also access services across machines, or even "interact with services across the boundaries of the Intranet or Internet ."
With traditional distributed technologies such as DCOM and. net remoting is different. No matter whether the service object is local or remote, WCF provides a completely consistent programming model, that is, through proxy. The advantage of doing so is: "It makes it possible for developers not to affect the client because of service location changes, while greatly simplifying the programming model of the application ".

In WCF, to access the WCF Service, you must know the address of the service ). The book briefly introduces several service addresses: TCP address, HTTP address, IPC address, MSMQ address, and peer network address. When defining a service, you must specify the base address. The base address format is:
[Transfer Protocol]: // [machine name or domain name] [: Optional port]
The book lists several common addresses:
Http: // localhost: 8001
Http: // localhost: 8001/myservice
Net. TCP: // localhost: 8002/myservice
Net. Pipe: // localhost/mypipe
Net. MSMQ: // localhost/private/myservice
Net. MSMQ: // localhost/myservice
The first two addresses are the HTTP address, the third is the TCP address, the fourth is the IPC address, the named pipe is used, and the last two are the MSMQ addresses.
After a base address is defined, you can specify a URI for each service. Add the specified URI to the base address of the service. The address description will be detailed in chapter 2.
Chapter 2 briefly introduces the contract ). "All the services in WCF are exposed as contract ). Contracts are platform-independent and are the standard way to describe service functions ." WCF defines four types of contracts: service contract, data contract, fault contract, and message contract ).
This book will introduce the service contract and data contract in Chapter 2nd and Chapter 3rd respectively, and Chapter 6th introduces the wrong contract. As for the message contract, it is not within the scope of this book.
The service contract is defined as servicecontract, which can be applied to both interfaces and services. The best practice is to apply features to interfaces, such:
[Servicecontract]
Interface imycontract
{
[Operationcontract]
String mymethod (string text );

// Will not be part of the contract
String myothermethod (string text );
}
To define a service type member in a service contract, you must combine servicecontract and operationcontract features. When defining a service contract, we should focus on service provision rather than business implementation. Especially in the concept of a service contract, whether it is a service contract is not necessarily related to the access limitation of objects. For example, the ticket booking service contains two methods: Subscribe () and settle (). For business objects, both methods need to be exposed as public methods. But for the service definition, the service consumer only needs to care about the subscribe () method. In this case, the ticket booking service interface should be defined as follows:
[Servicecontract]
Public interface isubscribeticket
{
[Operationcontract]
Bool subscribe (customer );

Void settle (double money );
}
In this case, only the subscribe () method is exposed in the service contract.
A separate class can support multiple contracts by inheriting or implementing multiple interfaces that apply the servicecontract feature.
The book specifically states: "You must avoid using constructors with parameters when implementing (services), because WCF can only use Default constructors ."
The service name and namespace can be specified through the name and namespace attributes of the servicecontract feature. If not specified, the default namespace is the http://tempuri.org, and the default service name is the interface Name (or service class name if the servicecontract feature is applied directly to the class ).

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.