2012-12-18 48 views
4

我有一個MVC項目,我已經在根目錄中添加了一個名爲WCF的文件夾。在此文件夾中,我創建了一個名爲CustomFunctions的WCF服務。當我嘗試啓動我收到以下錯誤的服務:在區域之外的MVC應用程序中託管WCF服務

Error: Cannot obtain Metadata from http://localhost/Viper/WCF/CustomFunctions.svc ... Metadata contains a reference that cannot be resolved:

有了額外的描述:

The type 'Viper.WCF.CustomFunctions', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

昨天我得到這個錯誤,並花了一些時間在網上淘了一個答案。這導致我對Web.config和Global.asax.cs進行了很多更改。昨天一點,它開始工作,我停了下來。但是,當我今天早上回來時,它並沒有重新工作。沒有添加任何新內容,並且在此期間沒有任何代碼被更改。

我加入以下到我的web.config:

<system.serviceModel> 
<services> 
    <service behaviorConfiguration="WCFBehavior" name="Viper.WCF.CustomFunctions"> 
    <endpoint address="" binding="wsHttpBinding" contract="Viper.WCF.ICustomFunctions"> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://localhost/Viper/WCF/CustomFunctions/" /> 
     </baseAddresses> 
    </host> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="WCFBehavior"> 
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" httpGetUrl=""/> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 

這對我Global.asax.cs

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.svc/{*pathInfo}"); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new 
      { 
       controller = "Home", 
       action = "Index", 
       id = UrlParameter.Optional 
      }, // Parameter defaults 
      new { controller = "^(?!CustomFunctions).*" } 
     ); 

     routes.Add(new ServiceRoute("CustomFunctions", new ServiceHostFactory(), 
        typeof(CustomFunctions))); 
    } 

誰能幫助我?我在這裏完全沒有想法。

+1

我不確定這是可行的(儘管我懷疑它是這樣),但是有沒有一些原因讓你不想有不同的路徑?我們的MVC應用程序還使用了一個WCF服務,我在同一個應用程序池中託管,但由於您現在面臨的複雜情況,我只是選擇不採取這種方式,並沒有令人信服的理由讓事情比他們需要的更加困難是...但是如果有人發佈它,我會對解決方案感興趣... – Pete

+0

你看過嗎? http://stackoverflow.com/questions/4047485/expose-wcf-services-that-belong-to-an-area-in-mvc-app-at-a-routed-path – Pete

+0

我已經看過那篇文章關於路線並進行必要的更改,如我發佈的Global.asax.cs代碼中所示。我們希望將WCF服務放入我們的MVC項目的原因是我們不希望在兩個項目之間分開使用緩存。我們只想要一個。我的主管想讓我試着讓這個表演很好。但是,如果我不快速計算出某些東西,我可能會隨單獨的項目一起去,因爲我知道它在我的WCF項目之外的MVC中工作。 –

回答

4

我已經知道了這個問題。首先,我錯過了註冊我的路線的功能路徑的一部分。修復了這條路徑之後,我能夠讓我的wsdl顯示在我的託管環境中。但是,這搞砸了我的區域的默認路由。因此,對於任何人誰在未來有這個問題,這裏是我的解決方案:

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.svc/{*pathInfo}"); 

     routes.MapRoute(
      "CustomFunctions", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new 
      { 
       controller = "CustomFunctions", 
       action = "Index", 
       id = UrlParameter.Optional 
      }, // Parameter defaults 
      new { controller = "^(?!CustomFunctions).*" } 
     ); 

     routes.Add(new ServiceRoute("CustomFunctions", new ServiceHostFactory(), 
        typeof(CustomFunctions))); 

     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");   

     // Had to do some special stuff here to get this to work using a default area and no controllers/view in the root 
     routes.MapRoute(
      name: "Default", 
      url: "{area}/{controller}/{action}/{id}", 
      defaults: new { area = "", controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new string[] { "Viper.Areas.Home" } 
     ).DataTokens.Add("area", "Home");   
    } 

我指定我的自定義路線主要以至於當我瀏覽到指定的網址,然後它會顯示我的.svc文件。我從Global.asax.cs中的ApplicationStart方法調用此方法。我還必須在我的家居區域內爲我的CustomFunctions創建一個單獨的控制器和視圖,以便它可以區分我的默認路線和我的CustomFunctions,並在我的路線圖中指定,如上所示。所以當我到localhost \ Viper時,它會找到在我的默認映射中指定的路由,當我到localhost \ Viper \ CustomFunctions時,它會找到我的.svc文件的路由。 IgnoreRoute基本上是這樣做的,所以在調用頁面時不必將文件擴展名放在URL的末尾。所以,而不是CustomFunctions.svc我只指定CustomFunctions。確保您在執行此操作時將System.ServiceModel.Activation組合件和使用語句添加到項目中。

感謝大家的幫助。希望這有助於其他一些可憐的迷失靈魂。

相關問題