测试程序只是不会调用服务,但一切看起来都没问题 - 找不到404

时间:2017-04-27 03:55:23

标签: wcf

我已经创建了一个WCF服务来接收来自发送应用程序的数据。运行时,会显示常用的索引列表:

enter image description here

并点击.svc即可显示成功页面:

enter image description here

所以,我已经创建了一个基本的测试表单来调用服务的方法(有2个,都是" POST"方法)。由于服务方法使用JSON& webhttp,我试图通过HttpWebRequest调用服务。

private void button1_Click(object sender, EventArgs e) {
  try { 
    var request = (HttpWebRequest)WebRequest.Create("http://localhost:54945/Take2ToOffice.svc/ErrorReport");

    request.ContentType = "'text/json; charset=utf-8'";
    request.Method = "POST";

    byte[] chunk = new byte[2048]; //chunk gets initialised, but what it 
                                   //contains is mostly irrelevant here
    string result = "";

    ErrorInfo EI = new ErrorInfo {
      Edumis = "9999",
      SiteName = "Unknown",
      ErrorID = "123",
      ContentBlock = chunk
    };

    string json = new JavaScriptSerializer().Serialize(EI);

    //MessageBox.Show(json);

    using (var sW = new StreamWriter(request.GetRequestStream())) {
      sW.Write(json);
      sW.Flush();
      sW.Close();
    }

    var response = (HttpWebResponse)request.GetResponse();
    using (var streamReader = new StreamReader(response.GetResponseStream())) {
      result = streamReader.ReadToEnd();
    }
    MessageBox.Show(result);
  }
  catch (WebException wex) {
    string lcResult;
    if (wex.Response != null) {
      lcResult = "ERROR (web exception, response generated): " + Environment.NewLine;
      StreamReader sr = new StreamReader(wex.Response.GetResponseStream());
      lcResult += sr.ReadToEnd();
    }
    else {
      lcResult = "ERROR (web exception, NO RESPONSE): " + wex.Message + wex.StackTrace;
    }
    MessageBox.Show(lcResult);
  }
  catch (Exception ex) {
    MessageBox.Show(ex.Message);
  }
}

这一切似乎都是非常基本的东西 - 特别是因为几年前我写了另一个与此非常类似的服务,而我现在所做的一切的起点就是我之前写的代码 - 并且在此我只是在我的开发环境中这样做。

然而,

var response = (HttpWebResponse)request.GetResponse();

返回404 Not Found。 WebException.Response包含:

{System.Net.HttpWebResponse}
    [System.Net.HttpWebResponse]: {System.Net.HttpWebResponse}
    base: {System.Net.HttpWebResponse}
    ContentLength: 0
    ContentType: ""
    Headers: {X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcVXNlclxEb2N1bWVudHNcVmlzdWFsIFN0dWRpb1xWaXN1YWwgU3R1ZGlvIDIwMTNcUHJvamVjdHNcVGFrZTJPZmZpY2VDb21tdW5pY2F0aW9uc1xUYWtlMk9mZmljZUNvbW11bmljYXRpb25zXFRha2UyVG9PZmZpY2Uuc3ZjXEVycm9yUmVwb3J0?=
Content-Length: 0
Cache-Control: private
Date: Thu, 27 Apr 2017 03:19:29 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
}
    IsFromCache: false
    IsMutuallyAuthenticated: false
    ResponseUri: {http://localhost:54945/Take2ToOffice.svc/ErrorReport}
    SupportsHeaders: true

但根本没有实际响应(长度= 0)。如果我在服务中放置断点,(并启动测试表单和服务 - 在同一解决方案中单独的项目),断点永远不会被命中。

从逻辑上讲,服务似乎没有运行,或者由于某种原因无法找到 - 但是在浏览器和地址(localhost:54945)中很容易看到列表和成功页面在程序中显示。

任何人都可以解释发生了什么,或者可能出了什么问题?请帮忙,我完全难过了。

web.config如下:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    ...
  </connectionStrings>
  <appSettings>
    ...
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Take2OfficeCommunications.Take2ToOffice" behaviorConfiguration="serviceBehavior">
        <endpoint address="rest" 
                  binding="webHttpBinding" 
                  bindingConfiguration="WebBinding" 
                  behaviorConfiguration="web" 
                  name="restEndpoint" 
                  contract="Take2OfficeCommunications.ITake2ToOffice"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="WebBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="Transport" />
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

感谢您的关注,以及您可以提出的任何意见。

0 个答案:

没有答案