简体   繁体   English

将 WCF Restful 服务添加到现有的 ASP.Net 网站

[英]Adding WCF Restful service to an existing ASP.Net Website

I am trying to add a wcf restful service to my existing asp.net 4.0 website (not mvc).我正在尝试向我现有的 asp.net 4.0 网站(不是 mvc)添加 wcf 宁静服务。 I am finding hard time trying to find a good tutorial that walks through in adding one to an existing app/website.我发现很难找到一个很好的教程来逐步将一个添加到现有的应用程序/网站。 Everywhere I see there are notes on creating a new project (with wcf restful template).我看到的每个地方都有关于创建新项目的注释(使用 wcf restful 模板)。 What are the basic steps?基本步骤是什么? can I do this without an.svc file since I am on .net 4.0?因为我使用的是 .net 4.0,所以我可以在没有 .svc 文件的情况下执行此操作吗? Any tutorial you can point me to?有什么教程可以指点我吗?

So far i've tried adding a IService.cs and Service.cs classes that define service contracts and uses webinvoke/webget attributes, added routes in global.asax and service definitions in web.cong as shown in this tutorial http://www.codeproject.com/Articles/255684/Create-and-Consume-RESTFul-Service-in.NET-Framewor but when I point to localhost/restservice, I get 404 not found.到目前为止,我已经尝试添加一个 IService.cs 和 Service.cs 类来定义服务契约并使用 webinvoke/webget 属性,在 global.asax 中添加路由,在 web.cong 中添加服务定义,如本教程所示http://www .codeproject.com/Articles/255684/Create-and-Consume-RESTFul-Service-in.NET-Framewor但是当我指向 localhost/restservice 时,我得到 404 not found。

update: This is what I have in my config file.更新:这是我的配置文件中的内容。 Note that is an existing wcf soap based service.请注意,这是一个现有的基于 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>

You don't need any *.svc file for a WCF REST service - you can activate it using a ServiceRoute - see RESTful WCF Services with No svc file and No config for a great explanation.对于 WCF REST 服务,您不需要任何*.svc文件 - 您可以使用ServiceRoute激活它 - 请参阅RESTful WCF Services with No svc file and No config以获得很好的解释。

Basically what it boils down to is adding a ServiceRoute to your route table in global.asax :基本上它归结为在global.asax中添加一个ServiceRoute到你的路由表:

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

Not sure whether you can do without svc file.Adding svc file is not abig deal.Just click add new item and YourServiceName.svc.In the sc file enter the following code:-不确定你是否可以没有 svc 文件。添加 svc 文件不是什么大事。只需单击添加新项目和 YourServiceName.svc。在 sc 文件中输入以下代码:-

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

.net 4 may come with easy ways to add the svc file directly .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)));
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM