繁体   English   中英

如何关闭WCF客户端连接

[英]How to close a WCF Client connection

我是WCF的新手,到目前为止已经学习了。 但是我想知道如何从客户端关闭WCF客户端连接(如果需要的话?我想是)。

我有一个名为

[ServiceContract]
ICalculatorService { blabla... }

问题是在客户端。 到目前为止,我使用以下格式:

EndpointAddress epAddress = new EndpointAddress("http://localhost:8090/CalculatorService");
ICalculatorService calculatorProxy = ChannelFactory<ICalculatorService>.CreateChannel(new WSHttpBinding(), epAddress);

现在我可以:

Result numeralResult = calculatorProxy.AddNumbers(4, 5);

我得到了结果,我很高兴。

每按一次(例如)按一次按钮,就会使上述代码运行一次。

我的问题是:效率高吗?

现在,我正在考虑将其放入一个单独的类中,例如:

class CalculatorProxy
{
    static EndpointAddress epAddress = new EndpointAddress("http://localhost:8090/CalculatorService");

    public static ChannelFactory<ICalculatorService> GetCalculatorProxy()
    {

    }

    public void Dispose() { ... }
}

...并像这样使用它:

using (ICalculatorService calculatorClient = CalculatorProxy.GetCalculatorProxy())
{
    calculatorClient.AddNumbers(4, 4);                
}

哪一个会更有效?

更新:

谢谢大家的答案。

我终于结束了这堂课:

class CalculatorServiceClient : ClientBase<ICalculatorService>, IDisposable
{
    static EndpointAddress epAddress = new EndpointAddress("http://localhost:8090/CalculatorService");
    ICalculatorService myCalculatorProxy;

    public CalculatorServiceClient()
        : base(new WSHttpBinding(), epAddress)
    {
        myCalculatorProxy = ChannelFactory.CreateChannel();
    }

    public static CalculatorServiceClient GetNewInstance()
    {
        return new CalculatorServiceClient();
    }

    public Result AddNumbers(int aIn, int bIn)
    {
        return myCalculatorProxy.AddNumbers(aIn, bIn);
    }

    public void Dispose()
    {
        try
        {
            Close();
        }
        catch (CommunicationObjectFaultedException ex)
        {
            throw new DBCommunicationException("CalculatorServiceClient is in the Faulted state.", ex);
        }           
        catch (Exception ex)
        {
            throw new DBCommunicationException("Communication is unsuccessful between the CalculatorServiceClient and the CalculatorService.", ex);
        }
    }
}

并以这种方式使用它:

try
{
    using (CalculatorServiceClient calculatorClient = CalculatorServiceClient.GetNewInstance())
    {
        Result aResult = calculatorClient.AddNUmbers(tbA.Text, tbB.Text);
    }
}
catch (DBCommunicationException ex)
{
    MessageBox.Show("Service is shut down.");
}

我的问题是:这样有效吗?

您只需在完成每个操作后关闭客户端,就不再需要进行其他调用了。 工作完成后,只需使用Close方法Close客户端:

calculatorProxy.Close();

关于Close()方法,MSDN文档指出:

此方法使CommunicationObject正常地从除Closed状态以外的任何状态转换为Closed状态。 使用Close方法可以在返回之前完成所有未完成的工作。 例如,完成所有缓冲消息的发送。

关于您的方法,我认为第二种方法很好而且效率更高,因为您还正在实现Dispose模式并释放使用的资源(这取决于您使用的资源)。 只需在工作完成后添加Close()方法即可:

calculatorClient.AddNumbers(4, 4);
calculatorProxy.Close();

还请记住,创建和连续关闭WCF客户端没有性能问题。 这只是正常习惯。

您可以调用代理类的close方法。

喜欢

calculatorProxy.Close();

或者,如果发生异常 ,则在服务代理类上调用abort方法。

 try
 {
       calculatorProxy.SomeMethod();
       calculatorProxy.Close();
 }
 catch
  {
       calculatorProxy.Abort();
   }

请参阅链接以获取更多详细信息

我认为您最好将所有这些都放在一个类中。 建立类的实例可以构造连接并在时间到时关闭/处置。 在此之前,您将有一个开放且活动的频道可以拨打电话。

using (var client = new CalculatorServiceClient())
{
    client.SomeMethod();
}

将对计算器Web服务的服务引用添加到客户端项目后,CalculatorServiceClient对象将可用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM