简体   繁体   English

为WCF ChannelFactory实现IDisposable <T>

[英]Implementing IDisposable for WCF ChannelFactory<T>

We have a web service which is used to take requests from a website and extract data from a database to return to the website. 我们有一个Web服务,用于从网站获取请求并从数据库中提取数据以返回到网站。 It all seems to work fine, but there has been something that has been bugging me since we developed it. 这一切似乎都很好,但是自从我们开发它以来,一直有一些困扰我的东西。 My code for establishing the connection to the service from the client is as follows: 我从客户端建立与服务的连接的代码如下:

string bindingName = ConfigurationManager.AppSettings["BindingName"];
ChannelFactory<IIntranet> factory = new ChannelFactory<IIntranet>(bindingName);
IIntranet proxy = factory.CreateChannel();

Now the proxy exposes all of the methods in my interface, but one thing it doesn't do is implement IDisposable. 现在,代理公开了我界面中的所有方法,但是它没有做的一件事就是实现IDisposable。 So after a bit of googling I found various people saying we had to implement the interface, which I did as follows: 所以经过一些谷歌搜索后,我发现各种人都说我们必须实现界面,我做了如下:

public class Service : IInternet, IIntranet, IDisposable

The IDisposable provides the Dispose method, which is as follows: IDisposable提供Dispose方法,如下所示:

public void Dispose()
{
    Dispose(true);
} 

The dispose method was one I copied from the internet and looks like: dispose方法是我从互联网上复制的方法,看起来像:

protected virtual void Dispose(bool disposing)
{
    if (disposing)
    {
        try
        {
            if (State != CommunicationState.Faulted)
            {
                Close();
            }
        }
        finally
        {
            if (State != CommunicationState.Closed)
            {
                Abort();
            }
        }
    }
}

Now the problem I have is that the compiler doesn't recognise State, Close or Abort as being valid keywords. 现在我遇到的问题是编译器不会将State,Close或Abort识别为有效关键字。 I can resolve State and it goes to System.Activities.Debugger which doesn't seem right to me at all and the others just don't resolve at all. 我可以解决国家问题,它会转到System.Activities.Debugger,这对我来说似乎不对,其他人根本就没有解决。

I have included System.ServiceModel, System.ServiceModel.Web and System.ServiceModel.Channels and these don't seem to resolve it. 我已经包含System.ServiceModel,System.ServiceModel.Web和System.ServiceModel.Channels,这些似乎无法解决它。

So my questions are: 所以我的问题是:

1) Am I implementing IDisposable correctly for the scenario above (website creating service connections to retrieve data) 1)我是否正确地为上述场景实现IDisposable(网站创建服务连接以检索数据)
2) If so then how can I resolve the errors in the Disposing method? 2)如果是,那么如何解决Disposing方法中的错误?

Any help appreciated. 任何帮助赞赏。

Update 更新

Just for reference - what I am looking to do is something like the following: 仅供参考 - 我希望做的是以下内容:

using (IIntranet proxy = factory.CreateChannel())
{  
}

Did you perhaps copy my dispose implementation ? 您是否复制了我的处置实施

In that case it is meant to be added as a partial for the someServiceClient class generated by the "add service reference" command in visual studio. 在这种情况下,它意味着添加为visual studio中“add service reference”命令生成的someServiceClient类的部分内容。 It is not meant to be use with the ChannelFactory<T> class. 它不适用于ChannelFactory<T>类。

It might be possible to make a similar solution for ChannelFactory<T> but the code will probably not (as you've found out) work with anytyhing else than the Wcf Client proxy (which inherits from ClientBase<T> ). 也许可以为ChannelFactory<T>创建一个类似的解决方案,但代码可能不会(正如您所知)与Wcf客户端代理(继承自ClientBase<T> )之外的任何其他方式一起工作。

  1. It is a wrong idea to dispose the service from a client. 从客户端部署服务是错误的想法。 Because you cannot control when your service will be disposed. 因为您无法控制何时处置您的服务。 The proper way is to dispose it from the server side when you want to close the service. 正确的方法是在要关闭服务时从服务器端部署它。
  2. The State property belongs to IClientChannel interface. State属性属于IClientChannel接口。 So you can use it like that : ((IClientChannel)proxy).State 所以你可以这样使用它: ((IClientChannel)proxy).State

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

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