Definitions of WCF transactions

Source: Internet
Author: User

The WCF development tool brings us great benefits. Its powerful functional advantages make it a very important position in the development field. Here we will introduce in detail the definitions and implementation methods of WCF program transactions, hoping to help you.

A WCF program transaction is defined as follows:

 

[ServiceContract(SessionModeSessionMode = SessionMode.Required)]  
public interface IComplexService  
{  
[OperationContract]  
[TransactionFlow(TransactionFlowOption.Mandatory)]  
void ExecuteNoneQuery1(string sql);  
[OperationContract]  
[TransactionFlow(TransactionFlowOption.Mandatory)]  
void ExecuteNoneQuery2(string sql);  
[OperationContract]  
[TransactionFlow(TransactionFlowOption.Mandatory)]  
void ExecuteNoneQuery3(string sql);  

 

The implementation of WCF program transactions is as follows:

 

[ServiceBehavior (TransactionTimeout = "00:00:45 ",
TransactionIsolationLevel = System. Transactions. IsolationLevel.
Serializable)]

Public class ComplexServiceImplement: IComplexService

{

Private static void ExecuteNoneQuery (string SQL)

{

String sqlConnectionString = "Password = sa; User
ID = sa; Initial Catalog = ipedf; Data Source = .";

String oraConnectionString = "User ID = sample;
Password = sample; Data Source = insapp ";

Using (OracleConnection oraConnection = new OracleConnection
(OraConnectionString ))

{

Try

{

OraConnection. Open ();

OracleCommand cmd = oraConnection. CreateCommand ();

Cmd. CommandText = SQL;

Cmd. CommandType = CommandType. Text;

Cmd. ExecuteNonQuery ();

}

Catch (Exception e)

{

String Debug = e. Message;

}

Finally

{

OraConnection. Close ();

}

}

Using (SqlConnection sqlConnection =
New SqlConnection (sqlConnectionString ))

{

Try

{

SqlConnection. Open ();

SqlCommand cmd = sqlConnection. CreateCommand ();

Cmd. CommandText = SQL;

Cmd. CommandType = CommandType. Text;

Cmd. ExecuteNonQuery ();

}

Catch (Exception e)

{

String Debug = e. Message;

}

Finally

{

SqlConnection. Close ();

}

}

}

[OperationBehavior (

TransactionScopeRequired = true, TransactionAutoComplete = true)]

Public void ExecuteNoneQuery1 (string SQL)

{

ExecuteNoneQuery (SQL );

// OperationContext. Current. SetTransactionComplete ();

}

[OperationBehavior (

TransactionScopeRequired = true, TransactionAutoComplete = true)]

Public void ExecuteNoneQuery2 (string SQL)

{

ExecuteNoneQuery (SQL );

// OperationContext. Current. SetTransactionComplete ();

}

[OperationBehavior (

TransactionScopeRequired = true, TransactionAutoComplete = true)]

Public void ExecuteNoneQuery3 (string SQL)

{

ExecuteNoneQuery (SQL );

// OperationContext. Current. SetTransactionComplete ();

}

 

The server configuration is as follows:

 

< System. serviceModel> 

<! -- Provided services --> 

< Services> 

< ServiceName = "Wf. Complex. Service. ComplexServiceImplement"
BehaviorConfiguration = "myServiceBehavior"> 

< EndpointAddress = "" binding = "wsHttpBinding" bindingConfiguration =
"TransactionalWsatHttpBinding" contract =
"Wf. Complex. Service. IComplexService"/> 

< EndpointAddress = "mex" binding = "mexHttpBinding"
Contract = "IMetadataExchange"/> 

</Service> 

</Services> 

< Behaviors> 

< Servicebehaviors> 

< BehaviorName = "myservicebehavior"> 

< ServicemetadataHttpgetenabled = "true"/> 

< ServicedebugIncludeexceptiondetailinfaults = "false"/> 

</Behavior> 

</Servicebehaviors> 

</Behaviors> 

< Bindings> 

< Wshttpbinding> 

< BindingName = "transactionalwsathttpbinding" transactionflow = "true"/> 

</Wshttpbinding> 

</Bindings> 

</System. serviceModel> 

 

The client code is as follows:

 

IComplexService complex = new ComplexServiceClient();  
try  
{  
using (TransactionScope scope = new TransactionScope
(TransactionScopeOption.Required))  
{  
string sql = "delete from student";  
complex.ExecuteNoneQuery1(sql);  
sql = "insert into student(id,name) values(1,'yeyang_1')";  
complex.ExecuteNoneQuery2(sql);  
sql = "insert into student(id,name) values(2,'yeyang_2')";  
complex.ExecuteNoneQuery3(sql);  
scope.Complete();  
}  
Console.WriteLine("succeed");  
}  
catch (Exception expt)  
{  
Console.WriteLine(expt.Message);  
}  
finally  
{  
(complex as ComplexServiceClient).Close();  

 

Running result:

 

The using (OracleConnection oraConnection = new OracleConnection (oraConnectionString) code block in the ExecuteNoneQuery method of the server is abnormal. The exception is as follows:

"Unable to load DLL" oramts. dll ": the specified module cannot be found. (The exception is from HRESULT: 0x8007007E ). "

Conclusion:

For database transactions, you can use the WCF program transaction deformation method, but you must obtain the corresponding database product support, which is supported by sqlserver by default. oracle database products need to consider this risk and avoid it in time.

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.