0

I have a simple WCF web service that works great if I create a new simple C# WPF application, adding the WCF Service Reference. Then I can access the methods/operationscontracts from the WCF service.

Eg:

Service1Client svc = new Service1Client();
        svc.GetData()

If I create a WPF UserControl where I add the WCF service reference and instanciate with: Service1Client svc = new Service1Client(); - everything compiles fine Again (can't debug since it's just a UserControl).

But if I then import the .dll from the UserControl to a new C# WPF application and use the UserControl in the MainWindow.xaml it fails:

An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll but was not handled in user code Additional information: Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

How is that? Why can't I use the WCF Service Reference in a WPF UserControl that gets used in another WPF Mainapplication?

I have searched and searched for this but nothing helpful found.

Best regards

EDIT #1

I added the System.ServiceModel to my MainApplication (Thank you for pointing that out) I get the same error

An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll but was not handled in user code Additional information: Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

I guess the problem is that the MainApplication don't know the endpoint, from the app.config from the UserControl Library.

I have to copy this part from the app.config (UserControl Library) to the MainApplication App.config The it will compile and start up and it works.

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="DELETED" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

But I still get Error in Visual Studio i live design view in MainWindow.xaml

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfControlLibrary1="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1" x:Class="WpfApplication1.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <WpfControlLibrary1:UserControl1/>
</Grid>

I get Error on the and can't see the UserControl in Live view (Cannot create an instance of "UserControl1")

And from the Error list:

Error   1   Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.    DELETED\WpfApplication1\WpfApplication1\MainWindow.xaml 7   9   WpfApplication1
user1281991
  • 763
  • 1
  • 12
  • 34
  • 1
    I guess that user control is being instantiated inside of devenv.exe which means that none of your config files comes into play. – usr Jul 25 '14 at 14:33
  • Hi usr, anyway I could alter Visual Studio to atleast display the UserControl and not just a bit red Error-cross? It runs fine and gets data fine from WCF service. Only design view that does not Work. But that is still very irritating. – user1281991 Jul 25 '14 at 14:47

3 Answers3

0

Based on the error you get, you need to reference the System.ServiceModel namespace from your main WPF application.

UPDATE:

For the second error you get, this is probably due to the VS designer.

Savvas Kleanthous
  • 2,695
  • 17
  • 18
  • 1
    If the application compiles and runs OK, it may be an issue with the VS designer. – Savvas Kleanthous Jul 25 '14 at 14:28
  • Everything works, it is only in design view I can't see my UserControl. Seems this was documented here: http://stackoverflow.com/questions/699644/wcf-error-could-not-find-default-endpoint-element-that-references-contract-us - I did the same thing and it would then compile and run, but my design view is still screwed :( – user1281991 Jul 25 '14 at 14:32
0

Funny, the output from svcutil.exe is

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://REMOED/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

The App.config from the UserControl is:

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://REMOVED/Service1.svc" binding="basicHttpBinding"
      bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
      name="BasicHttpBinding_IService1" />
</client>

Notice the "contract".

But if I copy/paste the config from svcutil I get Error in VS. Same as my first. So it seems that the right contract is contract="ServiceReference1.IService1" - but funny that svcutil outputs something different?

user1281991
  • 763
  • 1
  • 12
  • 34
0

Got it!!

Updated the app.config in my USERCONTROL with the output from svcutil and compiled it Again. Now design view works :)

user1281991
  • 763
  • 1
  • 12
  • 34