WCF Web 服务客户端没有端点侦听

WCF Web Service Client no endpoint listening

我正在尝试使用 JSON 创建 WCF Web 服务并在 ASP .NET 中与客户端一起使用 我的 WCF Web 服务器已启动并且 运行 托管在 IIS 上,我已检查浏览器,得到 JSON 响应。

这是服务器web.config文件:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <!--<compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>-->
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfServiceApp.Service1">
        <endpoint address="../Service1.svc" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior >
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- 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="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
      </customHeaders>
    </httpProtocol>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

之后,我在 ASP.NET 中创建了一个 Web 应用程序客户端来使用 WCF Web 服务。 我在客户端添加了 WCF web 服务引用,但是

Visual Studio 2012 not updating client web.config

。 我在 Whosebug 中为我的客户 web.config

找到了一些东西

客户端web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <!--<compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>-->
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webby">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="http://localhost/WCFService/Service1.svc" name="Service1" binding="webHttpBinding" 
                contract="ServiceReference1.IService1" behaviorConfiguration="webby"/>
    </client>
  </system.serviceModel>
</configuration>

calling Service from Client

protected void Button1_Click(object sender, EventArgs e)
        {
            string result;
            string input = tbInput.Text;
            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            try
            {
                result = client.GetData(input);
                lbResult.Text = result;
                client.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }

但是当我尝试从 Web 服务读取时,出现异常

There was no endpoint listening at http://localhost/WCFService/Service1.svc/GetData that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

Inner Exception: The remote server returned an error: (404) Not Found."}

我怀疑它是我的 web.config 文件中的配置问题,但不确定是什么导致了这个问题。

谢谢, 阿肖克

我认为这不会总是配置问题:有一次我遇到了同样的问题但我忘记了 servicecontract 所以服务器上的服务是 运行 但无法访问它。 这里有一些检查点:

1) 找到您的 .svc 文件并右键单击它并在浏览器中选择 select 选项视图,这将为您提供准确的本地 URL.

2) 给你合约参数一个完整的值,例如而不是 Contract1 添加完整命名空间 x.y.Contract1

3) 检查您是否已将最新的 dll 放在 IIS 服务器上并重置了应用程序池

4) 检查OperationContract,ServiceContract标签设置是否正确

您正在为 service.So 使用 WebHttpBinding,基本上这是一项 REST 服务。所以你不能添加 Service Reference 因为 REST 服务不公开任何元数据,你需要通过 HTTP 动词查询资源,比如 GETPOST 等等。

试一试:

var req = (HttpWebRequest)WebRequest.Create("your endpoint");
var data_to_send = Encoding.ASCII.GetBytes("some data");
using (var _temp = req.GetRequestStream())
{
    _temp.Write(data_to_send, 0, data_to_send.Length);
}    

var res = req.GetResponse();

或者您可以选择:

var req = new HttpClient().PostAsync("your url", new StringContent(JsonConvert.SerializeObject("your params")));

同时在您的 endpointBehavior 中启用一些属性以捕获更具体的异常详细信息。

我详细了解了 webHttpBinding,REST 发现无法使用添加服务引用创建此类服务的客户端。

这里是调用 WCF 服务的示例代码:(我发现它很简单),响应是 JSON 格式,你需要以不同的方式提取它(到目前为止我不知道该怎么做)

感谢http://www.codeproject.com/Articles/275279/Developing-WCF-Restful-Services-with-GET-and-POST

string url = "http://localhost:50327/Service1.svc/data/"+input; 
                    string strResult = string.Empty;
                    // declare httpwebrequet wrt url defined above
                    HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
                    // set method as post
                    webrequest.Method = "GET";
                    // set content type
                    webrequest.ContentType = "application/json"; //x-www-form-urlencoded”;
                    // declare & read response from service
                    HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
                    // set utf8 encoding
                    Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                    // read response stream from response object
                    StreamReader loResponseStream = new StreamReader
                        (webresponse.GetResponseStream(), enc);
                    // read string from stream data
                    strResult = loResponseStream.ReadToEnd();
                    // close the stream object
                    loResponseStream.Close();
                    // close the response object
                    webresponse.Close();
                    // assign the final result to text box
                    result = strResult;
                    lbResult.Text = strResult;