我有一个wcf应用程序,需要在其中调用asp.net服务。我在项目中添加了服务引用。我将统一用于依赖注入,并希望注入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();
}我使用分部类扩展了WcfServiceClient:
public partial class WcfServiceClient : ISimpleProxy
{
public void CloseConnection()
{
try
{
Close();
}
catch (Exception)
{
Abort();
}
}
}然后像这样注射:
container.RegisterType<ISimpleProxy>(new InjectionFactory(c => new WcfServiceClient()));所以我不依赖于ClientBase,而且在使用这个wcf代理的类中也没有wcf内容。这个解决方案有什么缺点吗?
发布于 2015-09-30 15:59:14
不,它没有,我使用(大部分)相同的方法。
我建议您从IDisposable继承您的接口,因为底层ClientBase<T>是一次性的。这样你就不需要CloseConnection了。
https://stackoverflow.com/questions/32870610
复制相似问题