This series of courses comes from webcast
Author: Xu Changlong
Special msdn lecturer Vsts_china@hotmail.com
Conditions required to listen to this courseConditions required to listen to this course
* FamiliarWeb ServiceProgramming
* FamiliarVisual Studio 2005/2008
* Familiar with distributed applicationsProgramDevelopment
Create a service agreement-WCFTerms
> Message
-A message is an independent data unit, which may consist of several parts, including the message
Body and message header.
> Service
-A service is a constructor that exposes one or more endpoints, each of which has
Publish one or more service operations.
> Endpoint
-An endpoint is a structure used to send or receive messages (or perform these two operations. Final
A node contains the location (address) of the destination where the message can be sent. A node contains the location (address) of the destination where the message can be sent) A communication mechanism specification (binding) that describes how a message should be sent, a definition of a group of messages that can be sent or received at this location (or both) (Service Agreement) -This definition also describes the messages that can be sent.
-WCFThe service is made public to the outside world as a collection of endpoints.
·Service Agreements can be defined for both classes and interfaces.
·We recommend that you use interfaces because they can be directly modeled on service agreements.
· A service agreement interface has all the advantages of a hosted interface:
-The service agreement interface can expand any number of other service agreement interfaces.
-A class can implement any number of service agreements by implementing the service agreement interface.
-You can modify the implementation of a service agreement by modifying the interface, so that the service agreement can be guaranteed.
Unchanged.
-You can determine the service version by implementing the old interface and new interface. Old Client Connection
To the original version, and the new client can connect to a newer version.
· Define service agreements
-Use on classes or interfacesServicecontractattributeProperty tag
· Define service operations
-Use methodsOperationcontractattributeAttribute to mark it
· Parameters and return values
Each operation has a return value and a parameter, even if they areVoidYou can enable-each operation has a return value and a parameter, even if they areVoid. You can use the local method to pass references to an object from one object to another. However, unlike the local method, service operations do not pass references to the object, they pass only copies of objects.
-This is important because every type used in parameters or return values must be serializable. In other words, objects of this type must be converted to byte streams, it is serializable. In other words, this type of object must be able to be converted to byte streams and can be converted from byte streams to objects.
-By default, the primitive type is serializable,. NET FrameworkMany types are serializable.
Message mode of service operation
> Request/Reply
-Request/In the reply mode, the request sender (client application) will receive a response related to the request. This is the default mode, because it supports both input operations (one or more parameters are passed to this operation ), return operation is also supported. (This operation passes one or more parameters to this operation. return operation is supported. This operation returns one or more output values to the caller)
[Operationcontract]
String Hello (string greeting );
-Note: unless other basic message modes are specified, even if the service operation returnsVoid(InVisual BasicIsNothing), Also belongs to the request/Reply to message exchange.
-The result of the operation is: unless the client calls the operation asynchronously, the client stops processing until it receives the returned message, even if the message is normally empty.
· Disadvantages
-If the operation takes a long time, the client performance and response capabilities will be reduced.
· Advantages
-The response message can be returned.SoapError. This indicates that some service-related errors may occur during communication or processing.
> Unidirectional
-IfWCFThe client of the service application does not have to wait for the Operation to complete, and does not processSoapThe operation can specify the one-way message mode.
-One-way operation is performed when the client calls the operation andWCFAfter a message is written to the network, the client calls the operation andWCFWrite the message to the network and continue processing. This usually means that, unless the data sent in the outbound message is extremely large, the client will continue running almost immediately (unless an error occurs when sending the data ).
This type of message exchange mode supports event-like behavior from the client to the service application.
-To returnVoidTo specify one-way message exchange.IsonewaySet propertyTrue, Default value:False.
[Otictt (iowt)] [operationcontract (isoneway = true)]
Void Hello (string greeting );
· This method corresponds to the previous request/The reply example is the same,IsonewaySet propertyTrueThis means that even though the methods are the same, the service operation will not send a return message, and the client will return immediately when the outbound message arrives at the channel layer.
> Duplex
-Duplex mode is characterized by either one-way message transmission or request/Reply message sending method. Both the service and client can independently send messages to each other. This bidirectional communication mode is useful for services that must communicate directly with the client or provide an asynchronous experience (including event-like behavior) to any party that exchanges messages.
-Because there is an additional mechanism for communication with the client, two-way Mode Comparison request/Reply or one-way
The mode must be slightly complex.
-To design a duplex protocol, you must also design a callback protocol and assign the type of the callback protocol toServicecontractattributeAttribute(Attribute)OfCallbackcontractAttribute(Property).
-To implement the duplex mode, you must create the second interface, which contains the method declaration called by the client.
[Servicecontract (namespace = "http://Microsoft.ServiceModel.Samples ",
Sessionmode = sessionmode. required,
Callbackcontract = typeof (icalculatorduplexcallback)]
Public interface icalculatorduplex
{
[Operationcontract (isoneway = true)]
Void clear ();
}
Public interface icalculatorduplexcallback
{
[Operationcontract (isoneway = true)]
Void equals (double result );
[Operationcontract (isoneway = true)] [operationcontract (isoneway = true)]
Void equation (string eqn );
}
Demo1
CreateWCFService
1Request/Reply,2Unidirectional,3Duplex
Create data agreements create data agreements
· Service-oriented applications (suchWindows Communication Foundation
(WCF)Applications) are designed to workMicrosoftPlatform and non-MicrosoftOn the platform(WCF)Applications) are designed to workMicrosoftPlatform and non-MicrosoftOn the platform
The maximum number of client applications allowed for interoperability.
· We recommend that you useDatacontractattributeAndDatamemberattributeTo create a data contract.
· Data agreements are part of service agreements used to describe the data exchanged for your service operations
Create data agreements
[Datacontract]
Public class compositetype
{
Bool boolvalue = true;
String stringvalue = "hello ";
[Datamember]
Public boolboolvalue
{
Get {return boolvalue ;}
Set {boolvalue = value ;}
}
}
Create data agreements
· Data agreements are optional style agreements: No types or data members are serialized unless you explicitly apply the data agreement attributes.
· Data protocols and hostingCodeThe access range of is independent: The sequence data protocol can be performed on private data members, and the access range of the managed code is irrelevant. The private data members can be serialized and sent to other locations, so that they can be publicly accessed
·WCFProcess the basics used to enable the operation functionSoapDefines the message, and processes the serialization of the data type to the message body and the deserialization from the message body. Once the data type is serialized, you do not need to consider the basic message exchange infrastructure during design operations.
· Other serialization mechanisms can be used. StandardIserializable, serializableattributeAndIxmlserializableCan be used to process data types to basicSoapMessage andIxmlserializableCan be used to process data types to basicSoapMessage serialization. These messages can bring data types from one application to another.
Demo2
Create data agreements
OutAndRefParametersOutAndRefParameters
· In most cases, you can useInParameters (Visual BasicIsByval),OutAndRefParameters (Visual BasicIsByref). BecauseOutAndRefThe parameters indicate that the data is returned from the operation. If the operation signature is similar to the following, the request/Reply to the operation, even if the operation signature returnsVoidThe same is true.
[Servicecontract]
Public interface imycontract
{
[Operationcontract]
Void populatedata (ref customdatatypedata );
}
· UseOutOrRefParameters require that the operation has a basic response message before the modified object can be returned. If the operation is a one-way operationInvalidoperationexceptionException