进行依赖项注入时找不到 WCF 引用

WCF reference not found when doing dependency injection

我有一个 WinForms 应用程序,最近在打开某些表单时开始显示设计时错误:

Could not find default endpoint element that references contract ...

可以看到winforms designer截图here.

这是一个有据可查的问题(例如here, here and here),除了我的问题只是在我尝试引入依赖注入后才出现。

我有一个调用此网络服务的业务层对象。我已经改变了一些这样的方法:

public class Stocks : BusinessObjectBase
{
    private readonly IAxReferenceService axService;
    private readonly IDALStock dalstock;

    public Stocks()
    {
        this.axService = new AxReferenceServiceClient();
        this.dalstock = new DALStock();
    }

    public Stocks(IAxReferenceService axService, IDALStock dalStock)
    {
        this.axService = axService;
        this.dalstock = dalStock;
    }

    public IEnumerable<Model.Stock> GetStock() {
        return this.axService.GetStock();
    }
}

当我删除此服务的依赖项注入时,突然一切正常:

public class Stocks : BusinessObjectBase
{
    private readonly IDALStock dalstock;

    public Stocks()
    {
        this.dalstock = new DALStock();
    }

    public Stocks(IDALStock dalStock)
    {
        this.dalstock = dalStock;
    }

    public IEnumerable<Model.Stock> GetStock() {
        using (var axService = new AxReferenceServiceClient()) {
            return axService.GetStock();
        }
    }
}

我的webservice是这样配置的(是的,它在业务层项目和主项目中):

<binding name="BasicHttpBinding_IAxReferenceService" 
         maxBufferSize="2147483647"
         maxBufferPoolSize="2147483647"
         maxReceivedMessageSize="2147483647" >
  <readerQuotas maxDepth="32"
                maxStringContentLength="2147483647"
                maxArrayLength="2147483647"
                maxBytesPerRead="2147483647"
                maxNameTableCharCount="2147483647" />
</binding>
...
<endpoint address="http://.../AxReferenceService.svc"
          binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_IAxReferenceService"
          contract="AX.IAxReferenceService"
          name="BasicHttpBinding_IAxReferenceService" />

我注射时引入的问题是什么?

我还没有找到成功解决这个特定问题的方法(除了在我的构造函数中捕获这个特定异常并检查它是否是设计时间)。
我假设 Visual Studio 设计时在单独的位置编译,并且不复制 app.config 文件。

但是,我发现我做的用户控件依赖注入是错误的(参见:here and here

因此,对于我的特定 buggy 控制,我用 属性 注入替换了我的构造函数注入(实际上是通过一种方法,但是嘿)。

public ucSelectStock()
{
    InitializeComponent();
}

public void LoadControl(
    IStocks stocks,
    IStockFactory factory)
{
    this.stocksBll = stocks;
    this.stockFactory = factory;

    // these were creating many problems because they're badly designed.
    // moving them from constructor to method fixes the design-time problems
    this.SelectorProduct = new GridCheckMarksProductSelection();
    this.SelectorSupport = new GridCheckMarksSupportSelection();
}

然后我在容器表单的 Form_Load 事件中调用 LoadControl