Friday, January 13

Could not find endpoint element with name... error when using a web part to connect to a WCf service

This is actually a more general WCf problem, but I decided to put it in the SharePoint section because it is more of a hidden problem in SarePoint development.

Typically, when you create a BizTalk WCF service and host it in IIS. Using it is as simple as consuming the service wsdl from your calling client via a service reference which will create entries in an app.config (or web.config) file corresponding to the needed / expected bindings, generating a proxy and using the service.

However, when you create a SharePoint web part for inclusion in a web page hosted in SharePoint server 2010 which in turn is used by to call the WCF service (in my case the WCf service was a BizTalk 2010 WCF service) you might get an error of the following type: Could not find endpoint element with name 'xxxx_xxx' and contract 'xxxx.xxxxx' 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 name could be found in the client element

A long search on google actually returned a few potential )and sensible) solutions, none of which solved my problem.

  1. One solution is suggested here http://www.roelvanlisdonk.nl/?p=735 which suggests making sure the name in the contract attribute of the endpoint is the same as you’re Microsoft Visual Studio Service Reference name. Basically, when you add the service reference to the WCF service in Visual Studio, make sure the first part of the 'contract name' in your app.config endpoint element configuration matches the service reference name specified
  2. Another solution is suggested here http://www.vistax64.com/indigo/32159-location-name-config-file.html which suggests making sure that the fully qualified names are used in the app.config file for the contract attribute in the endpoint element. Meaning if the namespcae of your client applicaiton is smarties.mydev.callingclients, make sure the part of the endpoint element which spoecifies the contract is like: contract="smarties.mydev.callingclients.ServiceReference.Nameofcontractinservice"
  3. Another solution here http://social.msdn.microsoft.com/forums/en-US/biztalkgeneral/thread/071cb8bd-4891-4e42-8f63-f18f74f47f3f/ suggests ensuring that your app.config informaiton is correctly copied into your application.exe.config (or in my case application.dll.config) file (where application refers to the name of your client application). This will usually be located in the same locaiton to which your .exe or .dll file is built, and should be more or less identical to the app.config file
  4. The problem with my web part scenario was this: When you create a web part and add a service reference to it, it adds the configuraiton informaiton to the app.config file and (hopefully) the application.exe.config file. Unfortunately, when you ad the web part to a web page hosted in a SharePoint site, your client's app.config file is no longer used for the WCf call, as the hosting site's web.config file is the locaiton referenced for the configuration information. You need to copy your binding configuration information (<binding> element) and the endpoint confuguration information (<endpoint> element) into the relevant secitons of the <client> node within the System.Servicemodel section of the web.config file in the root location of the SharePoint site hosting the web page
After much thinking and brainstorming (since I am a BizTalk developer and not a SharePoint developer) and without much success searching on the Internet for my scenario, I solved the issue with the fourth step above

No comments:

Post a Comment