【问题标题】:C# call webservice by urlC#通过url调用webservice
【发布时间】:2011-05-03 19:04:03
【问题描述】:

我正在开发一个网络应用程序,我想通过它的 url 调用一个网络服务。我怎样才能做到这一点?

我在 global.asax 中注册了以下路线;

routes.MapRoute(
                "ServiceHandler",
                "{*path}/{*.svc}",
                new { Url = "Services/Service.svc/GetLatestTweets" }
                );

我想调用 web 服务,在这种情况下,通过类似于下面的 url 从 twitter 获取最新消息;

http://www.domainname.com/Services/Service.svc/GetLatestTweets

当我使用上面提到的注册路由时,我在下面添加的 stackstrace 中遇到了错误。

[ArgumentException: A catch-all parameter can only appear as the last segment of the route URL.
Parameter name: routeUrl]
   System.Web.Routing.RouteParser.Parse(String routeUrl) +3823656
   System.Web.Routing.Route..ctor(String url, IRouteHandler routeHandler) +24
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints, String[] namespaces) +86
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults, Object constraints) +28
   System.Web.Mvc.RouteCollectionExtensions.MapRoute(RouteCollection routes, String name, String url, Object defaults) +18
   CMS.Presentation.FrontEnd.Framework.CMSApplication.RegisterRoutes(RouteCollection routes) in C:\Projects\Website\Web.Presentation\FrontEnd\Framework\CMSApplication.cs:62
   CMS.Presentation.FrontEnd.Framework.CMSApplication.Application_Start() in C:\Projects\Website\Web.Presentation\FrontEnd\Framework\CMSApplication.cs:80

[HttpException (0x80004005): A catch-all parameter can only appear as the last segment of the route URL.
Parameter name: routeUrl]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +3988565
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +191
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +325
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +407
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +375

[HttpException (0x80004005): A catch-all parameter can only appear as the last segment of the route URL.
Parameter name: routeUrl]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11529072
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4784373

我应该做什么或改变,以便我可以通过它的 url 调用 web 服务?我正在使用一个简单的 wcf 服务

【问题讨论】:

    标签: c# asp.net-mvc wcf web-services routing


    【解决方案1】:

    你无法做到这一点。 .svc 扩展已注册到与 MVC 非常不同的处理程序,以便处理 WCF。如果您尝试更改它,该服务将永远无法工作。您可能需要从充当桥梁的控制器操作中调用 Web 服务。

    【讨论】:

    • 感谢您的快速响应。太糟糕了,这是不可能的,因为我想出来了,将尝试使用控制器作为桥接器
    【解决方案2】:

    也许我错过了什么,但似乎你可以这样做

            routes.MapRoute(
                "ServiceHandler",
                "Services/{service}.svc/{*path}",
                new { controller = "services", action = "ParseAndExecuteServiceCall" service = "TwitterService", path = "GetLatestTweets" }
                );
    

    并解析控制器中的远程调用,如果它更复杂的话。路由中只能有一个“catchall”参数,但可以使用任意数量的变量,并使用 IRouteConstraint 实现或 Regex 模式限制/定义它们。

    此外,对于下面关于 .svc 与 .mvc 处理程序的评论的回应 - 没有什么可以限制您重写 IHttpHandler 为了做到这一点,您需要重写 GetHttpHandler() 中的 IRouteHandler 方法...然后你可以做任何你想做的处理。

    【讨论】:

    • 感谢您的回复 :-) 你有什么例子我应该如何在控制器中处理这个吗?
    • 这取决于您需要如何解析字符串的服务。如果您要为 api 构建一个外观,那么您也可以实现一个模型绑定器,并且只需让控制器操作看起来像:ActionResult ExecuteServiceCall(ServiceFacade service, MethodInfo serviceMethod, NameValueCollection arguments){},但是,这真的是全部取决于你和你想怎么做。这可能是最好的事情。
    • 感谢您的回复,将尝试解决。我又想到了一个问题。为什么我不能使用这样的东西来忽略路线? routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
    • 听起来是个好主意。查看这篇博文,haacked.com/archive/2008/07/14/…。写它的人很聪明。
    • 我用 ignoreRoute 尝试了几个解决方案,但不幸的是我无法让它工作。最后,我通过在数据库级别的应用程序中注册一个路由来修复它(可能是由于应用程序设计),并且我能够调用一个控制器操作,该操作将从 twitter 为我获取信息。我放弃了服务的整个想法
    猜你喜欢
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2013-07-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多