簡體   English   中英

在WCF中使用REST GET作為第二個端點

[英]Using REST GET in WCF as 2nd endpoint

我試圖在現有的WCF應用程序中添加第二個端點,但是我的REST方法不起作用。 我創建了新界面

[ServiceContract]
public interface IRestService
{
    [OperationContract]
    [WebGet(UriTemplate = "RestURI", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string SomeMethod();
}

並在現有服務中實施

public string SomeMethodImplementation()
    {
        //some logic
    }

我正在嘗試使用basicaddress / service / RestURI訪問此方法,但收到400錯誤的請求響應。

之后,我在配置文件中添加了protocolMapping,第二個端點和端點行為,但沒有幫助。

現在,我的配置文件如下所示:

<system.serviceModel>
<protocolMapping>
  <add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<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="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<services>
  <service name="servicename">
    <endpoint address="" binding="webHttpBinding" contract="IRestService" behaviorConfiguration="web"/>
    <endpoint address="" binding="basicHttpBinding" contract="ISoapService"/>
  </service>
</services>
<bindings>
  <basicHttpBinding>
    <binding maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" receiveTimeout="00:10:00" sendTimeout="00:10:00">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16348" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
    </binding>
  </basicHttpBinding>
</bindings>

我再也聽不到400了,而是“由於EndpointDispatcher的AddressFilter不匹配,無法在接收器上處理”。 錯誤出現。 同樣在添加了protocolMapping之后,當我在瀏覽器中打開wsdl時,我的soap方法也消失了。 沒有它,我的rest方法可以在這樣的列表中看到:

<wsdl:port name="BasicHttpBinding_IRestService" binding="i0:BasicHttpBinding_IRestService">

之后,我在Service的屬性中添加了AddressFilterMode = AddressFilterMode.Any),但這無濟於事。

我在這里找到了很多答案,但是某種程度上沒有一個答案可以幫助我解決此問題。 我錯過了什么?

在安裝WCF for Visual Studio之后,我找到了解決方案。 一直以來,我在web.config中都有錯誤的名稱空間,不知道,但是VS在安裝模塊后對其進行了標記。 我添加了正確的名稱空間,問題就消失了。 我真的建議在VS中安裝WCF。

問題歸結為我們無法以Restful風格托管服務。 通常,有兩種方法來配置Webhttpbinding創建的Restful樣式服務。

1.使用服務部分來配置接口和實現。

  <services>
          <service name="WcfService3.Service1">
            <endpoint address="" binding="webHttpBinding" contract="WcfService3.IService1" behaviorConfiguration="rest"></endpoint>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="rest">
              <webHttp />
            </behavior>
          </endpointBehaviors>
    </behaviors>

我們要注意兩點。 在服務部分,我們應該使用相應的接口和實現。

  <service name="WcfService3.Service1">
    <endpoint address="" binding="webHttpBinding" contract="WcfService3.IService1"

添加webhttp端點行為也是必不可少的,然后通過使用Behaviorconfiguration屬性將其應用。

behaviorConfiguration="mybinding"
  1. 在最新功能中,我們可以創建帶有協議映射的WCF Restful樣式服務。

      <endpointBehaviors> <behavior> <webHttp /> </behavior> </endpointBehaviors> </behaviors> <protocolMapping> <add binding="webHttpBinding" scheme="http"/> 

應該注意的是,我們需要添加一個沒有名稱的端點行為。 根據您的配置,由於您有多個接口,建議您刪除服務部分,並使用協議映射功能托管該服務。 1.刪​​除配置中的“服務”部分。 2.刪除端點行為的名稱。

<system.serviceModel>
<protocolMapping>
  <add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<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>
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>

然后,我們可以通過下面的Uri訪問該服務。

basicaddress / service.svc / RestURI

請隨時告訴我是否有什么我可以幫助的。

暫無
暫無

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

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