Reputation: 2335
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. 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. 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:
public void Dispose()
{
Dispose(true);
}
The dispose method was one I copied from the internet and looks like:
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. 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.
I have included System.ServiceModel, System.ServiceModel.Web and System.ServiceModel.Channels and these don't seem to resolve it.
So my questions are:
1) Am I implementing IDisposable correctly for the scenario above (website creating service connections to retrieve data)
2) If so then how can I resolve the errors in the Disposing method?
Any help appreciated.
Just for reference - what I am looking to do is something like the following:
using (IIntranet proxy = factory.CreateChannel())
{
}
Upvotes: 3
Views: 1669
Reputation: 759
State
property belongs to IClientChannel
interface. So you can use it like that : ((IClientChannel)proxy).State
Upvotes: 0
Reputation: 69260
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. It is not meant to be use with the ChannelFactory<T>
class.
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>
).
Upvotes: 2