WCF开发工具为我们带来了非常大的好处。它的强大的功能优势令其在开发领域中占据着非常重要的地位。在这里我们将会为大家详细介绍一下有关WCF程序事务的相关定义与实现方法,希望能对大家有所帮助。
创新互联公司于2013年开始,先为南山等服务建站,南山等地企业,进行企业商务咨询服务。为南山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
WCF程序事务定义如下:
- [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);
- }
WCF程序事务实现如下:
- [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();
- }
服务端配置如下:
- < system.serviceModel>
- < !--提供的服务-->
- < services>
- < service name="Wf.Complex.Service.ComplexServiceImplement"
behaviorConfiguration="myServiceBehavior">- < endpoint address="" binding="wsHttpBinding" bindingConfiguration=
"transactionalWsatHttpBinding" contract=
"Wf.Complex.Service.IComplexService"/>- < endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange"/>- < /service>
- < /services>
- < behaviors>
- < serviceBehaviors>
- < behavior name="myServiceBehavior">
- < serviceMetadata httpGetEnabled="True"/>
- < serviceDebug includeExceptionDetailInFaults="False"/>
- < /behavior>
- < /serviceBehaviors>
- < /behaviors>
- < bindings>
- < wsHttpBinding>
- < binding name="transactionalWsatHttpBinding" transactionFlow="true"/>
- < /wsHttpBinding>
- < /bindings>
- < /system.serviceModel>
客户端代码如下:
- 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();
- }
运行结果:#t#
服务端的ExecuteNoneQuery方法中using (OracleConnection oraConnection = new OracleConnection(oraConnectionString))代码块发生异常,异常内容如下:
"无法加载 DLL“oramts.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。"
结论:
对于数据库事务,可以通过WCF程序事务变形的方式来实现,但必须获取对应数据库产品的支持,sqlserver产品默认支持,oracle数据库产品需要考虑本风险,及时规避.
网站名称:WCF程序事务相关定义与实现方法详解
文章路径:http://www.36103.cn/qtweb/news43/2693.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联