views:

41

answers:

1

Hi,

I'm getting the following error in my WCF project:

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll

Additional information: Could not find default endpoint element that references contract 'IPhiFeed' 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."

The WCF project is a bit experimental: is a mixture of managed and unmanaged C++, and C#. I've got everything working in pure C#, but I have to consume WCF from unmanaged C++, thus the need to write a C++ wrapper around WCF.

Update

As requested, here is the code thats throws the exception:

// WCF library written in C#
public class EngineAPI : IEngineAPI
{
  public FeedClient client;
  // constructor
  public EngineAPI()
  {
    // the line below in this C# library works *perfectly* when called from a C# 
    // console app, but it fails when a C++ console app calls the same C# library
    // UPDATE: exception fixed if you copy app.config to out.exe.config, see comments below
    client = new FeedClient(); // << exception here

  }
  .....
}
// NOTE: the line "client = new FeedClient" instantiates generatedProxy.cs,
// which is generated with svcutil
// NOTE: if I temporarily delete "app.config" from the pure C# project, it generates *exactly* the same error as I'm getting when I attempt to call everything from the separate C++ project with managed code.

Update

Found the problem, it was unrelated to the code: you have to copy app.config to out.exe.config. I now have a 100% working C++/CLI project calling a C# library which uses WCF. See my comments below.

+1  A: 

Typically when you are using wcf in the client project you have entries in your config file that describe the binding to be used as well as the endpoint where the service is located:

I am presuming that your FeedClient class is the class that inherits from ClientBase(IPhiFeed). ClientBase actually defines numerous constructors, if you call the constructor with no parameters it will attempt to find the 'default' client endpoint in your configuration file; and in this case there isn't one because the only endpoint defined in the configuration has a name. To correct this you can do one of two things: you could call the ClientBase constructor that takes a string parameter with the endpoint name: ClientBase<IPhiFeed>("MyService") or you could change the configuration file so that the endpoint does not have a name:

Edits: The code you have in your question looks like C# code. Does the FeedClient class inherit from ClientBase? If yes and the C# code that calls it works then what is the C++ code that doesn't work? In general in C++ code you can have both managed and unmanaged code; that is unmanaged code can call into managed code. In your unmanaged project you should do the following: go to the property page for the project, Click Configuration Properties the General; in the general tab under Project Defaults the second from the bottom option should be Common Language Runtime Support, make sure it is set to Common Language Runtime Support (/clr). Add the project that contains the FeedClient class in as a reference to the unmanaged project if necessary. Then you should be able to instantiate the FeedClient class directly in unmanaged code:

MyNamespace::FeedClient wcfClient;
wcfClient.SomeMethod()  // Add parameters as appropriate...
Steve Ellinger
It sounds like you know WCF inside out. Right now, the C# project is still working flawlessly, while the C++/CLI is still throwing the same error as before. However, if I remove the endpoint name from app.config, the C# project stops working, with the error "Could not find default endpoint element that references contract 'IPhiFeed' in the ServiceModel client configuration section.". Do you know the C++/CLI code that I could use to test the instantiation of the IPhiFeed classs directly from the C++/CLI project?
Gravitas
See the edits in my answer
Steve Ellinger
I discovered the problem, and now everything works perfectly: my executable was "out.exe", and I needed to copy "app.config" to "out.exe.config". C# does this automatically if you add "app.config" to your project, but a C++ project does not do this automatically. Now I have a 100% working C++/CLI project referencing a C# library which calls WCF. Thanks Steve for your generous help - really appreciate it.
Gravitas