簡體   English   中英

WCF測試客戶端給出正確的結果,但在Chrome中執行時卻不能

[英]WCF Testing Client giving right results but not when executed in Chrome

我正在嘗試構建我的第一個WCF服務。 我現在有以下行為。

  1. 運行WCF服務時,我可以發送輸入並在Testing Client中獲得正確的結果。
  2. 當我在Chrome中輸入http://localhost:12345/Service1.svc ,我得到一個頁面。
  3. 單擊svcutil.exe http://localhost:12345/Service1.svc?wsdl會給我一個XML。

但是,當我輸入http:// localhost:12345 / Service1.svc / test / 13時 ,我只會得到一個空響應。 除了<body><pre>什么都沒有。 我該怎么辦?我該如何解決? (請記住,我是新手。)一旦按照我想要的方式運行行為(以便在瀏覽器中看到正確的結果),我將以XML生成REST或JSON數據格式(如果有任何重要性)。

討論中我得到了這一點。

namespace WcfService1
{
  [ServiceContract]
  public interface IService1
  {
    [OperationContract]
    [WebGet(UriTemplate = "/test/{indata}", 
      ResponseFormat = WebMessageFormat.Xml)]
    String Ping(String indata);
  }
}

這個問題可以看出,我的實現如下。

namespace WcfService1
{
  public class Service1 : IService1
  {
    public string Ping(String indata)
    {
      return "Pong " + indata;
    }
  }
}

建議的web.config無法正常工作,因此我嘗試結合本文的討論使用本文中的指針發布元數據(無論是哪種形式)。 我的配置文件看起來與后一個鏈接中的配置文件差不多(除了我已刪除診斷部分)。

我相信WCF測試客戶端在SOAP上運行。 它測試的是您正在提供某些東西的事實,而不是您正在提供想要得到的東西。

根據我的經驗,您得到的空的身體不過是一條錯誤消息。 但是,在某些情況下,例如跨域調用(不確定名稱是否正確,也不是可能出現的問題的完整列表),當您使用JavaScript中的XDomainRequest對象(與通常的XmlHttpRequest相對)時,響應為空是錯誤消息的結果。

您是否嘗試檢查狀態碼? 是200 OK還是更大的東西?

我相信您在Social MSDN上也提出了類似的問題,並且在如何編寫代碼方面有些困惑。 讓我回顧一下下面的要點。

Web.config文件

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>
    <services>
      ...
    </services>
    <behaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

服務 -描述服務性質的標簽內容

<service name="DemoRest.RestService" 
         behaviorConfiguration="ServiceBehavior">
  <endpoint address="" binding="webHttpBinding" 
            contract="DemoRest.IRestService" 
            behaviorConfiguration="web"></endpoint>
</service>

behaviors-標記的內容,描述服務的行為和端點

<serviceBehaviors>
  <behavior name="ServiceBehavior">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true"/>
  </behavior>
</serviceBehaviors>

<endpointBehaviors>
  <behavior name="web">
    <webHttp/>
  </behavior>
</endpointBehaviors>

現在,可以使用以下URL檢索響應。

localhost/Service1.svc/inputData

您可以嘗試以下操作:

  1. 使用ServiceBehaviour屬性標記服務實現

     [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class Service1 : IService1 
  2. 在web.config內,添加/修改以下保留現有數據:

     <services> <service name="WcfService1.Service1"> <endpoint binding="webHttpBinding" contract="WcfService1.IService1" /> </service> </services> <behaviors> <endpointBehaviors> <behavior> <webHttp /> </behavior> </endpointBehaviors> ... </behaviors> 

這些步驟使其正常工作。

為了使REST使用dot net framework 4簡化的配置工作 ,您的web.config需要包含以下內容:

<system.serviceModel>
  <!-- 1) Specify webHttp as an endpoint behavior -->
  <behaviors>
    <endpointBehaviors>
      <behavior >
        <webHttp/>
      </behavior>
    </endpointBehaviors>
  </behaviors>

  <!-- 2) Setup a protocol mapping to allow the service to be accessed via webHttp-->
  <protocolMapping>
    <add scheme="http" binding="webHttpBinding"/>
  </protocolMapping>
</system.serviceModel>

要獲得瀏覽器中沒有所有xml的輸出,請添加以下內容:

<!-- Configure the webHttp standard endpoint -->
<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
  </webHttpEndpoint>
</standardEndpoints>

要允許訪問服務元數據(需要輕松創建代理),請將其添加到behaviors元素:

 <serviceBehaviors>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
 </serviceBehaviors>

我個人更喜歡舊式配置,在該配置中可以顯式配置端點,如下所示:

 <system.serviceModel>
  <services>
    <service name="WcfService1.Service1">
      <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1"  behaviorConfiguration="webHttpBehavior"/>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttpBehavior">
        <webHttp automaticFormatSelectionEnabled="true"/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

最后,您可以使用WCF服務配置編輯器使用gui進行配置。 您可以在Visual Studio的工具菜單上找到它。 打開后,用它打開項目的web.config並開始編輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM