I have an asp.net mvc application and need to call wcf service in it. I've added service reference to the project. I use unity for dependency injection and want to inject wcf client. But it's not so simple because of System.ServiceModel.ClientBase. I did it this way.
This is an autogenerated proxy:
public partial class WcfServiceClient :
System.ServiceModel.ClientBase<WcfUnity.IWcfServiceClient>,
WcfUnity.IWcfServiceClient
{
//autogenerated constructors and methods
}
I created interface:
public interface ISimpleProxy : WcfUnity.IWcfServiceClient
{
//duplication of methods' signatures
void CloseConnection();
}
I extended WcfServiceClient using partial class:
public partial class WcfServiceClient : ISimpleProxy
{
public void CloseConnection()
{
try
{
Close();
}
catch (Exception)
{
Abort();
}
}
}
And inject it like this:
container.RegisterType<ISimpleProxy>(new InjectionFactory(c => new WcfServiceClient()));
So I don't have dependecies from ClientBase and there is no wcf stuff inside classes which use this wcf proxy. Does this solution have some disadvatages?
ClientBasealready implementsIDisposable, so everything should be working fine without any work. Your code should only know aboutISimpleProxyand you should let Unity handleWcfServiceClientlogic. - AronISimpleProxyshould not exist. You should just haveIWcfServiceClientin your code. You should register usingcontainer.RegisterType<WcfServiceClient, IWcfServiceClient>(lifetimeManager);. Then get rid of everything else. - Aron