Orion
Orion

Reputation: 462

C# Could not find default endpoint element that references contract

I am try for 2 weeks to get this working and still nothing :(

This is the WCF Url > http://api.proactiveclothing.com/services/Product.svc

I've added it as a Service Reference in Visual Studio.

When I access the code as below.

        ServiceReference1.ProductDataServiceClient f = new ServiceReference1.ProductDataServiceClient();

        ServiceReference1.GetProductSellableRequest req = new ServiceReference1.GetProductSellableRequest();
        req.password = "xxxxx";
        req.productId = "148";
        ServiceReference1.GetProductSellableResponse resp = f.getProductSellable(req);

I get the following Error on the last line.

System.ServiceModel.FaultException`1 was unhandled by user code
  HResult=-2146233087
  Message=Could not find default endpoint element that references contract 'ProductDataService' 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.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
       at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at ServiceReference1.ProductDataService.getProductSellable(getProductSellableRequest1 request)
       at ServiceReference1.ProductDataServiceClient.ServiceReference1.ProductDataService.getProductSellable(getProductSellableRequest1 request) in c:\Users\laptop\AppData\Local\Temp\Temporary ASP.NET Files\vs\45e2a7e3\d169f135\App_WebReferences.qunwmnb1.0.cs:line 3522
       at ServiceReference1.ProductDataServiceClient.getProductSellable(GetProductSellableRequest GetProductSellableRequest) in c:\Users\laptop\AppData\Local\Temp\Temporary ASP.NET Files\vs\45e2a7e3\d169f135\App_WebReferences.qunwmnb1.0.cs:line 3528
       at _Default.Page_Load(Object sender, EventArgs e) in d:\Websites\5. Stand Alone Applications\WebSite1\Default.aspx.cs:line 20
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

Here is my Web.Config file

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ProductDataServiceBinding"/>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://api.proactiveclothing.com/services/Product.svc" binding="basicHttpBinding" bindingConfiguration="ProductDataServiceBinding" contract="ServiceReference1.ProductDataService" name="ProductDataService"/>
    </client>
  </system.serviceModel>
</configuration>

Please can somebody try adding it to a test project and see if you can get it working on your side?

I generated the data and service contracts in a new WCF project which is uploaded to our web server. When I try to access the WCF project from my local web app, it bombs out as described above.

EDIT - Below is information on the WCF Service

system.servicemodel in web.config

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>

    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBindingConfig">
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>


    <services>
      <!-- Note: the service name must match the configuration name for the service implementation. -->
      <service name="ProductDataService">
        <!-- Add the following endpoint.  -->
        <!-- Note: your service must have an http base address to add this endpoint. -->
        <endpoint
            address="http://api.proactiveclothing.com/services/Product.svc"
            binding="basicHttpBinding"
            bindingConfiguration="basicHttpBindingConfig"
            contract="ProductDataService" />
        <endpoint address="mex"
            binding="mexHttpsBinding"
            contract="IMetadataExchange" />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" externalMetadataLocation="http://api.proactiveclothing.com/wsdl/ProductDataService.wsdl"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Service and Data Contracts generated from the wsdl and xsd files Note: only adding part of the file as it's too large

namespace Proactive.Product
{


    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace = "http://api.proactiveclothing.com/WSDL/ProductDataService/v1/", ConfigurationName = "ProductDataService")]
    public interface ProductDataService
    {
    }
}

WCF Product.svc class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using Proactive.Product;
using System.Collections;

namespace Proactive_WebAPI.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Product" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Product.svc or Product.svc.cs at the Solution Explorer and start debugging.
    public class Product : Proactive.Product.ProductDataServiceClient
    {
        public GetProductSellableResponse getProductSellable(GetProductSellableRequest _GetProductSellableRequest)
        {
            //check passsowrd is valid

            if (_GetProductSellableRequest.password != "xxxx")
                return null;

            //if valid, proceed

            GetProductSellableResponse rep = new GetProductSellableResponse();

            //try
            //{
            //    List<Proactive_WebAPI.Models.Product> products = ProductDB.GetSellableProducts();

            //    GetProductSellableResponseProductSellable[] p = new GetProductSellableResponseProductSellable[products.Count];

            //    int i = 0;
            //    foreach (Proactive_WebAPI.Models.Product _prod in products)
            //    {
            //        p[i].productId = _prod.Id.ToString();
            //        rep.ProductSellableArray = p;
            //        i++;
            //    }
            //}
            //catch
            //{
            //    rep.ErrorMessage.code = 999;
            //    rep.ErrorMessage.description = "General Error – Contact the System Service Provider";
            //}

            rep.ErrorMessage.code = 999;
            rep.ErrorMessage.description = "General Error – Contact the System Service Provider";

            return rep;
        }
   }
}

Upvotes: 1

Views: 7756

Answers (2)

Orion
Orion

Reputation: 462

We got it working with all credit to StfBln who helped me in chat.

Firstly I had to make sure the svc class was inheriting from the service and not the client like below line

public class Product : Proactive.Product.ProductDataService

Also cleaned up the generated code and changed this

 [System.ServiceModel.ServiceContractAttribute(Namespace = "http://api.proactiveclothing.com/WSDL/ProductDataService/v1/", ConfigurationName = "ProductDataService")]

to this

[System.ServiceModel.ServiceContractAttribute(Namespace = "http://api.proactiveclothing.com/WSDL/ProductDataService/v1/")]

Also make sure the OperationContractAttribute only has an Action like below line

[System.ServiceModel.OperationContractAttribute(Action = "getProduct")]

Use the following line of code to make sure you are generating the correct service contracts, take note of the /servicecontract command

svcutil.exe yourdomain.com/wsdl/ProductDataService.wsdl /language:C# /out:"C:\Users\IEG-WS02\Desktop\WSDL\IService2.cs" /t:code /serviceContract

Upvotes: 2

Bruno Lopes
Bruno Lopes

Reputation: 41

Try generating the reference with svcutil tool. If still not working, you can try to delete all phisical reference files and add it again, you know this kind of weird stuffs just happens.

Upvotes: 1

Related Questions