2012-04-10 61 views
4

我想向现有的asp.net 4.0网站(而不是mvc)添加一个wcf的restful服务。我很难找到一个很好的教程,通过添加一个到现有的应用程序/网站。我看到的每个地方都有关于创建新项目的注释(使用wcf restful模板)。 什么是基本步骤?我可以做到这一点没有.svc文件,因为我在.net 4.0?你可以指点我的任何教程?将WCF Restful服务添加到现有的ASP.Net网站

到目前为止,我已经尝试添加一个定义服务合约并使用webinvoke/webget属性的IService.cs和Service.cs类,在本教程中显示的global.asax和web.cong中的服务定义中添加路由http://www.codeproject.com/Articles/255684/Create-and-Consume-RESTFul-Service-in-NET-Framewor但是当我指向localhost/restservice时,我找不到404。

更新: 这是我在我的配置文件中。请注意,这是一个现有的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> 
+0

您可以发布您的配置文件?此外,你如何托管你的应用程序是在IIS上运行还是在卡西尼上运行? – Rajesh 2012-04-10 15:02:11

+0

它在IIS上运行... – xoail 2012-04-10 15:19:35

+0

请发布您的配置文件,并确保您添加了@Marc_s建议的路由表条目。 – Rajesh 2012-04-10 15:21:35

回答

9

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

基本上它归结为是增加什么ServiceRoute你的路由表中global.asax

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

我确实在我的路线中有这个,但是结果在404 ...保护无效Application_Start(对象发件人,EventArgs e) { System.Web.Routing.RouteTable.Routes.Add(新的ServiceRoute(“RestService”,新的System.ServiceModel.Activation.WebServiceHostFactory(),typeof(Site.Web.WebServices.Service))); } – xoail 2012-04-10 15:25:59

+0

@xoail:你用什么URL连接到这个设置.... – 2012-04-10 18:55:23

+0

site.com/RestService – xoail 2012-04-10 19:21:30

2

不知道你是否可以不使用SVC file.Adding SVC文件做的不是abig deal.Just单击添加新项和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))); 
    } 
} 
+0

我希望避免添加它,以便我的URI看起来更干净,如site.com/restservice/getdata而不是site.com/restservice.svc/getdata ... – xoail 2012-04-10 14:28:30

+1

您可以使用.net 4中的路由表来避免它。您可以在global.asax文件中添加更改 – 2012-04-10 14:42:53