【问题标题】:How to close WCF service in MVC C# (Umbraco Api controller)如何在 MVC C#(Umbraco Api 控制器)中关闭 WCF 服务
【发布时间】:2017-05-19 14:12:54
【问题描述】:

如何在我的 Api 控制器中关闭我的 wcf 服务 我的 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);
        }

【问题讨论】:

    标签: c# asp.net-mvc wcf model-view-controller umbraco


    【解决方案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;
    

    【讨论】:

      猜你喜欢
      • 2012-01-14
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 2017-04-26
      相关资源
      最近更新 更多