在Areas之外的MVC应用程序中托管WCF服务

Ube*_*1.0 4 c# model-view-controller wcf web-services metadata

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

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

附加说明:

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

我昨天遇到此错误,并花了一些时间在互联网上寻找答案。这使我对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> 
Run Code Online (Sandbox Code Playgroud)

这给我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)));
    }
Run Code Online (Sandbox Code Playgroud)

谁能帮我?我在这里没有主意。

Ube*_*1.0 5

我已经解决了问题。首先,我错过了注册我的路线的功能的一部分路径。修复该路径后,我能够在自己的托管环境中显示我的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");           
    }
Run Code Online (Sandbox Code Playgroud)

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

感谢大家的帮助。希望这可以帮助其他可怜的迷路者。