System.ServiceModel.Clientbase.Open()做什么?

xr2*_*0xr 5 c# wcf

System.ServiceModel.Clientbase.Open()做什么?我从来没有使用它,但只是在一些代码中遇到它.可以抛出异常吗?如果没有调用Close()是一个问题吗?

Vin*_*rgh 4

如果您为 WCF 服务创建代理,则该代理实际上是 ClientBase

我的应用程序的示例:

public class DataClient : ClientBase<Classes.IDataService>, Classes.IDataService
{
    public DataClient(string connectToHost)
        : base(new NetTcpBinding(SecurityMode.Transport)
            {
                PortSharingEnabled = true,
                Security = new NetTcpSecurity()
                {
                    Transport = new TcpTransportSecurity()
                    {
                        ClientCredentialType = TcpClientCredentialType.Windows
                    }
                }
            },
            new EndpointAddress(string.Format("net.tcp://{0}:5555/MyService",connectToHost)))
    { }

    #region IDataService Members

    public Classes.Folder GetFolder(string entryID)
    {
        return Channel.GetFolder(entryID);
    }

    public Classes.IItem GetItem(string entryID)
    {
        return Channel.GetItem(entryID);
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

编辑 根据您的要求,我用谷歌搜索了一下,发现了这个

实现 ICommunicationObject.Open()

这导致了这个

通讯异常

ICommunicationObject 无法打开并已进入“故障”状态。

超时异常

在 ICommunicationObject 能够进入“已打开”状态并进入“故障”状态之前,默认打开超时已过。

另外,根据经验和我在网上遇到的情况,不关闭您的客户可能会导致各种形式的奇怪现象发生,因此通常被认为是“一件坏事”。