//------------------------------------------------- // WCFの公開メソッドをコール //------------------------------------------------- public void HelloWorld() { SetLog("-- ↓ --"); //★App.config化により不要 StartService(); try { //★App.config化により追記 using (var client = new GabekoreApiService.ServiceClient(new InstanceContext(this))) { // サービスメソッドの呼び出し. // string HelloWorld(string name); //★App.config化により変更 var result = _service.HelloWorld("Pochi"); var result = client.HelloWorld("Pochi"); SetLog(result); } } catch (CommunicationException ex) { // エラー発生 // // WCFでは、サービスとクライアント間の通信中にエラーが発生した場合 // CommunicationExceptionがスローされる。 MessageBox.Show(ex.Message); SetLog(ex.Message); } SetLog("-- ↑ --"); }
//------------------------------------------------- // WCFサービスオープン/クローズの処理 //------------------------------------------------- //★App.config化により不要 /// <summary> //★App.config化により不要 /// WCFサービス開始 //★App.config化により不要 /// </summary> //★App.config化により不要 private void StartService() //★App.config化により不要 { //★App.config化により不要 if (_service != null || _channel != null) //★App.config化により不要 { //★App.config化により不要 return; //★App.config化により不要 } //★App.config化により不要 //★App.config化により不要 try //★App.config化により不要 { //★App.config化により不要 // 参照設定:System.ServiceModel.dll //★App.config化により不要 EndpointAddress endpointAddr = new EndpointAddress(ServiceBaseAddr); //★App.config化により不要 //★App.config化により不要 // チャネルを構築しサービス呼び出しを行う. //★App.config化により不要 _channel = new ChannelFactory<IService>(new BasicHttpBinding()); //★App.config化により不要 //★App.config化により不要 // サービスへのプロキシを取得. //★App.config化により不要 _service = _channel.CreateChannel(endpointAddr); //★App.config化により不要 } //★App.config化により不要 catch (CommunicationException ex) //★App.config化により不要 { //★App.config化により不要 // エラー発生 //★App.config化により不要 // //★App.config化により不要 // WCFでは、サービスとクライアント間の通信中にエラーが発生した場合 //★App.config化により不要 // CommunicationExceptionがスローされる。 //★App.config化により不要 MessageBox.Show(ex.Message); //★App.config化により不要 SetLog(ex.Message); //★App.config化により不要 } //★App.config化により不要 } //★App.config化により不要 /// <summary> //★App.config化により不要 /// WCFサービス終了 //★App.config化により不要 /// </summary> //★App.config化により不要 public void StopService() //★App.config化により不要 { //★App.config化により不要 if (_service == null && _channel == null) //★App.config化により不要 { //★App.config化により不要 return; //★App.config化により不要 } //★App.config化により不要 //★App.config化により不要 using (_channel) //★App.config化により不要 { //★App.config化により不要 _channel.Close(); //★App.config化により不要 } //★App.config化により不要 _service = null; //★App.config化により不要 _channel = null; //★App.config化により不要 } //------------------------------------------------- // コールバック登録処理 //------------------------------------------------- //private GabekoreApiService.ServiceClient _client; public void CallbackInitial() { // 双方向通信を行う場合、サービス側にコールバックの実装を教える必要がある。 // 参照設定:System.ServiceModel.dll var context = new InstanceContext(this); var _client = new GabekoreApiService.ServiceClient(context); string push_id = CmbPushIdSelectedItem.DisplayValue; if (push_id == null || push_id.Trim() == string.Empty) { SetLog("プッシュIDが空なので中止"); return; } _client.CallbackRegist(push_id); }