TransactionFlowAttribute属性设置为Mandatory,但通道的绑定未配置TransactionFlowBindingElement

时间:2012-04-11 19:33:54

标签: wcf distributed-transactions

我有一个大问题。我正在尝试创建一个可以处理分布式事务的Web服务 下面的所有代码都在Web服务的服务器端(从客户端调用的Web服务) 我在我的界面中写了这个:

[ServiceContract]
public interface IClientOperations
{

    [OperationContract]
    [ServiceKnownType(typeof(TriggerExecInput))]
    [ServiceKnownType(typeof(TriggerExecOutput))]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    TriggerExecOutput TriggeredProfileDataUpdate(TriggerExecInput triggerInputData, bool isST3StatusActive);

这是在web.config文件中:

<services>
  <service name="ClientOperationsService" behaviorConfiguration="ServiceBehavior">
    <endpoint address="" binding="wsHttpBinding" 
              bindingConfiguration="wsHttpBinding_Common" contract="SL.STAdmin.Applications.WebAPI.IClientOperations"/>
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
  </service>
</services>

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpBinding_Common" transactionFlow="true">
    </binding>
  </wsHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <!-- 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>

如果我右键单击.svc文件并单击“在浏览器中查看”,我会收到以下错误

Exception Details: System.InvalidOperationException: At least one operation on the 'ClientOperations' contract is configured with the TransactionFlowAttribute attribute set to Mandatory but the channel's binding 'BasicHttpBinding' is not configured with a TransactionFlowBindingElement. The TransactionFlowAttribute attribute set to Mandatory cannot be used without a TransactionFlowBindingElement.

我有其他不使用交易的.svc文件。 他们都运作良好。 我不明白为什么当我指示它使用其他绑定类型时它仍然试图使用BasicHttpTransaction。

有谁知道我做错了什么? 提前谢谢。

2 个答案:

答案 0 :(得分:0)

在web.config的<system.serviceModel>元素中添加:

<protocolMapping>
  <add scheme="http" binding="wsHttpBinding" bindingConfiguration="wsHttpBinding_Common"/>
</protocolMapping>

答案 1 :(得分:0)

您需要做一些事情才能使交易有效。 将事务流添加到您的操作

[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
void TransactionSupported(int id, string name);

之后,您将向操作添加一个操作行为

[OperationBehavior(TransactionScopeRequired = true)]
public void TransactionSupported(int id, string name)
{
    ...
}

在配置文件中,您需要将事务流添加到主机绑定

<system.serviceModel>
    ...
    <bindings>
      <netNamedPipeBinding> --> Your binding (don't use basicHttpBinding)
        <binding transactionFlow="true"/>
      </netNamedPipeBinding>
    </bindings>
  </system.serviceModel>

最后但并非最不重要的是,您需要设置客户端的事务流以使其正常工作。在我的示例中,我在单元测试中的代码中执行此操作,我认为您也可以在配置文件的配置中执行此操作。

var factory = new ChannelFactory<IService>(callback,
              new NetNamedPipeBinding() { TransactionFlow = true },
              new EndpointAddress("net.pipe://localhost/ping"));