【问题标题】:Could not find default endpoint element that references contract 'SumServiceReference.IService1' in the ServiceModel client configuration section在 ServiceModel 客户端配置部分中找不到引用合同“SumServiceReference.IService1”的默认端点元素
【发布时间】:2014-10-11 00:31:09
【问题描述】:

我想从后面的代码和 ajax 调用服务。我可以使用 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.

【问题讨论】:

    标签: wcf


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-08
      • 2011-11-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2011-01-12
      相关资源
      最近更新 更多