使用unity注入WCF代理客户端
Injection WCF proxy client using unity
我有一个 asp.net mvc 应用程序,需要在其中调用 wcf 服务。我已经为该项目添加了服务参考。我使用 unity 进行依赖注入,想注入 wcf 客户端。但是因为System.ServiceModel.ClientBase,事情就不那么简单了。我是这样做的。
这是一个自动生成的代理:
public partial class WcfServiceClient :
System.ServiceModel.ClientBase<WcfUnity.IWcfServiceClient>,
WcfUnity.IWcfServiceClient
{
//autogenerated constructors and methods
}
我创建了接口:
public interface ISimpleProxy : WcfUnity.IWcfServiceClient
{
//duplication of methods' signatures
void CloseConnection();
}
我使用部分 class 扩展了 WcfServiceClient:
public partial class WcfServiceClient : ISimpleProxy
{
public void CloseConnection()
{
try
{
Close();
}
catch (Exception)
{
Abort();
}
}
}
然后像这样注入:
container.RegisterType<ISimpleProxy>(new InjectionFactory(c => new WcfServiceClient()));
所以我没有来自 ClientBase 的依赖项,classes 中也没有使用此 wcf 代理的 wcf 内容。此解决方案是否有一些缺点?
不,不是,我使用(大部分)相同的方法。
我建议您让您的界面继承自 IDisposable
,因为底层 ClientBase<T>
是一次性的。这样你就不需要 CloseConnection
.
我有一个 asp.net mvc 应用程序,需要在其中调用 wcf 服务。我已经为该项目添加了服务参考。我使用 unity 进行依赖注入,想注入 wcf 客户端。但是因为System.ServiceModel.ClientBase,事情就不那么简单了。我是这样做的。
这是一个自动生成的代理:
public partial class WcfServiceClient :
System.ServiceModel.ClientBase<WcfUnity.IWcfServiceClient>,
WcfUnity.IWcfServiceClient
{
//autogenerated constructors and methods
}
我创建了接口:
public interface ISimpleProxy : WcfUnity.IWcfServiceClient
{
//duplication of methods' signatures
void CloseConnection();
}
我使用部分 class 扩展了 WcfServiceClient:
public partial class WcfServiceClient : ISimpleProxy
{
public void CloseConnection()
{
try
{
Close();
}
catch (Exception)
{
Abort();
}
}
}
然后像这样注入:
container.RegisterType<ISimpleProxy>(new InjectionFactory(c => new WcfServiceClient()));
所以我没有来自 ClientBase 的依赖项,classes 中也没有使用此 wcf 代理的 wcf 内容。此解决方案是否有一些缺点?
不,不是,我使用(大部分)相同的方法。
我建议您让您的界面继承自 IDisposable
,因为底层 ClientBase<T>
是一次性的。这样你就不需要 CloseConnection
.