Processing affairs in .NET (2)

zhaozj2021-02-17  51

Automatic transactions and XML Web Services [C #]

ASP.NET provides built-in support for creation and public XML Web Services with a web form and similar programming abstract models. The result model is scalable and scalable, and accepts HTTP, XML, SOAP, WSDL open INTERNET, and other standards. XML Web Services supports an open standard, so any client or device supports Internet access and uses XML Web Services.

XML Web Services provides options for running code within the automatic transaction. Transaction ensures that all interactive maintenance of the resource manager (such as SQL Server, MSMQ Server, Oracle Server, and SNA Server), the ACID attribute required to run reliable distributed applications.

The TransactionOption property using the WebMethod feature declares an automated transaction. If you set the TransactionOption property to TransactionOption.RequiresNew, you will start a new transaction every time the XML Web Services client calls the XML Web Services method.

The following codes displays a service that opens an XML Web Services method (named deleteAuthor). This XML Web Services method performs database operations within an automated transaction range.

[Visual Basic]

<% @ Webservice Language = "VB" class = "Orders"%>

<% @ askEMBLY Name = "System.EnterpriseServices"%>

Imports system

Imports system.data

Imports system.data.sqlclient

Imports System.Web.Services

Imports system.web.util

Imports System.EnterpriseServices

Public class Orders

Inherits WebService

_

Public Function DeleteauThor (Lastname As String) AS INTEGER

Dim deletecmd as [string] = "delete from authors2 where au_lname = '"

& Lastname & "'"

DIM SQLCONN AS New SqlConnection ("User

ID = SA; Database = Pubs; Server = myserver ")

DIM MyCommand as new SqlCommand (deletecmd, sqlconn)

'IF a XML Web Service Method Is Participating In a Transaction and

'An Exception Occurs, ASP.NET Automatically Aborts The Transaction.

'Likewise, if no exception occurs, The Transaction IS

'Automatically Committed.

mycommand.connection.open ()

Return mycommand.executenonquery () end function

END CLASS

[C #]

<% @ Webservice Language = "C #" class = "Orders"%>

<% @ askEMBLY Name = "System.EnterpriseServices"%>

Using system;

Using system.data;

Using system.data.sqlclient;

Using system.Web.services;

Using system.web.util;

Using system.enterprises;

Public Class Orders: WebService

{

[WebMethod (TransactionOption = TransactionOption.Requiresnew)]

Public int deleteauThor (String lastname)

{

String deletecmd = "delete from authors2

WHERE AU_LNAME = '" lastname "' ";

SQLConnection Sqlconn = New SqlConnection ("User

ID = SA; Database = Pubs; Server = myserver ");

Sqlcommand mycommand = new sqlcommand (deletecmd, sqlconn);

// if a XML Web Service Method Is Participating In a Transaction and an

// Exception Occurs, ASP.NET Automatically Aborts the Transaction.

// likewise, if no exception occurs, the transfer is

// Automatically Committed.

mycommand.connection.open ();

Return mycommand.executenonquery ();

}

}

Note that the transaction will begin only if the activated XML Web Services method (from the client call) is transaction metadata. If the activated XML Web Services method does not carry the appropriate transaction metadata, the enthinated XML Web Services method may neither participate in existing transactions and do not start new transactions.

Automatic transaction and .NET framework [C #]

As long as the .NET framework class is prepared to participate in automatic transactions, such an instance can participate in automated transactions. Each of the resources accesses accessed is registered in transactions. For example, if an object uses ADO.NET to send money on a account in a account in a database, the resource manager of this database will determine if the object is executed in the transaction. If the object is executed in the transaction, the Explorer is automatically registered in the transaction.

Use the following procedures to prepare classes to participate in automated transactions:

Apply TransactionAttribute to this class.

Divide this class from the ServicedComponent class.

Use a strong name to sign the program collection.

To use the feature to sign the program set, create a key pair using the Sn.exe utility.

Sn -k testapp.snk

Add AssemblyKeyFileAttribute or AssemblyKeynameAttribute assembly features (they specify the name of the file containing the key pair) to use the strong name as an assembly signature. [Visual Basic]

[C #]

[Assembly: AssemblyKeyFileAttribute ("testapp.snk")]]

Registration to the COM directory contains the assembly of this class.

If the class client calls an instance is managed by the public language running library, the registration will be executed automatically. However, if the expected unmanaged caller may create and call an instance of the class, use the .NET Service Installation Tool (Regsvcs.exe) to manually perform registration.

The following example shows how to apply TransactionAttribute to classes derived from the ServicesDComponent class.

[Visual Basic]

Public Class Bar

Inherits ServicedComponent

'.

END CLASS

[C #]

[Transaction (TransactionOption.Required)]

Public class bar (): servicedcomponent

{

//.

}

When using a transaction feature, Transaction, Transaction, TransactionAttribute, and TransactionAttribute can be alternate. For example, you can use Transaction or TransactionAttribute to produce the same result.

The following table lists and describes each constructor variant.

Characteristic value

Description

Disabled

Eliminate automatic transaction control of objects. Objects to apply this feature value can be used directly to use the Distributed Transaction Coordinator (DTC) for transactional support.

[Transaction (TransactionOption.Disabled)]

NotSupported

Indicates that objects are not running within the transaction. After processing the request, the object context is created regardless of whether there is an activity transaction.

Transaction (TransactionOption.notSupported]

Supported

Indicates that if there is a transaction, the object runs in the context of existing transactions. If there is no transaction, the object is run without a transaction.

Transaction (TransactionOption.supported)]

Required

(Defaults)

Indicates that the object requires a transaction. If there is a transaction, the object runs in an existing transaction range. If there is no transaction, the object starts a transaction.

[Transaction (TransactionOption.Required)]

Requiresnew

Indicates that the object requires a transaction and launches a new transaction for each request.

Transaction (TransactionOption.Requiresnew)]

Example

The following code example illustrates several elements of automatic transactions. In this case, transactional classes and clients call such a client are managed by running library.

[Visual Basic]

'------------------------------------- ----------------

'TestApp.vb

'Generate A Strong Name:

'Sn -k Testapp.snk

'Compile the code:

'VBC / Target: EXE /R :System.EnterpriseServices.dll TestApp.vb' Run TestApp:

'Start Testapp.exe

'------------------------------------- ----------------

Option expedition

Option strict

Imports system

Imports system.Runtime.compilerServices

Imports System.EnterpriseServices

Imports system.reflection

'Registration details.

'COM Application Name as it Appears in the COM Catalog.

'Strong name for assembly.

Public Class Account

Inherits ServicedComponent

'Provides setcomplete behavior in the absence of exceptions.

public subnement (Amount AS Integer)

'Do Some Database Work. Any Exception Thrown Here Aborts

'Transaction; Otherwise, Transaction Commits.

End Sub

END CLASS

Public Class Client

Public Shared Sub Main ()

DIM AccountX as new account ()

Accountx.debit (100)

Environment.exit (0)

End Sub

END CLASS

[C #]

/ / -------------------------------------------------------------------------------------------- -----------------

// TestApp.cs

// generate a strong name:

// sn -k testapp.snk

// compile the code:

// CSC / Target: Exe /R :system.EnterpriseServices.dll TestApp.cs

// Run TestApp:

// Start TestApp.exe

/ / -------------------------------------------------------------------------------------------- -----------------

Using system;

Using system.Runtime.compilerServices;

Using system.enterprises;

Using system.reflection;

// registration details.

// COM Application Name as it Appears in the COM Catalog.

[Assembly: ApplicationName ("TestApp")]]]

// Strong name for assembly.

[Assembly: AssemblyKeyFileAttribute ("TestApp.snk")] [Transaction (TransactionOption.Required)]

Public Class Account: ServicedComponent

{

// provides setcomplete behavior in the absence of exceptions.

[AutoComplete]

Public void debit (int Amount)

{

// do some database work. Any Exception thrown here Aborts the

// Transaction; OtherWise, Transaction Commits.

}

}

Public Class Client

{

Public static int main ()

{

Account AccountX = New Account ();

AccountX.debit (100);

Return 0;

}

}

Vote in automatic transaction [c #]

The .NET Framework class and ASP.NET page can be submitted or suspended by voting or suspended their current transactions. By default, if there is no explicit voting in the code, the default is submitted. However, the default submission may extend the time used to release expensive resources for each transaction, making it possible to reduce the performance of the application.

Explicit voting also allows classes or pages to stop business when they encounter serious errors. In addition, the performance of the application can be improved by caught fatal errors, end transactions, release resources, etc. in the early stage of transaction.

Automatic completion

System.EnterpriseServices.autocompletettribute makes the object to complete the transaction when the object is returned to the method. If the method calls triggers an exception, the transaction is aborted. This feature can only be applied from the class from the ServicedComponent class.

To use this feature, insert this feature before the class method. If this feature is added to an interface method, the public language runtime will ignore it. The following codes displays the location of this feature on transaction recognition classes.

[Visual Basic]

Public Class Account

Inherits ServicedComponent

public subnement (Amount AS Integer)

'Do Some Database Work. Any Exception Thrown Here Aborts

'Transaction; Otherwise, Transaction Commits.

End Sub

END CLASS

[C #]

Transaction (TransactionOption.supported)]

Public Class Account: ServicesDComponent {

[AutoComplete]

Public void debit (int amount) {

// do some database work. Any Exception thrown here Aborts the transport;

// OtherWise, Transaction Commits.

}

}

Use Setabort and SetComplete

You can explicitly submit or abort transactions using the System.EnterpriseServices.ContextUtIL class of the public SETCOMPLETE or SETABORT method. SetComplete indicates that the object voted to submit its work; Setabort indicates that the object encounters problems and vote for the aborting business. The transaction is submitted or suspended until the root object of the transaction is deactivated. In addition, there is a suspension voting in the object of the transaction, which will result in failure of the entire transaction. The following codes displays the SETABORT and SETCOMPLETE methods that are being used.

[Visual Basic]

On the THE TO DO SOMETHING CRUCIAL to the Transaction in Progress.

IF not dosomework () THEN

'Something Goes WRONG.

Contextutil.Setabort ()

Else

'All Goes Well.

ContextUtil.setComplete ()

END IF

[C #]

SOMETHING CRUCIAL TO.

IF (! DOSMEWORK ())

{

// Something Goes WRONG.

Contextutil.Setabort ();

}

Else

{

// All Goes Well.

Contextutil.setComplete ();

}

to sum up

The above is some concepts and little examples of processed affairs in vs.net, and give you a reference. If you have any suggestions, please Mil I Paulni@citiz.net.

转载请注明原文地址:https://www.9cbs.com/read-28541.html

New Post(0)