2011-12-30 95 views
2

我已經編寫了幾個WCF數據服務,並發現它們非常有用。但是,我發現路由很痛苦。我見過對話表明你可以在ASP.Net MVC應用程序中託管數據服務(我一直使用ASP.Net網站)。但是,我似乎找不到如何實現這一目標的任何示例。有沒有人有我可以檢查或建議的任何參考?如何在ASP.Net MVC3應用程序中託管WCF數據服務

回答

0

WCF web api可能會做你正在尋找的東西。這是他們的getting started page。您將該服務託管在MVC應用程序內部,甚至可以掛接到MVC使用的相同路由。

+3

問題是關於WCF數據服務,它的工作方式與基於SOAP的WCF服務不同。我不確定你的回答是否適用於他們的問題。 – 2011-12-30 21:20:59

+0

感謝您的建議。不幸的是,從我可以確定的,@M。是正確的。這對數據服務不起作用。 – RockyMountainHigh 2011-12-31 21:17:13

2

前段時間發佈了這個問題,但我認爲仍然有人對在ASP.NET MVC項目中使用WCF數據服務感興趣。

假設你在你的項目中調用的服務:「DataSourceService.svc」你可以在MVC項目中使用這項服務由「RouteConfig.cs」配置路由如下:

using System.Data.Services; 
using System.ServiceModel.Activation; 
using System.Web.Mvc; 
using System.Web.Routing; 

namespace <YourNamespace> 
{ 
    public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
       // the controller should NOT contain "DataSourceService" 
       constraints: new { controller = "^((?!(DataSourceService)).)*$" } 
      ); 

      routes.Add(new ServiceRoute("DataSourceService", new DataServiceHostFactory(), typeof(DataSourceService))); 

     } 
    } 
} 

製作確保您在Web.config中有如下配置:

<configuration> 
    ... 
    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
    </system.serviceModel> 
    ... 
</configuration> 

現在,您可以檢查任何事情都在瀏覽器中運行你的項目,並使用以下URL正常工作:

http:// localhost:port_number/DataSourceService/$ metadata

...應該返回你的元數據。

相關問題