2016-05-19 8 views
0

2つのクライアントプロキシを呼び出すときに2つのサービス間で変数を共有したい。 2つの異なるクライアント呼び出しで2つのサービス間で変数を共有

は**サーバー**

サービス1

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)] 
public class MyService : IMyService 
{ 
    public string SharedValue = string.Empty; 

    public void SETValue(string inputString) 
    { 
     SharedValue = inputString; 
    } 
} 

[ServiceContract(SessionMode = SessionMode.Required)] 
public interface IMyService 
{ 
    [OperationContract(IsOneWay = true)] 
    void SETValue(string inputString); 
} 

サービス2

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)] 
public class MyService2 : MyService, IMyService2 
{ 
    public string GETSharedValue() 
    { 
     return base.SharedValue; //This value got Empty 
    } 
} 

[ServiceContract(SessionMode = SessionMode.Required)] 
public interface IMyService2 
{ 
    [OperationContract] 
    string GETSharedValue(); 
} 

I」は、次のように私のサンプルコード(そして、また変数は、セッションごとに共有される必要があります)両方のサービスに 'wsHttpBinding'を使用しています。

**クライアント**

private string sharedValue = string.Empty; 
    MyServiceReference.MyServiceClient client = null; 
    MyService2Reference.MyService2Client client2 = null; 

    public Form1() 
    { 
     InitializeComponent(); 

     client = new MyServiceReference.MyServiceClient(); 
     client2 = new MyService2Reference.MyService2Client(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     client.SETValue("Hello this is client 1"); 

     sharedValue = client2.GETSharedValue(); 
     lblResult.Text = sharedValue; 
    } 
+0

WCFセッションは通常サービスごとです。非永続である必要がありますか?値が実際に属している「セッション」が何であるかを知るための認証や方法がありますか?それをユーザーIDなどに結びつけることはできますか? – smoksnes

+0

InstanceContextSharingを見ることができます。 https://msdn.microsoft.com/en-us/library/aa354514.aspx – smoksnes

+0

クライアントオブジェクトを一度だけインスタンス化します。いいえ、現在認証を使用していません。認証方法を見つける方法はありますか?返信いただきありがとうございます。私はあなたのリンクを確認します。 – Neo

答えて

0

は、例えば 静的変数を持つクラスを書く:あなたはロックを使用して同時実行cosiderをしたい場合

public static class SharedValues 

    { 
     public static String SharedValue = string.Empty; 
    } 

その後、

public void SETValue(string inputString) 
    { 
     SharedValues.SharedValue = inputString; 
    } 

を使用ステートメント

+0

他のセッションでもその文字列を利用できないでしょうか? – smoksnes

+0

私はグローバル変数のようなすべてのセッションで共有する静的変数を試しました。 – Neo

+0

@smoksnes Shamilrox申し訳ありませんが、私はそれを行う1つの方法は、InstanceContextSharing smoksnesだと思うが、私はそれを試していない、別の方法は、クライアント側(発信者)機能と静的なキー値を維持する補助的なサーバー側 –

関連する問題