2017-05-19 1 views
0

Apiコントローラでwcfサービスを閉じるには 私のWCFサービスオブジェクトにはcloseメソッドがありません!MVC C#(Umbraco Apiコントローラ)でWCFサービスを閉じる方法

プログラムは必要に応じて動作します。

private readonly ICarWebServices Service 


     public CarBookingApiController(ICarWebServices Service) // Injecting 
     { 
      this.Service = Service;  
     } 





     public IHttpActionResult GetAvaliableCars(string userId) 
     { 
      try 
      { 
       List<Cars> cars = Service.GetCars(userId).ToList(); 

      // Service.Close() ?????????????????????? 

      } 

      catch (Exception exception) 
      { 
      } 
      return Ok<List<Cars>>(cars); 
     } 

答えて

1

私は常にサービスクライアントを作成します。呼び出しが成功した場合は "client.Close()"を使用してサービスを終了し、呼び出しが失敗した場合は "client.Abort()"を使用する必要があります。以下の例を参照してください。あなたの場合は、閉じたか中止することができる変数に注入されたサービスを保存する必要があります。

NetTcpBinding netTcpBinding = new NetTcpBinding(); 
    netTcpBinding.Name = "NetTcpBinding_IYourService"; 

    string remoteUrlAddress = "net.tcp://localhost:24888/YourService"; 
    EndpointAddress endpointAddress = new EndpointAddress(remoteUrlAddress); 

    List<Message> lstMessages = new List<Message>() { }; 
    YourServiceClient client = new YourServiceClient(netTcpBinding, endpointAddress);    
    try 
    { 
     lstMessages = client.GetAllMessages(); 
     client.Close(); 
    } 
    catch (FaultException<EntityFrameworkFault> faultException) 
    { 
     var errorText = constructEFErrorText(faultException); 
     client.Abort(); 
    } 
    return lstMessages; 
関連する問題