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

时间:2015-06-26 15:24:33

标签: wcf dependency-injection

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

  

找不到引用合同的默认端点元素...

您可以看到winforms设计师截图here

这是一个记录良好的问题(例如hereherehere),但我的问题只是在我尝试引入依赖注入时才出现。

我有一个调用此Web服务的业务层对象。我改变了一些类似的方法:

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();
        }
    }
}

我的网络服务配置如下(是的,它都在业务层项目和主项目中):

<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" />

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

1 个答案:

答案 0 :(得分:0)

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

但是,我发现我的用户控件依赖注入错误(请参阅:herehere

所以,对于我的特定错误控件,我已经用属性注入替换了我的构造函数注入(实际上是通过一个方法,但是嘿)。

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();
}

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