1

少しトリッキーな開発で数日間立ち往生しています。説明:

機能上の必要性: 同じコントラクトを共有する異なるバインディング タイプを持つ一意のサービスを公開し、クライアントの機能で使用するバインディングを実行時に切り替えます (.Net クライアントの場合は net.tcp を使用し、Java クライアントの場合は http バインディングを使用します)。

ここで私は私の設定ファイルにいます:

  <!-- Test Service -->
  <service name="TestService.v1.TestServiceImplementation, TestService" behaviorConfiguration="MyBehavior">
        <endpoint name="TestServiceV1Htpp" contract="ITestService.v1" address="http://localhost:6001/TestService/v1" binding="basicHttpBinding"  bindingConfiguration="HttpConf" behaviorConfiguration="HttpSOAPBehavior"/>
        <endpoint name="TestServiceV1NetTcp" contract="ITestService.v1" address="net.tcp://localhost:6002/TestService/v1" binding="customBinding" bindingConfiguration="TcpConfStream" behaviorConfiguration="TcpSOAPBehavior"/>
  </service>

TestService dataContract :

[ServiceContract(...)]
public interface ITestService : IDisposable
{
    [OperationContract]
    IEnumerable<string> GetData();
}

[ServiceBehavior(...)]
public class TestServiceImplementation : ITestService
{
     public IEnumerable<string> GetData()
    {
        yield return "Pong";
    }
}

そして、「実行時」のコントラクトの変更(エンドポイントの動作で、ストリームされた戻り結果を偽造するため):

public sealed class CustomBehavior : IEndpointBehavior
{
    void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        foreach (var msg in endpoint.Contract.Operations.First().Messages)
        {
            var part = msg.Body.ReturnValue;
            if (part != null)
                part.Type = typeof(Stream);
        }           
    }
}

実行:

CustomBehavior を使用しない場合、すべてが完全に機能します。それをTCP エンドポイント(TcpSOAPBehavior)の動作構成に追加すると、Body.ReturnValue.Type が変更され、この変更により、すべてのエンドポイント(http も含む)のすべてのコントラクトが変更されます。TCPエンドポイントコントラクトを変更したいだけですが、HTTPのものには触れません...そのような変更を行うことは可能ですか? それとも、これらのエンドポイントは、同じコントラクトを永久に共有することを意図していますか?

4

1 に答える 1

0

数日間の作業の後、解決策を見つけました。次のエラー メッセージが表示されました。

「InnerException メッセージは 'Type 'System.Collections.Generic.List`1[[System.String]]' で、データ コントラクト名 'ArrayOfValuationResult_testService_wsdl' は想定されていません。既知のタイプのリストに静的に知られていないタイプを追加します。たとえば、KnownTypeAttribute 属性を使用するか、DataContractSerializer に渡される既知の型のリストにそれらを追加します。

このブログのように、インターフェイスで ServiceKnownTypeAttribute を使用しました: http://blogs.msdn.com/b/youssefm/archive/2009/04/21/understanding-known-types.aspx

このようにして、IEnumerable を公開できますが、それを Stream に変換し、TCP/Http の両方のバインドで呼び出しを成功させることができます。

于 2013-12-18T15:50:51.277 に答える