【问题标题】:Endpoint not found WCF未找到端点 WCF
【发布时间】:2012-06-07 10:42:12
【问题描述】:

我有自己托管的 WCF 库,登录功能运行良好,但第二个功能 ReturnCounter

界面是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace PMAService
{
    [ServiceContract]
    public interface IPMA
    {
        [OperationContract]
        string Login(string username, string password);

        [OperationContract]
        List<usp_ReturnEncounter_Result> ReturnEncounter();



    }
}

代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;
using System.Security.Cryptography;
using System.Web.Security;

namespace PMAService
{
    public class PMA : IPMA
    {

        [WebInvoke(Method = "GET",
   ResponseFormat = WebMessageFormat.Json,
   UriTemplate = "LogIn/{username}/{password}")]
        public string Login(string username, string password)
        {
            if (Membership.ValidateUser(username, password))
                return "true";
            else
                return "false";
        }

        // Method to retrieve the Counter 
        [WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "ReturnEncounter")]
        public List<usp_ReturnEncounter_Result> ReturnEncounter()
        {
            using (PMAEntities context = new PMAEntities())
            {
              return   context.usp_ReturnEncounter().ToList();
            }
        }

    }
}

我连接到实体框架的地方

web.config 看起来像

<?xml version="1.0"?>
<configuration>
  <appSettings/>
  <connectionStrings>
</connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <roleManager enabled="true" />
    <membership>
      <providers>
        <remove name="AspNetSqlMembershipProvider"/>
        <add name="AspNetSqlMembershipProvider"
             type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
             connectionStringName="Login"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             applicationName="/"
             requiresUniqueEmail="false"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="1"
             minRequiredNonalphanumericCharacters="0"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression="" />
      </providers>
    </membership>

    <authentication mode="Windows"/>
    <customErrors mode="On"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="PMAService.PMA">
        <endpoint binding="webHttpBinding" contract="PMAService.IPMA" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

login/x/y 运行良好,而 ReturnCounter 给出错误端点未找到

有什么办法解决这个问题

【问题讨论】:

  • 当您尝试调用 ReturnCounter Web 方法时返回的错误/状态代码是什么?是 Http 400 还是 500

标签: json wcf entity-framework c#-4.0


【解决方案1】:

首先在你的服务上启用Tracing,看看异常的原因是什么。

您还可以考虑增加服务器端和客户端的 ReaderQuotas,以便传递更大的数据而不会出现任何问题。示例如下:

<system.serviceModel>    
<bindings>
<webHttpBinding>
          <binding maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
             <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                  maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                  maxNameTableCharCount="2147483647" />
            <security mode="None" />
          </binding>
        </webHttpBinding>
</bindings>
</system.serviceModel>   
 

我还在您的代码中看到您正在直接传递实体框架获取的对象。在某些情况下,实体框架对象不会反序列化并可能导致异常。创建一个简单的 POCO,然后填充获取的数据并返回 POCO。

【讨论】:

  • 除了 POCO,我建议将列表作为 IList 或数组返回。
  • 追踪没有给出任何东西
  • @AMH :您是否已在服务器上包含配置项以启动跟踪?还有什么是返回的 HTTP 状态码
  • HTTP 返回状态代码,例如 200 - OK、400 - Bad Request、500 - Internal Server Error 等...因此,当您调用您收到的状态代码之一的方法时回应
  • 我没有收到任何代码,我也将代码更改为使用 POCO,但找不到相同的端点。
【解决方案2】:

为什么选择 WebInvoke?

要使用 Get 操作,您需要为此方法使用 WebGet。

WebInvoke 仅用于执行插入更新删除操作。 我们为它们使用 POST、PUT 和 DELETE 方法名称。(有序)

当你需要获取一些数据时,你应该这样做,

  [WebGet(UriTemplate = "ReturnEncounter",
 RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

您注意到有一种请求格式,这可能是 WebMessageFormat 上枚举的 XML 或 JSON。

用于后期操作。你可以使用 WebRequest 对象。

希望有所帮助。

【讨论】:

    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多