2010-07-29 38 views
11

我已經寫了一個服務,我想通過休息和肥皂公開。我所讀到的關於WCF 4.0的一切都表明,我只需要公開具有不同行爲的兩個端點來執行此操作。但我無法讓它工作。主持WCF肥皂和休息端點並排

這裏是我的服務合同:

[ServiceContract] 
public interface MyService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate="data/{value}")] 
    string GetData(string value); 
} 

這裏是我的web.config:

<?xml version="1.0"?> 
<configuration> 

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

     <services> 
      <service name="MyService"> 
       <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/> 
       <endpoint address="rest" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="MyService" /> 
       <endpoint address="soap" behaviorConfiguration="soapBehavior" binding="basicHttpBinding" contract="MyService" /> 
      </service> 
     </services> 

     <behaviors> 

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

      <endpointBehaviors> 
       <behavior name="restBehavior"> 
        <webHttp automaticFormatSelectionEnabled="true" helpEnabled="true" /> 
       </behavior> 
       <behavior name="soapBehavior" /> 
      </endpointBehaviors> 

     </behaviors> 

     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 

    </system.serviceModel> 

    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true" /> 
    </system.webServer> 

</configuration> 

我使用的路由來定義我的服務網址:

public class Global : System.Web.HttpApplication 
    { 
     protected void Application_Start(object sender, EventArgs e) 
     { 
      RouteTable.Routes.Add(new ServiceRoute("dns", new ServiceHostFactory(), typeof(MyService))); 
     } 
    } 

有我在這裏做錯了什麼?我真的可以用一些幫助。

+0

我也發現這個其他[提問/回答(http://stackoverflow.com/questions/186631/rest-soap-endpoints-for-a-wcf - 服務)有幫助。 – SMB 2011-09-14 23:46:10

回答

4

我從來沒有在配置中找到「正確」的方式來做到這一點,但能夠使用路由引擎來實現這一點。

我的全球ASAX文件現在看起來是這樣的:

public class Global : System.Web.HttpApplication 
    { 
     protected void Application_Start(object sender, EventArgs e) 
     { 
      RouteTable.Routes.Add(new ServiceRoute("my/soap", new ServiceHostFactory(), typeof(MyService))); 
      RouteTable.Routes.Add(new ServiceRoute("my/rest", new WebServiceHostFactory(), typeof(MyService))); 
     } 
    } 

和我的配置是這樣的:(使其餘的幫助頁面)

<system.serviceModel> 

    <standardEndpoints> 
     <webHttpEndpoint> 
      <standardEndpoint automaticFormatSelectionEnabled="true" helpEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 

</system.serviceModel> 

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true" /> 
</system.webServer> 

我喜歡,這是符合更多的asp.net MVC模型,並需要小配置。此外,這樣做可以讓我從我的項目中完全刪除.svc文件,這也是一個很好的IMO。

1

你是如何託管你的WCF服務?在IIS中,您需要一個虛擬目錄和一個MyService.svc文件來啓用服務。

如果刪除ServiceRoute現在(爲簡單起見),你應該能夠到達你的SOAP服務端點:

http://YourServer:Port/YourVirtualDirectory/YourService.svc/soap 

和您的REST服務應該是在

http://YourServer:Port/YourVirtualDirectory/YourService.svc/rest/data/{value} 

(您爲{value}提供一些任意值)。

究竟是什麼不工作在你的情況?

您可以嘗試使用WCF Test Client來測試您的SOAP端點,而您應該能夠在任何瀏覽器中打開REST URL。

+1

我在IIS中託管。 ,我看到的問題,如果,如果它被配置爲根URL SOAP端點時,纔可訪問: 要麼 的http:// YourServer:端口/ YourVirtualDirectory/YourService.svc 或 的http:// YourServer: Port/YourVirtualDirectory/YourService (取決於我是否使用服務路線)。 但是,如果我這樣配置,那麼由於某種原因,其餘終端無法訪問。或者,如果我按照原來的描述進行配置,則只能訪問剩餘的端點,並且soap端點會返回400錯誤。 – Troy 2010-08-02 17:39:20

+0

我試着給我的項目添加一個全新的服務文件(.svc)。使用與上面相同的配置,它可以工作... rest和soap服務都可以在.svc url中正常工作,但soap服務只能從根訪問。沒有遵守配置的/ soap地址。 – Troy 2010-08-02 17:56:18

1

這是可以做到的配置。從MSDN論壇主題由用戶拉吉斯拉夫Mrnka:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4e95575f-1097-4190-80dd-7a0f96d73f6e

<system.serviceModel> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="REST"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior name="iSell.Prospects.ProspectBehavior"> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<services> 
    <service behaviorConfiguration="iSell.Prospects.ProspectBehavior" name="iSell.Prospects.ProspectService"> 
    <endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding" contract="iSell.Prospects.ProspectService" /> 
    <endpoint address="soap" binding="basicHttpBinding" contract="iSell.Prospects.ProspectService" /> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 
</system.serviceModel> 
+1

暗示任何人試圖讓這個工作。您的soap URI將如下所示:'http:// host/myService.svc/soap /' – dummy 2014-10-09 16:15:05