zoukankan      html  css  js  c++  java
  • wcf中事务的操作

    using System;
    using System.ServiceModel;
    
    namespace Larryle.Wcf.ServiceContract.Transaction
    {
        [ServiceContract]
        public interface IHello
        {
            [OperationContract]
            [TransactionFlow(TransactionFlowOption.Mandatory)]
            void WriteHello(string name);
        }
    }
    using System;
    using System.ServiceModel;
    
    namespace Larryle.Wcf.ServiceContract.Transaction
    {
        [ServiceContract]
        public interface IHi
        {
            [OperationContract]
            [TransactionFlow(TransactionFlowOption.Mandatory)]
            void WriteHi(string Name);
        }
    }


    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using Larryle.Wcf.ServiceContectData.Transaction;
    
    namespace Larryle.Wcf.ServiceContract.Transaction
    {
        [ServiceContract]
        public interface IResult
        {
            [OperationContract]
            List<Item> GetResult();
        }
    }
    
    
    
    
    
    using System;
    using System.Linq;
    using System.ServiceModel;
    using Larryle.Wcf.ServiceContract.Transaction;
    using Larryle.Wcf.ServiceContectData.Transaction;
    
    namespace Larryle.Wcf.Service.Transaction
    {
        public class Hello : IHello
        {
            [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
            public void WriteHello(string name)
            {
                DBDataContext ctx = new DBDataContext();
                ctx.Item.InsertOnSubmit(new Item
                {
                    Title = string.Format("Hello:{0},TransactionId:{1}", name, System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier),
                    CreatedTime = DateTime.Now
                });
                ctx.SubmitChanges();
            }
        }
    }
    
    
    using System;
    using System.ServiceModel;
    using Larryle.Wcf.ServiceContectData.Transaction;
    using Larryle.Wcf.ServiceContract.Transaction;
    
    
    namespace Larryle.Wcf.Service.Transaction
    {
        public class Hi : IHi
        {
            [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
            public void WriteHi(string Name)
            {
                if (DateTime.Now.Second % 2 == 0)
                {
                    throw new System.Exception("为测试事务而抛出的异常");
                }
                DBDataContext dbcx = new DBDataContext();
                dbcx.Item.InsertOnSubmit(new Item
                {
                    Title = string.Format("Hi:{0},TransactionId:{1}",Name,System.Transactions.Transaction.Current.TransactionInformation.LocalIdentifier),
                    CreatedTime=DateTime.Now
                });
                dbcx.SubmitChanges();
            }
        }
    }
    using System;
    using System.Linq;
    using System.ServiceModel;
    using Larryle.Wcf.ServiceContectData.Transaction;
    using Larryle.Wcf.ServiceContract.Transaction;
    
    namespace Larryle.Wcf.Service.Transaction
    {
        public class Result : IResult
        {
            public System.Collections.Generic.List<Item> GetResult()
            {
                DBDataContext dbcx = new DBDataContext();
                var result = from l in dbcx.Item orderby l.CreatedTime descending select l;
                return result.ToList();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Larryle.WcfWebClient
    {
        public partial class TestTransaction : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void text_Click(object sender, EventArgs e)
            {
                TransactionHelloSvc.HelloClient hell = new TransactionHelloSvc.HelloClient();
                TransactionHiSvc.HiClient hi = new TransactionHiSvc.HiClient();
                TransactionResultSvc.ResultClient result = new TransactionResultSvc.ResultClient();
                System.Transactions.TransactionOptions tr = new System.Transactions.TransactionOptions();
                tr.Timeout = new TimeSpan(0, 0, 30);
                tr.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
                {
                    try
                    {
                        hell.WriteHello("larryle1");
                        hi.WriteHi("larryle2");
                        hell.Close();
                        hi.Close();
                        ts.Complete();
                        ClientScript.RegisterStartupScript(this.GetType(), "js", "alert('ok')", true);
                    }
                    catch (Exception ex)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "js", "alert('失败')", true);
                    }
                }
                this.GridView1.DataSource = result.GetResult();
                this.GridView1.DataBind();
                result.Close();
            }
        }
    }
    <?xml version="1.0"?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="StreamedBindingConfiguration" transferMode="Streamed" maxReceivedMessageSize="1073741824" receiveTimeout="00:10:00"></binding>
          </netTcpBinding>
          <netMsmqBinding>
            <binding name="MSMQBindingConfiguration">
              <security>
                <transport msmqAuthenticationMode="None" msmqProtectionLevel="None"/>
                <message clientCredentialType="UserName"/>
              </security>
            </binding>
          </netMsmqBinding>
          <wsHttpBinding>
            <binding name="SecurityBindingConfiguration">
              <security>
                <message clientCredentialType="UserName"/>
              </security>
            </binding>
            <binding name="TransactionConfiguration" transactionFlow="true">
            </binding>
          </wsHttpBinding>
          <ws2007HttpBinding>
            <binding name="SessionManagementBinding" receiveTimeout="00:10:00">
              <reliableSession enabled="true"/>
              <security>
                <message establishSecurityContext="true"/>
              </security>
            </binding>
          </ws2007HttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="BindingBehavior">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
            <behavior name="MessageBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
            <behavior name="InstanceModeBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
            <behavior name="ExceptionBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
            <behavior name="SecurityBehavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
              <serviceCredentials>
                <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="Larryle.Wcf.Service.Security.CustomNamePasswordValidator,Larryle.Wcf.Service"/>
                <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
                <clientCertificate>
                  <authentication certificateValidationMode="None"/>
                </clientCertificate>
              </serviceCredentials>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service name="Larryle.Wcf.Service.Binding.Hello" behaviorConfiguration="BindingBehavior">
            <endpoint address="net.tcp://localhost:54321/Binding/Hello" binding="netTcpBinding" contract="Larryle.Wcf.ServiceContract.Binding.IHello"></endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/Binding/"/>
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.ConcurrencyLock.Hello" behaviorConfiguration="MessageBehavior">
            <endpoint address="Hello" binding="wsHttpBinding" contract="Larryle.Wcf.ServiceContract.ConcurrencyLock.IHello"></endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/ConcurrencyLock/"/>
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.Contract.PersonManager" behaviorConfiguration="MessageBehavior">
            <endpoint address="PersonManager" binding="mexHttpBinding" contract="ConfigurationNameTest">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/Contract/"/>
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.Exception.Hello" behaviorConfiguration="ExceptionBehavior">
            <endpoint address="Hello" binding="wsHttpBinding" contract="Larryle.Wcf.ServiceContract.Exception.IHello">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/Exception/"/>
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.Security.Hello" behaviorConfiguration="SecurityBehavior">
            <endpoint address="Hello" binding="wsHttpBinding" contract="Larryle.Wcf.ServiceContract.Security.IHello" bindingConfiguration="SecurityBindingConfiguration">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/Security/"/>
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.SessionManagement.Hello" behaviorConfiguration="MessageBehavior">
            <endpoint address="Hello" binding="ws2007HttpBinding" bindingConfiguration="SessionManagementBinding" contract="Larryle.Wcf.ServiceContract.SessionManagement.IHello">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/SessionManagement/" />
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.Transaction.Hello" behaviorConfiguration="MessageBehavior">
            <endpoint address="Hello" binding="wsHttpBinding" contract="Larryle.Wcf.ServiceContract.Transaction.IHello" bindingConfiguration="TransactionConfiguration">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/Transaction/Hello"/>
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.Transaction.Hi" behaviorConfiguration="MessageBehavior">
            <endpoint address="Hi" binding="wsHttpBinding" contract="Larryle.Wcf.ServiceContract.Transaction.IHi" bindingConfiguration="TransactionConfiguration">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/Transaction/Hi"/>
              </baseAddresses>
            </host>
          </service>
          <service name="Larryle.Wcf.Service.Transaction.Result" behaviorConfiguration="MessageBehavior">
            <endpoint address="Result" binding="wsHttpBinding" contract="Larryle.Wcf.ServiceContract.Transaction.IResult" bindingConfiguration="TransactionConfiguration">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:12345/Transaction/Result"/>
              </baseAddresses>
            </host>
          </service>
        </services>
      </system.serviceModel>
      <startup>
        <supportedRuntime version="v2.0.50727"/>
      </startup>
    </configuration>
     
  • 相关阅读:
    ACM成长之路
    洛谷P1047 校门外的树
    洛谷P1046 陶陶摘苹果
    2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 F题
    图论:POJ2186-Popular Cows (求强连通分量)
    DFS:POJ1562-Oil Deposits(求连通块个数)
    DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
    map函数的应用:UVa156-Ananagrams
    set的应用:UVa10815-Andy's First Dictionary
    水题:UVa253-Cube painting
  • 原文地址:https://www.cnblogs.com/systemnet123/p/3193703.html
Copyright © 2011-2022 走看看