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

时间:2015-07-27 05:57:02

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

我正在尝试使用JSON创建一个WCF Web服务,并在ASP .NET中使用Consume with Client 我的WCF Web服务器已启动并运行在IIS上,我已使用浏览器检查,获得JSON响应。

这是Server 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服务。 我在Client中添加了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)不是   实测值。“}

我怀疑我的web.config文件中存在配置问题,但不确定导致此问题的原因。

谢谢, 阿肖克

3 个答案:

答案 0 :(得分:2)

您正在使用$file_to_attach="".$uploaddir.$_FILES['file']['name']; $email->addAttachment($file_to_attach); $email->send(); 进行服务。基本上这是WebHttpBinding服务。因此,您无法添加REST,因为Service Reference服务不会公开任何元数据,您需要通过RESTHTTP左右的GET动词来查询资源。

尝试:

POST

或者您可以选择:

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"))); 中启用一些属性来捕获更具体的异常详细信息。

答案 1 :(得分: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 :(得分:0)

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

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

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

添加它

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

4)检查 OperationContract ServiceContract 标记是否已正确设置

相关问题