在区域外的MVC应用程序中托管WCF服务

我有一个MVC项目,我在根目录中添加了一个名为WCF的文件夹。 在这个文件夹中,我创建了一个名为CustomFunctions的WCF服务。 当我尝试启动该服务时,收到以下错误:

错误:无法从http://localhost/Viper/WCF/CustomFunctions.svc获取元数据…元数据包含无法解析的引用:

附加说明:

无法找到类型为“Viper.WCF.CustomFunctions”的类型,作为ServiceHost指令中的Service属性值提供,或者在配置元素system.serviceModel / serviceHostingEnvironment / serviceActivations中提供。

昨天我收到了这个错误,花了一些时间在互联网上寻找答案。 这导致我对我的Web.config以及我的Global.asax.cs进行了很多更改。 昨天,它开始工作,我停了下来。 然而,当我今天早上回来时,它再也没有工作。 没有添加任何新内容,也没有更改代码。

我已将以下内容添加到我的Web.config中:

                           

这是我的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))); } 

谁能帮我? 我在这里完全没有想法。

我已经找到了问题所在。 首先,我错过了注册我的路由的函数路径的一部分。 在修复该路径之后,我能够在我的托管环境中显示我的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"); } 

我主要指定了自定义路由,以便当我导航到指定的url时,它会显示我的.svc文件。 我从Global.asax.cs中的ApplicationStart方法调用此方法。 我还必须为我的Home Area中的CustomFunctions创建一个单独的控制器和视图,以便它可以区分我的默认路由和CustomFunctions,并在我的路由映射中指定,如上所示。 因此,当我转到localhost \ Viper时,它将找到我的默认地图中指定的路由,当我转到localhost \ Viper \ CustomFunctions时,它将找到到我的.svc文件的路由。 IgnoreRoute基本上是这样的,因此您在调用页面时不必将文件扩展名放在URL的末尾。 因此,我只指定CustomFunctions而不是CustomFunctions.svc。 确保在执行此操作时将System.ServiceModel.Activation程序集和using语句添加到项目中。

谢谢大家的帮助。 希望这有助于其他一些穷人失去的灵魂。