0
votes

I am working on a SharePoint 2013 webpart and a label on the webpart needs to communicate with a SQL Server data table using WCF. I have created the WCF interface and main class, and have also invoked the service in my Visual webpart like this:

protected void Page_Load(object sender, EventArgs e)
    {
        WcfServiceReference1.Service1Client client = new WcfServiceReference1.Service1Client();
        CustomerNameLbl.Text = client.GetCustomerName(ProjectIDDescLbl.Text);
    }

Where WcfServiceReference1 is the added WCF service reference and a customer label text is being changed depending on the project number label. The project builds and deploys fine but when I add the webpart, I get this error: Could not find default endpoint element that references contract 'WcfServiceReference1.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.

My app.config file is this:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://as-sv-dev02:2345/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="WcfServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>
</system.serviceModel>

And web.config file (for SharePoint) is this:

<system.serviceModel>
<bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://as-sv-dev02:2345/Service1.svc" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_IService1" contract="WcfServiceReference1.IService1"
            name="BasicHttpBinding_IService1" />
    </client>

Can anyone guide me on how to resolve this? Am I going wrong somewhere?

3
What version of .NET are you using? Also, why do you have a client endpoint in your app.config (which I assume is for your service)?Tim
I'm using .NET 4.5 (with Visual Studio 2012), and I have that endpoint in there after googling quite a lot, is that the cause of the problem though?Sharadha Jayaraman
Nope, just not needed unless the service is also calling another service (and acting as a client in that case). Since you're in 4.5, you'll have a default endpoint for the service which will be the address of wherever the Service1.svc file is. If you go to http://as-sv-dev02:2345/Service1.svc in a browser, what do you see?Tim
The reason for this exception is your configuration file (app.config / web.config) containing the <system.ServiceModel> is not picked up. If I understand your problem correctly, your service is hosted somewhere else and your web part is the client of your WCF service which is failing now? You should make sure your webpart / where ever your WCF client code side code is running is having this config. That should solve your problem. I'm seeing you have two config files? Why 2 config files?Praburaj
@Praburaj - One of the configs is for their Sharepoint app; the other is for (I'm guessing) the service itself.Tim

3 Answers

1
votes

After a lot of research, I found out that there was some problem with SharePoint which I don't really understand. Thankfully, in time, I chose not to go the SharePoint way and instead work with a simple .aspx page and that would resolve the issue. Just unhappy with Microsoft not offering as much support on such common issues.

1
votes

I had the same problem and I found this link.

http://social.technet.microsoft.com/Forums/en-US/dea0763d-d6ea-4659-8ef0-8275514d066a/sp2010-consuming-web-service-in-visual-web-part?forum=sharepointdevelopmentprevious

I did it and worked fine. The problem is the deploy does not copy the service model configuration from the Visual Studio' app.config to the IIS's web.config.

I hope helps.

Best Regards.

0
votes

Your client endpoint does not seem to have a default configuration (name="" or nameless). Try using:

WcfServiceReference1.Service1Client client = new WcfServiceReference1.Service1Client("BasicHttpBinding_IService1");