【问题标题】:WCF reference not found when doing dependency injection进行依赖注入时未找到 WCF 引用
【发布时间】:2015-09-13 15:04:55
【问题描述】:

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

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

我的注射引入了什么问题?

【问题讨论】:

  • 我的印象是你使用你的Stocks 类有点像一个单例......并且它是在应用程序引导期间初始化的。如果是这样,请尝试在应用引导生命周期中找到稍后的时间点来创建它。
  • 什么?不!为什么我会像单身一样使用它们?每当我需要它的功能时,我都会实例化一个新的Stock。我还将它注入到其他组件中。

标签: wcf dependency-injection


【解决方案1】:

我还没有找到成功解决这个特殊问题的方法(除了在我的构造函数中捕获这个特殊异常并检查它是否是设计时间)。
我假设 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();
}

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

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 2012-02-09
    • 2011-04-24
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    相关资源
    最近更新 更多