在区外的MVC应用程序托管WCF服务 [英] Hosting a WCF Service in an MVC application outside of Areas

查看:102
本文介绍了在区外的MVC应用程序托管WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an MVC project to which I have added a folder in the root directory called WCF. Within this folder I have created a WCF service entitled CustomFunctions. When I attempt to start the service I receive the following error:

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

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

通过一个附加说明:

类型'Viper.WCF.CustomFunctions',只要

在ServiceHost的指令中的服务属性值,或在配置元素system.serviceModel / serviceHostingEnvironment / serviceActivations找不到。

昨天我得到这个错误,并花了一些时间在网上淘了一个答案。这使我做出了很大的改动,以我的web.config以及我的Global.asax.cs。在一个点上,昨天它开始工作,我停了下来。然而,当我今天早上回来它不工作从头再来。 。没有新的加入,无代码的时间之间改变了

I was getting this error yesterday and spent some time scouring the internet for an answer. This led me to make a lot of changes to my Web.config as well as my Global.asax.cs. At one point yesterday, it started working and I stopped. However, when I came back this morning it wasn't working all over again. Nothing new was added and no code was changed between that time.

我添加了以下到我的web.config:

I have added the following to my 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)));
    }



谁能帮助我?我都离开这里的想法。

Can anyone help me? I'm all out of ideas here.

推荐答案

我已经想通了这个问题。首先,我缺少的路径注册我的路由功能的一部分。修复这条道路后,我能得到我的WSDL显示在我的托管环境。然而,这操蛋的默认路由我的地方。因此,对于任何人谁在未来的这个问题,这里是我的解决方案:

I've figured out the problem. First of all, I was missing a part of the path to the function that registers my routes. After fixing that path I was able to get my wsdl showing up in my hosting environment. However, this messed up the default routing for my areas. So for anyone who has this problem in the future, here is my solution:

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一个单独的控制器和视图,以便它可以我的默认路由和我CustomFunctions区分,并按上述规定可见,在我的路线图。所以,当我去localhost\Viper它会找到我的默认地图指定的路线,当我去localhost\Viper\CustomFunctions它会找到我的.svc文件的路径。该IgnoreRoute基本上使得它,所以你不必调用你的页面的时候把文件扩展名的URL的末尾。因此,而不是CustomFunctions.svc我只指定CustomFunctions。确保你这样做的时候System.ServiceModel.Activation组装和使用语句添加到您的项目。

I specified my custom route primarily so that when I navigated to the specified url then it would show my .svc file. I call this method from my ApplicationStart method in Global.asax.cs. I also had to create a seperate controller and view for my CustomFunctions within my Home Area so that it could differentiate between my default route and my CustomFunctions, and specified that in my route map as seen above. So when I go to localhost\Viper it will find the route specified in my default map and when I go to localhost\Viper\CustomFunctions it will find the route to my .svc file. The IgnoreRoute basically makes it so you don't have to put the file extension at the end of the url when calling your page. So rather than CustomFunctions.svc I only specify CustomFunctions. Make sure you add the System.ServiceModel.Activation assembly and using statement to your project when doing this.

谢谢大家对你的帮助。希望这有助于一些其他贫困丢了魂。

Thanks everyone for your help. Hope this helps some other poor lost soul.

这篇关于在区外的MVC应用程序托管WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆