2011-12-10 41 views
1

現在、wcf publish購読サービスを開発中です。私のWindowsフォームアプリケーションでは、私はサービスに加入しようとする接続ボタンがあります。例外処理用としてwcfサービスの例外処理

WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding; 
string clientcallbackaddress = binding.ClientBaseAddress.AbsoluteUri; 
clientcallbackaddress += Guid.NewGuid().ToString(); 
binding.ClientBaseAddress = new Uri(clientcallbackaddress); 

InstanceContext site = new InstanceContext(null, new mainForm(backgroundFormObject)); 
PostingContractClient client = new PostingContractClient(site); 

client.Subscribe(); 
MessageBox.Show("Service has been connected!", "Connected"); 

を次のようにコードが私のサービスは、[接続]ボタンをクリックすると、アクティブ化されていない場合、それはしばらくの間にロードされます

try 
{ 
    //refer to codes above 
} 
catch (EndpointNotFoundException) 
{ 
    MessageBox.Show("WCF Service has not been started.", "Error"); 
} 
catch (CommunicationException) 
{ 
    MessageBox.Show("Error"); 
} 

、これは私がやっていることであり、最初のエラーを送信する前に、「WCFサービスは開始されていません。その後、サービスを有効にして、[接続]ボタンをクリックすると、サービスがオンになっているにも関わらずCommunicationExceptionという2番目のエラーが表示されます。

これを修正する方法はありますか?それが障害状態であるため

エラー

通信オブジェクトのエラーメッセージ、System.ServiceModel.Channels.ServiceChannelは、通信のために使用することができません。

+0

、のForm_Loadメソッドであなたの "接続" ボタンで

site = new InstanceContext(null, new mainForm(backgroundFormObject)); client = new PostingContractClient(site); 

そして最後にすることによって従っ内部の例外? – Tim

+0

こんにちは。上記の私の投稿をエラーメッセージで更新しました。ありがとう – Thomas

答えて

1

ないこれが何をしたいですが、私はあなたの質問から理解することができたものから、これはあなたが必要なもののように思えるかどうかわから:

あなたがプロジェクト全体を通じて利用できるようにするための変数を必要とするので、あなたが宣言することができますクラスの先頭にあるInstanceContextとClient。

InstanceContext site; 
PostingContractClient client; 

て、CommunicationExceptionのエラーメッセージが言うと、そこにあるん何

 try 
     { 
      site = new InstanceContext(null, new mainForm(backgroundFormObject)); 
      var client = new PostingContractClient(site); 

      WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding; 
      string clientcallbackaddress = binding.ClientBaseAddress.AbsoluteUri; 
      clientcallbackaddress += Guid.NewGuid().ToString(); 
      binding.ClientBaseAddress = new Uri(clientcallbackaddress); 

      client.Subscribe(); 
      MessageBox.Show("Service has been connected!", "Connected"); 
     } 
     catch (EndpointNotFoundException) 
     { 
      if (client != null) 
      { 
       MessageBox.Show("WCF Service has not been started. Please try to manually connect again after the service has been started.", "Error"); 
3

ここでの問題は、以前の呼び出しでエラーが発生し、通信チャネルを失敗した状態にしたことです。したがって、それが閉じられるまで使用することはできません。

エラーをキャッチし、フォールト状態でクリーンアップしてチャネルする必要があります。

私たちが使用するいくつかの標準的なコードです。あなたの場合は、チャンネルを閉じたくないかもしれません。

タイムアウトが発生しているため、チャネルに障害が発生している可能性があります。

私は少し上記のコードを混同だし、コメントでそれを説明しようと
 try 
     { 
       client.Subscribe(); 
     } 

     catch (Exception exception) 
     { 

      if (client != null) 
      { 
       client.Abort(); 
      } 
      throw; 
     } 

     finally 
     { 
      if (client != null) 
      { 
       client.Close(); 
      } 
     } 
+0

こんにちは。あなたの返事に感謝します。あなたが与えたコードを試した後、接続ボタンをクリックすると、エラーを1回送信します。その後、2回目のクリックで、「廃棄されたオブジェクトにアクセスできません。」というエラーメッセージが表示されます。 オブジェクト名: 'System.ServiceModel.Channels.ServiceChannel'。 " – Thomas

+0

私はクライアント変数をドキュメント全体で利用できるようにするために、私は接続しようとするたびに新しい変数を宣言するのではなく、プログラムの開始時に変数を作成しました。 – Thomas

+1

それはあなたの問題があるところです。 「クライアント変数」(サービスチャネル)に障害が発生すると、それを再び使用することはできません。接続するたびに新しいインスタンスを作成する必要があります。 –

1

とにかく
// The client seems to be initialized by that time 
WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding; 

... 

// The client is initialized again 
PostingContractClient client = new PostingContractClient(site); 

、エラーから、あなたがしようとしていること、それはそう、掲載しています以下のように、結果的に通話の両方に同じオブジェクトを使用する:

var client = new Client(); 
client.Subscribe(); // you get an exception here 
client.Subscribe(); // channel is faulted, as you got an exception accessing it the first time 

私は参照としてWCFサービスを追加するとあまりうまくいかなかった、と私は実装の詳細を知りません。

var client = new Client(); 
client.Subscribe(); // you get an exception here 

client = new Client(); // I hope, another Channel is created, when calling the constructor. 
client.Subscribe(); 

敬具:しかし、多くを共有してアセンブリでの作業、私はClientもう一度を作成しようとすることをお勧めします。

+0

こんにちは。あなたの応答に感謝します。クライアントを再作成して、次のコードを使用しました。var client = new PostingContractClient(); PostingContractClientに0引数を取るコンストラクタが含まれていないというエラーが表示されます。私は何を引数として入れるのか分かりません。 – Thomas

+0

私は、以前と同じコンストラクタを使うべきだと言っていました。あなたのケースでは '新しいPostingContractClient(サイト);' –