WCF开发工具功能特点比较突出,其优势突出的功能为它在开发领域中占据着一个比较重要的地位。在这里我们将会通过对WCF事务演示的解读,来充分的了解一下这一开发平台的应用方式。
下面的这段代码就是WCF事务演示的经典示例:
// -------- Service1 -----------------
[ServiceContract]
public interface IService1
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void Test();
}
public class MyService1 : IService1
{
[OperationBehavior(TransactionScopeRequired=true)]
public void Test()
{
string connStr = "server=(local);uid=sa;pwd=sa;database=temp";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into [User]
([name]) values (@name)",
conn);
cmd.Parameters.Add(new SqlParameter("@name", "ZhangSan"));
cmd.ExecuteNonQuery();
}
}
}
// -------- Service2 -----------------
[ServiceContract]
public interface IService2
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void Test();
}
public class MyService2 : IService2
{
[OperationBehavior(TransactionScopeRequired = true)]
public void Test()
{
string connStr = "server=(local);uid=sa;pwd=sa;database=temp";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into Account
([user], [money]) values (@user, @money)",
conn);
cmd.Parameters.Add(new SqlParameter("@user", "ZhangSan"));
cmd.Parameters.Add(new SqlParameter("@money", 100));
cmd.ExecuteNonQuery();
}
}
}
public class WcfTest
{
public static void Test()
{
// -------- Host -----------------
AppDomain.CreateDomain("Server").DoCallBack(delegate
{
NetTcpBinding bindingServer = new NetTcpBinding();
bindingServer.TransactionFlow = true;
ServiceHost host1 = new ServiceHost(typeof(MyService1),
new Uri("net.tcp://localhost:8080"));
host1.AddServiceEndpoint(typeof(IService1), bindingServer, "");
host1.Open();
ServiceHost host2 = new ServiceHost(typeof(MyService2),
new Uri("net.tcp://localhost:8081"));
host2.AddServiceEndpoint(typeof(IService2), bindingServer, "");
host2.Open();
});
// -------- Client -----------------
NetTcpBinding bindingClient = new NetTcpBinding();
bindingClient.TransactionFlow = true;
IService1 client1 = ChannelFactory<IService1>.CreateChannel
(bindingClient,
new EndpointAddress("net.tcp://localhost:8080"));
IService2 client2 = ChannelFactory<IService2>.CreateChannel
(bindingClient,
new EndpointAddress("net.tcp://localhost:8081"));
using (TransactionScope scope = new TransactionScope())
{
try
{
client1.Test();
client2.Test();
scope.Complete();
}
finally
{
(client1 as IDisposable).Dispose();
(client2 as IDisposable).Dispose();
}
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
以上就是我们为大家带来的WCF事务演示。
【编辑推荐】