Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wcf 事务仅在客户端启动_Wcf - Fatal编程技术网

Wcf 事务仅在客户端启动

Wcf 事务仅在客户端启动,wcf,Wcf,我需要实现以下场景: 若客户机启动了事务,它将流向服务器,但若客户机并没有启动事务,那个么服务方法必须在并没有事务的情况下执行。可能吗?在我的例子中,没有TransactionScopeRequired=true,事务就不会流动 服务器: <system.serviceModel> <bindings> <netTcpBinding> <binding name="tcpTransactional" transactionFlow="t

我需要实现以下场景: 若客户机启动了事务,它将流向服务器,但若客户机并没有启动事务,那个么服务方法必须在并没有事务的情况下执行。可能吗?在我的例子中,没有TransactionScopeRequired=true,事务就不会流动

服务器:

<system.serviceModel> <bindings> <netTcpBinding> <binding name="tcpTransactional" transactionFlow="true" /> </netTcpBinding> </bindings> <services> <service name="WcfServiceLibrary1.TcpTransactionalService"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpTransactional" contract="WcfServiceLibrary1.ITcpTransactionalService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/TcpTransactionalService/" /> <add baseAddress="net.tcp://localhost:8730/Design_Time_Addresses/WcfServiceLibrary1/TcpTransactionalService/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="True"/> <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> [ServiceContract] public interface ITcpTransactionalService { [OperationContract] [TransactionFlow(TransactionFlowOption.Allowed)] void DoWork(); } //[OperationBehavior(TransactionScopeRequired = true)] public void DoWork() { Debug.Assert(Transaction.Current != null); Debug.Assert(Transaction.Current.TransactionInformation.DistributedIdentifier != Guid.Empty); } [服务合同] 公共接口ITcpTransactionalService { [经营合同] [TransactionFlow(TransactionFlowOption.Allowed)] 无效销钉(); } //[操作行为(TransactionScopeRequired=true)] 公共工作 { Assert(Transaction.Current!=null); Assert(Transaction.Current.TransactionInformation.DistributeIdentifier!=Guid.Empty); } 客户:

<system.serviceModel> <bindings> <netTcpBinding> <binding name="NetTcpBinding_ITcpTransactionalService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="true" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Transport"> <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> <message clientCredentialType="Windows" /> </security> </binding> </netTcpBinding> </bindings> <client> <endpoint address="net.tcp://localhost:8730/Design_Time_Addresses/WcfServiceLibrary1/TcpTransactionalService/" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITcpTransactionalService" contract="TcpTransactionalService.ITcpTransactionalService" name="NetTcpBinding_ITcpTransactionalService"> <identity> <dns value="localhost" /> </identity> </endpoint> </client> </system.serviceModel> private static void TestTcp() { using (var scope = new TransactionScope()) { var client = new TcpTransactionalService.TcpTransactionalServiceClient(); client.DoWork(); Debug.Assert(Transaction.Current != null); Debug.Assert(Transaction.Current.TransactionInformation.DistributedIdentifier != Guid.Empty); scope.Complete(); } } 私有静态void TestTcp() { 使用(var scope=new TransactionScope()) { var client=new TcpTransactionalService.TcpTransactionalServiceClient(); client.DoWork(); Assert(Transaction.Current!=null); Assert(Transaction.Current.TransactionInformation.DistributeIdentifier!=Guid.Empty); scope.Complete(); } }
使用TransactionScopeRequire=true

如果客户端不发送事务,则不会以分布式事务结束。如果是这样的话,你也会这样做

现在,这仍将调用本地事务中的操作。如果要禁止此操作,请检查分布式标识符。如果其Guid.Empty为空,请在另一个创建如下的TransactionScope中运行其余代码:

 if (Transaction.Current.TransactionInformation.DistributedIdentifier == Guid.Empty)
 {
    using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
    {
       DoWork();
       scope.Complete();
    }
 }
 else
 {
        DoWork();
 }