终端点未找到 - WCF Web服务

14

我已经为我的WCF服务创建了2个端点。

使用basicHttpBinding工作良好,但对于webHttpBinding会导致错误。

错误=找不到端点。

操作契约定义

[OperationContract]
[WebInvoke(Method = "POST",
           BodyStyle = WebMessageBodyStyle.WrappedRequest,
           ResponseFormat = WebMessageFormat.Json)]
VINDescription CallADSWebMethod(string vin, string styleID);

web.config:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="Description7aBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
            <message clientCredentialType="UserName" algorithmSuite="Default"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://services.chromedata.com:80/Description/7a"
                binding="basicHttpBinding"
                bindingConfiguration="Description7aBinding"
                contract="description7a.Description7aPortType"
                name="Description7aPort"/>
    </client>
    <services>
      <service behaviorConfiguration="asmx" name="ADSChromeVINDecoder.Service">
        <endpoint name="httpEndPoint" 
                  address="" 
                  binding="basicHttpBinding"
                  contract="ADSChromeVINDecoder.IService"/>
        <endpoint name="webEndPoint"
                  address="json"
                  behaviorConfiguration="web"
                  binding="webHttpBinding"
                  contract="ADSChromeVINDecoder.IService"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="asmx">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

请给我建议,如何解决这个问题?


1
你是如何收到“找不到终端”错误的?发送请求到哪个地址了? - carlosfigueira
在浏览器中访问 http://localhost:55410/Service.svc/json 时 - James
请告诉我,如果我还需要添加其他代码。 - James
ADSChromeVINDecoder = 解决方案名称 - James
json/help = 这也会出现相同的“未找到端点”错误。 - James
显示剩余9条评论
1个回答

13

我已经根据这个创建了一个与你的类似的服务:

 [ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(UriTemplate="/CallADSWebMethod", Method="POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
    string CallADSWebMethod(string vin, string styleID);
}

我添加的重要部分是UriTemplate,它告诉服务调用应该是什么样子的。然后我将这个服务实现为:

public class Service : IService
{
    public string CallADSWebMethod(string vin, string styleID)
    {
        return vin + styleID;
    }
}

而且在我的web.config文件中我有以下内容:

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="Description7aBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm=""/>
        <message clientCredentialType="UserName" algorithmSuite="Default"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="asmx" name="WebApplication1.Service">
    <endpoint address="basic" binding="basicHttpBinding" name="httpEndPoint" contract="WebApplication1.IService"/>
    <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="webBehavior" name="webEndPoint" contract="WebApplication1.IService"/>
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
</services>
<behaviors>
    <endpointBehaviors>
        <behavior name="webBehavior">
            <webHttp />
        </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
    <behavior name="asmx">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

我创建了一个简单的页面,它看起来像这样,并使用jQuery调用了此服务:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Ok").click(function () {
                var jData = {};
                jData.vin = "one";
                jData.styleID = "test";
                $.ajax({
                    type: "POST",
                    url: "/Service.svc/json/CallADSWebMethod",
                    data: JSON.stringify(jData),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        alert(msg);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" id="Ok" name="Ok" value="Ok" />
    </div>
    </form>
</body>
</html>

这将产生一个带有“onetest”文本的警报。希望这能提供一些指导。


  1. UriTemplate仅将URI或一组URI映射到服务操作,即服务如何被调用。
  2. 此绑定仅添加了访问WSDL的可能性。
  3. 这是一个函数,用于将对象转换为适用于ajax调用的json字符串,只是一个快捷方式,您可以自己完成此操作,但这很方便。
- Johan Lundqvist
目前它运行良好。无论如何使用JSON.parse有什么好处吗? - James
现在一切都正常了。但突然间我遇到了一个错误 - JSON.stringify() -- 因为JSON未定义,这只出现在IE的所有版本中...你能给我解释一下吗? - James
它支持IE > 7,参见http://msdn.microsoft.com/en-us/library/cc836459%28VS.85%29.aspx - Johan Lundqvist
我在IE8和9中进行了测试,但错误仍然存在。所以我做的是,我自己将字符串制作成JSON格式,而不使用该函数。这样我就避免了错误。 - James
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接