休息服务呼叫的显式交易

时间:2015-09-07 04:50:57

标签: c# wcf rest transactions

目前正在使用WCF Restful Service。我需要在服务调用中维护事务,如果发生错误则意味着必须回滚。所以我尝试使用system.transactions进行交易技术。

我可以通过以下c#代码维护交易:

    CommittableTransaction tx = new CommittableTransaction();
    SqlConnection myConnection = new SqlConnection(@"Data Source=199.999.9.9;Initial Catalog=AWCFTest;Persist Security Info=True;User ID=sa;Password=sa");
    SqlCommand myCommand = new SqlCommand();
    myConnection.Open();
    myConnection.EnlistTransaction(tx);
    try
    {
        myCommand.Connection = myConnection;
        myCommand.CommandText = "insert into Customer(CustomerName,CustomerCode) values('1','sss')";
        myCommand.ExecuteNonQuery();

        // Insert the first record.
        myCommand.CommandText = "insert into Customer(CustomerName,CustomerCode) values('3','bbb')";
        myCommand.ExecuteNonQuery();

        tx.Commit();
        catch (Exception ex)
        {
            tx.Rollback();
        }
        myConnection.Close();
        tx = null;
    }

但无法通过以下代码维护交易:

    CommittableTransaction tx = new CommittableTransaction();

    SqlConnection myConnection = new SqlConnection(@"Data Source=199.999.9.9;Initial Catalog=AWCFTest;Persist Security Info=True;User ID=sa;Password=sa");
    SqlCommand myCommand = new SqlCommand();
    myConnection.Open();
    myConnection.EnlistTransaction(tx);

    try
    {
        myCommand.Connection = myConnection;
        string requestData = "{\"logInsert\":\"first\"}";
        byte[] data = Encoding.UTF8.GetBytes(requestData);
        HttpWebRequest request;
        request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/RestfulServices/Common.svc/convert1");
        request.Method = "PUT";
        request.ContentType = "application/json";
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(data, 0, data.Length);
        dataStream.Close();
        HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
        String ss = new StreamReader(resp.GetResponseStream()).ReadToEnd();


        string requestData1 = "{\"logInsert\":\"second\"}";
        byte[] data1 = Encoding.UTF8.GetBytes(requestData1);
        HttpWebRequest request1;
        request1 = (HttpWebRequest)HttpWebRequest.Create("http://localhost/RestfulServices/Common.svc/convert2");
        request1.Method = "PUT";
        request1.ContentType = "application/json";
        Stream dataStream1 = request1.GetRequestStream();
        dataStream1.Write(data1, 0, data1.Length);
        dataStream1.Close();
        HttpWebResponse resp1 = (HttpWebResponse)request1.GetResponse();
        String ss1 = new StreamReader(resp1.GetResponseStream()).ReadToEnd();
        tx.Commit();
    }
    catch (Exception ex)
    {
        tx.Rollback();
    }
    myConnection.Close();
    tx = null;
}

是否有任何可能的方法来维护Rest Service呼叫的交易?并回滚?

1 个答案:

答案 0 :(得分:0)

试试这样:

try
{
    using (var conn = new SqlConnection(/* connection string or whatever */))
    {
        conn.Open();

        using (var trans = conn.BeginTransaction())
        {
            try
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.Transaction = trans;
                    /* setup command type, text */
                    /* execute command */
                }

                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                /* log exception and the fact that rollback succeeded */
            }
        }
    }
}
catch (Exception ex)
{
    /* log or whatever */
}