0

In have a WinForms application which, recently, has started to show a design-time error when opening some forms:

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

You can see the winforms designer screenshot here.

It's a well-documented problem (here, here and here for example), except that my problem has only appeared since I have tried to introduce dependency injection.

I have a business layer object calling this webservice. I have changed some methods like such:

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

When I remove my dependency injection for this service, it suddenly all works:

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

My webservice is configured like this (and yes, it's both in the business layer project and the main project):

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

What is the problem I introduced with my injection?

Community
  • 1
  • 1
thomasb
  • 5,816
  • 10
  • 57
  • 92
  • I get the impression that you are using your `Stocks` class somewhat like a singleton...and that it is initialized during application boot-strap. If so, try to find a later point in the app boot-strap lifecycle to create it. – Brent Arias Jun 26 '15 at 15:32
  • What? No! Why would I use them like a singleton? I instantiate a new `Stock` whenever I need its features. I also inject it into other components. – thomasb Jun 26 '15 at 15:40

1 Answers1

0

I haven't found a way to successfully fix this particular problem (except by catching this particular exception in my constructors and checking if it's design time).
I assume Visual Studio design-time compiles at a separate location, and does not copy the app.config files.

However, I have found that I am doing user control dependency injection wrong (see: here and here)

So, for my specific buggy control, I have replaced my constructor injection with a property injection (actually through a method, but hey).

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

And then I call LoadControl in the Form_Load event of the container forms.

Community
  • 1
  • 1
thomasb
  • 5,816
  • 10
  • 57
  • 92