R – WCF Service reference from Silverlight Class Library

silverlightwcf

I have a Silverlight application communicating with the server side through WCF services. Initially I had everything in the main Silverlight application, but now I wanted to factor our some classes to a separate Silverlight Class Library project. This however gave me some odd issues…

I wanted to factor the classes that does the WCF communication out to a separate project. So I:

  • Created a new project; Silverlight class library
  • Moved the classes from my Silverlight application to my Silverlight class library
  • Removed the Service reference in the application as I no longer call it from the app.
  • Added a Service reference from the class library project.

Now – compiling is fine and I get intellisense for the service stuff in the Class Library, so it seems to be fine. I also updated the service and got the updates in the Class Library.
But when running the application it fails when doing a service call giving the following error:

InvalidOperationException was unhandled by user code

Could not find default endpoint element that references contract 'MyServiceReference.IMyService' 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.

Seems like it can't find the service, but why? I assume there should be no problem to have service references from a Silverlight Class Library as it allows me to add one?

Best Answer

IT can't find any service configuration - where do you have the config for the WCF service? By default, the client app (the EXE) will have a app.config that contains the service endpoints to connect to.

Also by default, class libraries (DLL's) don't have their own configuration but rely on their hosting app to provide the configuration for them.

So all in all - you're probably missing the config for the client endpoint. Most likely, it has been created as an app.config in the class library project, but that's not being used, really - you'll have to move the <system.serviceModel> section up to the main app's config (I'm not fluent in Silverlight, but you'll know where to put it).

Marc

Related Topic