1

WCF複数のクライアントが接続しているサービスがあります。

私たちのサービスは、インスタンスごとのサービスになるように設定されています。サービスは、その作業を行うために別のインスタンス オブジェクトにアクセスする必要があります。必要なインスタンスは wcf サービスではなく、必要なインスタンスをシングルトンにしたくありません。

サービスが私が作成したオブジェクトである場合は、対話する必要があるインスタンス オブジェクトを渡すだけです。ただし、これは wcf サービスであるため、wcf によって作成されます。

サービスの作成をフックして、使用するデータ/インターフェイスを渡す方法、または作成後にサービスへのポインターを取得して、必要なインスタンスを渡す方法を教えてください。

[ServiceContract]    
public interface IMyService
{
    [OperationContract(IsOneWay = true)]
    void DoSomethingCool();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
class MyService : IMyService 
{
    private IHelper helper;
    void DoSomethingCool()
    {
        // How do I pass an instance of helper to MyService so I can use it here???
        helper.HelperMethod();
    }
}
4

2 に答える 2

0

さて、私が最終的にやったのは、ヘルパー クラスをシングルトンにする代わりに、シングルトンの ServiceGlue オブジェクトを仲介として作成することでした。

サービスとヘルパーはどちらもシングルトンである仲介者に自己登録し、シングルトンはインスタンスをやり取りします。

サービスをセットアップする前に、シングルトンをインスタンス化し、ヘルパーを登録して、各サービスがヘルパー オブジェクトを取得できるようにします。

これにより、私のコードは次のようになります。

public class ServiceGlue
{
    private static ServiceGlue instance = null;
    public static ServiceGlue Instance
    {
        get
        {
            if (instance == null)
                instance = new ServiceGlue();
             return instance;
         }
     }

    public Helper helper { get; set; }
}


[ServiceContract]    
public interface IMyService
{
    [OperationContract(IsOneWay = true)]
    void DoSomethingCool();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode =     ConcurrencyMode.Multiple)]
class MyService : IMyService 
{
    private IHelper helper;
    public MyService()
    {
       // use an intermidiary singleton to join the objects
       helper = ServiceGlue.Instance.Helper();
    }

    void DoSomethingCool()
    {
       // How do I pass an instance of helper to MyService so I can use it here???
       helper.HelperMethod();
    }
}
于 2013-10-24T18:52:26.640 に答える