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

标签 c# wcf web-services model-view-controller metadata

我有一个 MVC 项目,我在根目录中添加了一个名为 WCF 的文件夹。在此文件夹中,我创建了一个名为 CustomFunctions 的 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)));
    }

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

最佳答案

我已经解决了这个问题。首先,我错过了注册我的路线的函数的路径的一部分。修复该路径后,我能够让我的 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 方法调用此方法。我还必须在我的主区域内为我的 CustomFunctions 创建一个单独的 Controller 和 View ,以便它可以区分我的默认路由和我的 CustomFunctions,并在我的路由映射中指定它,如上所示。因此,当我转到 localhost\Viper 时,它将找到在我的默认映射中指定的路由,当我转到 localhost\Viper\CustomFunctions 时,它将找到到我的 .svc 文件的路由。 IgnoreRoute 基本上做到了,因此您在调用页面时不必将文件扩展名放在 url 的末尾。因此,我只指定 CustomFunctions 而不是 CustomFunctions.svc。确保在执行此操作时将 System.ServiceModel.Activation 程序集和 using 语句添加到您的项目。

感谢大家的帮助。希望这能帮助其他一些可怜的迷失灵魂。

关于c# - 在区域外的 MVC 应用程序中托管 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13938746/

相关文章:

c# - Windows 任务栏右键菜单中的标题

wcf - 在(每个)Web API 操作之前执行代码

c# - 为什么 System.ServiceModel.Persistence 过时了?

java - Web服务客户端堆栈跟踪

Java Web服务复杂类型

java - WSWS3499W : How to make WAS understand the HTTP 302 response from a web service?

c# - PInvoke 创建桌面

c# - 从 C# 设置 Word 2010 文档页边距

c# - GridView 数据绑定(bind)

c# - 向 Silverlight 客户端公开数据库索引是否安全?