2

WCFクライアントの実装についてのご意見をお聞かせください。

SecurityManagerのようないくつかのサービスを提供するサーバーがあります。このサービスは、インターフェイスISecurityManagerで定義され、クラスSecurityManagerに実装されます。

これまでのところ、すべてが順調です。クライアント側では、分離されたクラスを介してサービス呼び出しを実装したいと思います。私の質問は、同じISecurityManagerインターフェイスを実装するSecurityManagerクラスでもこれを行うかどうかです。

ここでのベストプラクティスは何ですか?

4

3 に答える 3

2

Visual Studio ジェネレーター

クライアント プロジェクトを右クリックし、Service Reference. サービス URL を入力するか、ソリューション内から検索できるダイアログがあります。

クライアントの作成

から継承したクライアント クラスを構築できますClientBase<ISecurityManager>, ISecurityManager。このクライアント クラスでの操作例:

public void ExampleMethod(int id)
{
   Channel.ExampleMethod(id);
}

本物の男のように

または、クライアント クラスを使用せずに、次のように呼び出します。

ServiceInvokerinvoker invoker = new ServiceInvoker(); 
var result = invoker.InvokeService<ISecurityManager, ReturnType>(     proxy => proxy.ExampleMethod(1) ); 

ISecurityManagerすでにクライアントを構成していると仮定した場合の最後の 2 つのオプション:

<client>     
<endpoint name="ServiceName" 
address="http://ServiceName.test/Service" 
binding="basicHttpBinding"   
contract="ISecurityManager" /> 
</client> 
于 2012-09-17T15:54:57.727 に答える
2

一般的なラッパーを使用して WCF 呼び出しを行うことをお勧めします。したがって、WCF 呼び出しを行う必要があるたびに、次のように実行できます。

var invoker = new ServiceInvoker();
        var result = invoker.InvokeService<ISecurityManager, MyObjectReturnType>(
            proxy => proxy.DoSomething(myParameters));
        return result;

ServiceInvoker を使用してチャネルを作成し、内部で発生する例外を管理します。ISecurityManager コントラクト、MyObjectReturnType の戻り型、アクション DoSomething (ISecurityManager コントラクトのメソッド) を使用してチャネルを作成します。このソリューションは、追加のクラス実装なしで、すべてのインターフェイスで使用できます!

InvokeService は次のようになります。

public TResult InvokeService<TServiceContract, TResult>(Func<TServiceContract, TResult> invokeHandler) where TServiceContract : class
    {
        ICommunicationObject communicationObject;
        var arg = CreateCommunicationObject<TServiceContract>(out communicationObject);
        var result = default(TResult);
        try
        {
            result = invokeHandler(arg);
        }
        catch (Exception ex)
        {
            Logger.Log(ex);
            throw;
        }

        finally
        {
            try
            {
                if (communicationObject.State != CommunicationState.Faulted)
                    communicationObject.Close();
            }
            catch
            {
                communicationObject.Abort();
            }
        }
        return result;
    }

private TServiceContract CreateCommunicationObject<TServiceContract>(out ICommunicationObject communicationObject)
        where TServiceContract : class
    {
        //Create the Channel
        // ICommunicationObject is an out parameter for disposing purposes 
        return channel;
    }
于 2012-09-17T15:41:55.977 に答える
0

「別のクラスを介して」サービス呼び出しを実装するには、サービスの実行中のインスタンスから svcutil.exe または Visual Studio を使用してサービス参照を生成するだけです。

これにより、サービス コントラクトと実装を含むアセンブリから完全に「分離」される、サービス コントラクトから一連の型が生成されます。

于 2012-09-17T15:53:35.923 に答える