2

我有一个大问题。我正在尝试创建一个可用于分布式事务的 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。

有人知道我做错了什么吗?先感谢您。

4

2 回答 2

0

将此添加到<system.serviceModel>web.config 的元素中:

<protocolMapping>
  <add scheme="http" binding="wsHttpBinding" bindingConfiguration="wsHttpBinding_Common"/>
</protocolMapping>
于 2012-11-14T01:54:22.007 回答
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"));
于 2015-10-08T09:27:22.957 回答