【问题标题】:WCF Web Service Client no endpoint listeningWCF Web 服务客户端没有端点监听
【发布时间】:2015-10-17 05:03:27
【问题描述】:

我正在尝试使用 JSON 和使用 ASP .NET 中的客户端创建 WCF Web 服务 我的 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 未更新客户端 web.config

。 我在stackoverflow 中为我的客户端 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>

从客户端调用服务

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 服务读取时,出现异常

没有端点监听 http://localhost/WCFService/Service1.svc/GetData 可以接受 信息。这通常是由不正确的地址或 SOAP 操作引起的。 有关详细信息,请参阅 InnerException(如果存在)。

内部异常:远程服务器返回错误:(404) Not 找到了。”}

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

谢谢, 阿肖克

【问题讨论】:

  • 如果您将浏览器指向localhost/WCFService/Service1.svc,会发生什么?你得到漂亮的 WCF 状态页面了吗?
  • 是的,我可以在 JSON 中看到 localhost/WCFService/Service1.svc/data/ashok 的响应。
  • 如果您的问题有答案,如果您认为正确,请标记答案。

标签: c# asp.net json web-services wcf


【解决方案1】:

我阅读了有关 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;

【讨论】:

    【解决方案2】:

    我认为这并不总是配置问题:一旦我遇到同样的问题,但我忘记了servicecontract,因此服务正在服务器上运行但无法访问它。 这里有一些检查点:

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

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

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

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

    【讨论】:

    • 这不是答案。要发表评论/提出问题/澄清,请将 cmets 放在问题旁边的评论部分。
    • 伙计,我没有 50 分来放 cmets,我真的很想帮助你,这就是为什么否则我知道该怎么做。主要是以上几点解决了你的问题吗?
    【解决方案3】:

    您使用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,我只是从互联网上举了一个例子并尝试学习。我尝试将其更改为 basichttpBinding 但这会导致其他错误。
    • 也许您正在尝试构建REST wcf 服务的示例。试试这个解决方案。
    • 试试这个并标记为答案如果有效。你已经了解了REST服务。
    • 我试过第一个得到协议破坏异常,你能建议我对 httpbinding 进行更改吗?我想做一个像初学者一样简单的添加参考方式
    • 承认你是对的,我不能为 REST 添加参考,它仅适用于 SOAP。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    相关资源
    最近更新 更多