找不到引用合同的默认端点元素' SumServiceReference.IService1'在ServiceModel客户端配置部分中

时间:2014-08-18 06:31:26

标签: wcf

我想从两个代码后面调用一个服务&阿贾克斯。我可以使用ajax从客户端脚本调用它,但是从后面的代码调用它时会出现异常。

这是我的示例代码,它是一个简单的求和操作,用于演示目的。

服务合同:

using System.ServiceModel;
using System.ServiceModel.Web;

namespace Sum_WcfService
{
[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    int AddNums(int Num1, int Num2);
}
}

服务实施:

using System.ServiceModel.Activation;

namespace Sum_WcfService
{
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public int AddNums(int Num1, int Num2)
    {
        return Num1 + Num2;
    }
}
}

在同一解决方案中添加了一个客户端应用以使用我的服务 并添加名为“SumServiceReference”的服务引用

客户申请:

using System;
using System.Globalization;
using Sum_WcfService.SumServiceReference;

namespace Sum_WcfService
{
public partial class AddServiceClient : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GetSum(object sender, EventArgs e)
    {
        var ServiceProxy = new Service1Client();
        int Num1,Num2;
        int.TryParse(txtNum1.Value, out Num1);
        int.TryParse(txtNum1.Value, out Num2);
        var Sum = ServiceProxy.AddNums(Num1, Num2);
        txtResult.Value = Sum.ToString(CultureInfo.InvariantCulture);
    }
}
}

我的配置文件是:

<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>

<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<behaviors>
  <serviceBehaviors>
    <behavior name ="SumServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>

  <endpointBehaviors>
    <behavior name="EndPointBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
</behaviors>

<services>
  <service name="Sum_WcfService.Service1" behaviorConfiguration="SumServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="Sum_WcfService.IService1"       behaviorConfiguration="EndPointBehavior" />
  </service>
</services>

<client>
  <endpoint address="http://localhost/Sum_Wcf/Service1.svc"
   binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
   contract="Sum_WcfService.SumServiceReference.IService1" name="BasicHttpBinding_IService1" />
</client>

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" openTimeout="00:01:00"
              receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
              bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
              maxBufferPoolSize="524288" maxBufferSize="65536" maxReceivedMessageSize="65536"
              textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"
              messageEncoding="Text">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
       maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    </binding>

  </basicHttpBinding>
</bindings>
</system.serviceModel>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>

</configuration>

我得到的错误是

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

1 个答案:

答案 0 :(得分:1)

将服务元素更改为:

<service name="Sum_WcfService.Service1" behaviorConfiguration="SumServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="Sum_WcfService.IService1" behaviorConfiguration="EndPointBehavior" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>

,客户端元素为:

<client>
<endpoint address="http://localhost/Sum_Wcf/Service1.svc" binding="webHttpBinding"
behaviorConfiguration="EndPointBehavior" contract="SumServiceReference.IService1" name="WebHttpBinding_Sum" />
</client>