WCF Example Study Notes 002 default service behavior (01.02.01.01)

Source: Internet
Author: User

The default service behavior and operation behavior are used in this example.
This example is listed belowProgramMainCode:
Its contracts and services are:

Code
// Define a service contract.
[Servicecontract (namespace =   " Http://Microsoft.ServiceModel.Samples " , Sessionmode = Sessionmode. Required)]
Public   Interface Icalculator
{
[Operationcontract]
Double Add ( Double N1, Double N2 );
[Operationcontract]
Double Subtract ( Double N1, Double N2 );
[Operationcontract]
Double Multiply ( Double N1, Double N2 );
[Operationcontract]
Double Divide ( Double N1, Double N2 );
}

[Servicebehavior (
Automaticsessionshutdown = True ,
Concurrencymode = Concurrencymode. Single,
Instancecontextmode = Instancecontextmode. persession,
Includeexceptiondetailinfaults = False ,
Usesynchronizationcontext = True ,
Validatemustunderstand = True )]
Public   Class Calculatorservice: icalculator
{
# Region Icalculator Member

[Operationbehavior (
Transactionautocomplete = True ,
Transactionscoperequired = False ,
Impersonation = Impersonationoption. notallowed)]
Double Icalculator. Add ( Double N1, Double N2)
{
System. Threading. thread. Sleep ( 1600 );
Return N1 + N2;
}

[Operationbehavior (
Transactionautocomplete =   True ,
Transactionscoperequired =   False ,
Impersonation = Impersonationoption. notallowed)]
Double Icalculator. Subtract ( Double N1, Double N2)
{
System. Threading. thread. Sleep ( 800 );
Return N1 - N2;
}

[Operationbehavior (
Transactionautocomplete =   True ,
Transactionscoperequired =   False ,
Impersonation = Impersonationoption. notallowed)]
Double Icalculator. Multiply ( Double N1, Double N2)
{
System. Threading. thread. Sleep ( 400 );
Return N1 * N2;
}

[Operationbehavior (
Transactionautocomplete =   True ,
Transactionscoperequired =   False ,
Impersonation = Impersonationoption. notallowed)]
Double Icalculator. Divide ( Double N1, Double N2)
{
System. Threading. thread. Sleep ( 100 );
Return N1 / N2;
}

# Endregion
}

 

The configuration file on the server is the same as the preceding example.

The code for the client to call the operation is:

Code
// Create a client.
Calculatorclient Client =   New Calculatorclient ();

// Call the add service operation.
Double Value1 =   100.00d ;
Double Value2 =   15.99d ;
Double Result = Client. Add (value1, value2 );
Console. writeline ( " Add ({0}, {1}) = {2} " , Value1, value2, result );

// Call the subtract service operation.
Value1 =   145.00d ;
Value2 =   76.54d ;
Result = Client. Subtract (value1, value2 );
Console. writeline ( " Subtract ({0 },{ 1}) = {2} " , Value1, value2, result );

// Call the multiply service operation.
Value1 =   9.00d ;
Value2 =   81.25d ;
Result = Client. Multiply (value1, value2 );
Console. writeline ( " Multiply ({0}, {1}) = {2} " , Value1, value2, result );

// Call the divide service operation.
Value1 =   22.00d ;
Value2 =   7.00d ;
Result = Client. Divide (value1, value2 );
Console. writeline ( " Divide ({0 },{ 1}) = {2} " , Value1, value2, result );

//Closing the client releases all communication resources.
Client. Close ();

Console. Read ();

 

The result generated by the program is the same as the preceding example, that is

This program gives us a preliminary understanding of the default situation of Service Behavior and operation behavior.
The following lists the attributes of servicebehaviorattribute and operationbehaviorattribute respectively:

Attribute list of servicebehaviorattribute:

Attribute name Description Default Value
Addressfiltermode Get or set the scheduler to route incoming messages to the addressfiltermode at the correct endpoint. Addressfiltermode.Exact
Automaticsessionshutdown Whether to automatically close the output session when the client closes the output session. True
Concurrencymode Gets or sets a value that indicates whether the Service supports single-threaded, multi-threaded, or reentrant calls. Concurrencymode.Single
Configurationname Gets or sets the value used to locate the Service Element in the application configuration file. This fully qualified type name does not contain assembly information
Ignoreextensiondataobject Gets or sets a value that specifies whether to send unknown serialized data to the network. False
Includeexceptiondetailinfaults Gets or sets a value that specifies whether to convert normal unhandled execution exceptions to system. servicemodel. predictiondetail type system. servicemodel. faultexception <tdetail> and send it as an error message. Set it to true only in the development phase to troubleshoot the service. False
Instancecontextmode Gets or sets the value indicating when a new service object is created. Instancecontextmode.Persession
Maxitemsinobjectgraph Gets or sets the maximum number of items allowed in a serialized object. 65536 bytes (64 KB)
Name Obtain or set the name attribute value of the service element in the Web Service Description Language (WSDL.  
Namespace Obtain or set the target namespace value of the service in the Web Service Description Language (WSDL.  
Releaseserviceinstanceontransactioncomplete Gets or sets a value that specifies whether to release the service object after the current transaction is completed. True
Transactionautocompleteonsessionclose Gets or sets a value that specifies whether the transaction is suspended when the current session is closed normally. False
Transactionisolationlevel Specifies the transaction isolation level between newly created transactions in the Service and the stream transactions passed in by the client. Isolationlevel.Unspecified
Transactiontimeout Gets or sets the time period during which transactions must be completed.  
Typeid When implemented in a derived class, obtain the unique identifier of this attribute. (Inherited from attribute .)  
Usesynchronizationcontext Gets or sets a value that specifies whether to use the current synchronization context to select the thread for execution. True
Validatemustunderstand Gets or sets a value that specifies whether the system or application enforces soap mustunderstand header processing. True

Operationbehaviorattribute attribute list:

attribute name description default value
autodisposeparameters obtain or set the automatic release parameter. true
impersonation gets or sets a value that indicates the simulation level supported by the operation. impersonationoption. notallowed
releaseinstancemode gets or sets a value that indicates the time when the service object is recycled during the call operation. releaseinstancemode. None
transactionautocomplete gets or sets a value that indicates whether to automatically complete the current transaction range if no unhandled exception occurs. true
transactionscoperequired gets or sets a value that indicates whether the transaction range is required for the method during execution. false
typeid when implemented in a derived class, obtain the unique identifier of this attribute. (Inherited from attribute .)

 

Download sample program

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.