【问题标题】:Adding WCF Restful service to an existing ASP.Net Website将 WCF Restful 服务添加到现有 ASP.Net 网站
【发布时间】:2012-04-22 20:11:03
【问题描述】:

我正在尝试将 wcf restful 服务添加到我现有的 asp.net 4.0 网站(不是 mvc)。我很难找到一个很好的教程来逐步将一个添加到现有的应用程序/网站。我看到的任何地方都有关于创建新项目的注释(使用 wcf restful 模板)。 基本步骤是什么?因为我在 .net 4.0 上,所以我可以在没有 .svc 文件的情况下执行此操作吗?有什么教程可以指点我吗?

到目前为止,我已经尝试添加定义服务合同并使用 webinvoke/webget 属性的 IService.cs 和 Service.cs 类,在 global.asax 中添加路由并在 web.cong 中添加服务定义,如本教程中所示 @987654321 @ 但是当我指向 localhost/restservice 时,我得到 404 not found。

更新: 这就是我的配置文件中的内容。请注意,这是一个现有的基于 wcf soap 的服务。

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="migrationServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="migrationHttpBinding" maxBufferSize ="104857600" maxReceivedMessageSize="104857600"/>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name ="Site.Web.WebService.MigrationService" behaviorConfiguration="migrationServiceBehavior">
        <endpoint binding="basicHttpBinding"
                  bindingConfiguration="migrationHttpBinding"
                  contract="Site.Web.WebService.IMigrationService"/>
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

【问题讨论】:

  • 你能发布你的配置文件吗?另外,您目前如何托管您的应用程序是在 IIS 上还是在 cassini 上运行?
  • 请发布您的配置文件,并确保您已按照以下@Marc_s 的建议添加路由表条目。

标签: c# asp.net .net wcf


【解决方案1】:

不确定是否可以不使用svc文件。添加svc文件没什么大不了的。只需单击添加新项目和YourServiceName.svc即可。在sc文件中输入以下代码:-

<%@ServiceHost Service="YourNamsespace.YourWCFservice"%>

.net 4 可能带有直接添加 svc 文件的简单方法

public class Global : System.Web.HttpApplication
 {
     protected void Application_Start(object sender, EventArgs e)
  {
         RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(YourService)));
   }
 }

【讨论】:

  • 我希望避免添加它,以便我的 URI 看起来像 site.com/restservice/getdata 而不是 site.com/restservice.svc/getdata ...
  • 您可以使用.net 4中的路由表来避免它。您可以在global.asax文件中添加更改
【解决方案2】:

对于 WCF REST 服务,您不需要任何 *.svc 文件 - 您可以使用 ServiceRoute 激活它 - 请参阅 RESTful WCF Services with No svc file and No config 以获得很好的解释。

基本上归结为在global.asax 中的路由表中添加ServiceRoute:

protected void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), 
                                           typeof(YourService)));
}

【讨论】:

  • 我的路由中确实有这个,但结果是 404... protected void Application_Start(object sender, EventArgs e) { System.Web.Routing.RouteTable.Routes.Add(new ServiceRoute("RestService ", 新 System.ServiceModel.Activation.WebServiceHostFactory(), typeof(Site.Web.WebServices.Service))); }
  • 您的服务合同(接口)应该具有类似[WebGet(UriTemplate = "/test")] 或类似的属性。这将定义要连接的 URL - 例如http://yourserver/test - 你的情况是什么意思?这NOT由ServiceRoute 构造函数的第一个参数(在您的示例中为RestService)确定!
  • 我把它写成 [WebGet(UriTemplate = "")]
  • @xoail:然后尝试仅连接到http://site.com - 这会调用您的服务吗?如果没有:尝试在您的服务合同中设置UriTemplate = "/test" 或类似的内容,然后连接到http://site.com/test - 可以吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
相关资源
最近更新 更多