1

我用过这个 guid:http ://www.c-sharpcorner.com/uploadfile/shivprasadk/wcf-faq-part-5-transactions/ 为什么它不回滚?我不明白!

我有一个服务和客户端应用程序,但我不知道这段代码有什么问题。在完美地完成这一行并将其保存在我的数据库中之后,

proxy.AddEmployee("Stav", "20");

下一行抛出异常,因为我没有向 Age 参数发送数字,但事务没有回滚第一行,所以信息:Stav,20 仍然存在于我的数据库中!

proxy.AddEmployee("Stav123", "Not a Number(-->will do Exception)") 

编辑 1: 我添加了 AddEmployee 工具。

客户:

static void Main(string[] args)
        {
            ServiceReference1.IJob proxy = new ServiceReference1.JobClient();
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    proxy.AddEmployee("Stav", "20");
                    proxy.AddEmployee("Stav123", "Not a Number(-->will do Exception) ");//stop the running and show the Exception but keep the stav,20 in DB
                    ts.Complete();
                }
                catch
                {
                    ts.Dispose();
                }
            }
        }

服务:

[ServiceContract]
    public interface IJob
    {
        [OperationContract]
        [TransactionFlow(TransactionFlowOption.Allowed)]
        void AddEmployee(string Name, string Age);
    }
public class JobImplement:IJob
    {
        [OperationBehavior(TransactionScopeRequired = true)]
        public void AddEmployee(string Name, string Age)
        {
string strConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\stavalfi\Desktop\WCF2\ConsoleApplication4\WCF_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        SqlConnection objConnection = new SqlConnection(strConnection);
        objConnection.Open();
        SqlCommand objCommand = new SqlCommand("INSERT INTO Employee (Name,Age) " + "VALUES ('" + Name + "' ,'" + Age + "')", objConnection);
        objCommand.ExecuteNonQuery();
        objConnection.Close();
        }
    }
static void Main(string[] args)
        {
            WSHttpBinding Basic = new WSHttpBinding();
            Basic.TransactionFlow = true;
            ServiceHost host = new ServiceHost(typeof(JobImplement), new Uri("http://localhost:8080"));
            //
            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            behavior.HttpGetEnabled = true;
            host.Description.Behaviors.Add(behavior);
            //
            host.AddServiceEndpoint(typeof(IJob), new BasicHttpBinding(), "Request123");
            host.Open();
            //
            Console.WriteLine("opened");
            Console.ReadLine();
            //
            host.Close();
        }
4

2 回答 2

3

看起来您的代码中可能有错字:

static void Main(string[] args)         
{             
    ...
    host.AddServiceEndpoint(typeof(IJob), new BasicHttpBinding(), "Request123");   
    ...
}

您正在添加 BasicHttpBinding 类型的端点 - 一种不支持事务的绑定协议。

我认为您实际上是要这样做:

static void Main(string[] args)         
{             
    WSHttpBinding Basic = new WSHttpBinding();
    Basic.TransactionFlow = true;  
    ...
    host.AddServiceEndpoint(typeof(IJob), Basic, "Request123");   
    ...
}

这会给你一个 WSHttpBinding 端点——一个支持事务的绑定协议。

于 2012-04-24T20:38:34.703 回答
-1

您应该实现自己的回滚功能。这是一个基本的方法。在您的服务类接口中添加[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]属性。然后添加这些代码:

private SqlCommand Command { get; set; }

[OperationContract]
public void BeginTransaction()
{
    this.Command = new SqlCommand();
    string strConnection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\stavalfi\Desktop\WCF2\ConsoleApplication4\WCF_DB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
    SqlConnection objConnection = new SqlConnection(strConnection);
    objConnection.Open();
    this.Command.Connection = objConnection;
}

[OperationContract]
public void RollBackTransaction()
{
    this.Command.Transaction.Rollback();
}

[OperationContract]
public void CommitTransaction()
{
    this.Command.Transaction.Commit();
}

[OperationContract]
public void CloseConnection()
{
    this.Command.Connection.Close();
    this.Command = null;
}

[OperationBehavior(TransactionScopeRequired = true)]
public void AddEmployee(string Name, string Age)
{
    this.Command.CommandText = "INSERT INTO Employee (Name,Age) " + "VALUES ('" + Name + "' ,'" + Age + "')";
    this.Command.ExecuteNonQuery();
}

然后您可以像这样在客户端代码中访问它:

try
{
    proxy.BeginTransaction();
    proxy.AddEmployee("Stav", "20");
    proxy.AddEmployee("Stav123", "Not a Number(-->will do Exception)");
    proxy.CommitTransaction();
}
catch
{
    proxy.RollBackTransaction();
}
finally
{
    proxy.CloseConnection();
}

希望这可以帮助。

于 2012-04-23T09:29:58.553 回答