2011-09-23 124 views
6

儘管讀了很多帖子,例如(This one seems popular)我似乎無法將我的服務公開爲與SOAP和REST協議兼容的多個端點 - 我的問題似乎與沒有WebServiceHostFactory的WCF REST和SOAP服務

Factory="System.ServiceModel.Activation.WebServiceHostFactory" 

元素在頁面後面的服務代碼中。

如果我不說,我的SOAP端點工作得很好,但找不到我的JSON端點。如果我放入該行,我的REST終結點節點會像bird一樣唱歌,並且SOAP終結點將導致Service.svc頁面中出現「Endpoint not found」。

我的行動似乎建立在標準的方式如:

[OperationContract] 
    [WebGet(UriTemplate = "/GetData", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] 
    string GetData(); 

和配置文件

<endpoint address="rest" binding="webHttpBinding" contract=".IMeterService" behaviorConfiguration="REST" /> 

<endpoint address="soap" binding="wsHttpBinding" contract="IMeterService" bindingConfiguration="secureBasic" /> 

<behavior name="REST"> 
     <webHttp /> 
</behavior> 

我怎樣才能做到這一點?有沒有辦法設置REST端點而沒有 System.ServiceModel.Activation.WebServiceHostFactory屬性?

在此先感謝。

+0

http://www.c-sharpcorner。com/uploadfile/dhananjaycoder/windows-authentication-on-rest-enabled-wcf-service/ – MaxRecursion

回答

5

如果不指定.svc文件的任何工廠,所有終端將來自web.config文件 - WCF將試圖找到一個<system.serviceModel/service>元素,其name屬性的完全限定名匹配服務類。如果找不到一個,那麼它將添加一個默認端點(使用basicHttpBinding,除非您更改了默認映射)。這似乎是你面臨的。確認<service>元素的「name」屬性與.svc文件中Service屬性的值匹配,並且您應該使兩個端點正常工作。

您可以嘗試做的另一件事是在服務中啓用跟蹤(level = Information)以查看服務上實際打開了哪些端點。下面的圖片:

SvcTraceViewer image

這個例子中的服務器是沒什麼大事:

namespace MyNamespace 
{ 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     string Echo(string text); 
    } 
    public class Service : ITest 
    { 
     public string Echo(string text) 
     { 
      return text; 
     } 
    } 
} 

沒有指定的Service.svc工廠:

<% @ServiceHost Service="MyNamespace.Service" Language="C#" debug="true" %> 

和Web .config定義兩個端點,這些端點顯示在跟蹤中:

<configuration> 
    <system.diagnostics> 
     <sources> 
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" 
       propagateActivity="true"> 
       <listeners> 
        <add type="System.Diagnostics.DefaultTraceListener" name="Default"> 
         <filter type="" /> 
        </add> 
        <add name="ServiceModelTraceListener"> 
         <filter type="" /> 
        </add> 
       </listeners> 
      </source> 
     </sources> 
     <sharedListeners> 
      <add initializeData="C:\temp\web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
       name="ServiceModelTraceListener" traceOutputOptions="Timestamp"> 
       <filter type="" /> 
      </add> 
     </sharedListeners> 
     <trace autoflush="true"/> 
    </system.diagnostics> 
    <system.serviceModel> 
     <behaviors> 
      <endpointBehaviors> 
       <behavior name="Web"> 
        <webHttp /> 
       </behavior> 
      </endpointBehaviors> 
     </behaviors> 
     <services> 
      <service name="MyNamespace.Service"> 
       <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="" 
        name="basic" contract="MyNamespace.ITest" /> 
       <endpoint address="web" behaviorConfiguration="Web" binding="webHttpBinding" 
        bindingConfiguration="" name="web" contract="MyNamespace.ITest" /> 
      </service> 
     </services> 
    </system.serviceModel> 
</configuration> 

請注意,偵聽器中顯示了一個額外的偵聽器,它是WCF的「幫助頁面」(當您瀏覽它時告訴服務沒有啓用元數據)。

您可以嘗試將此設置與您的設置進行比較,或者從這個簡單的設置開始,然後從您的設備開始添加組件,直到遇到問題。這將有助於隔離問題。

祝你好運!

+0

感謝您的期望,是在配置文件name =「MeterService」中指定的服務名稱與MeterService中的「名稱」匹配。 svc文件(例如Service =「MeterService」)。它必須與我的端點或服務配置有關,我只是不確定是什麼。 – Simian

+0

我用一個簡單的例子更新了我的答案;你可以試着看看它是否與你的有所不同。 – carlosfigueira