簡體   English   中英

將 WCF Restful 服務添加到現有的 ASP.Net 網站

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

我正在嘗試向我現有的 asp.net 4.0 網站(不是 mvc)添加 wcf 寧靜服務。 我發現很難找到一個很好的教程來逐步將一個添加到現有的應用程序/網站。 我看到的每個地方都有關於創建新項目的注釋(使用 wcf restful 模板)。 基本步驟是什么? 因為我使用的是 .net 4.0,所以我可以在沒有 .svc 文件的情況下執行此操作嗎? 有什么教程可以指點我嗎?

到目前為止,我已經嘗試添加一個 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。

更新:這是我的配置文件中的內容。 請注意,這是一個現有的基於 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>

對於 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)));
}

不確定你是否可以沒有 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)));
   }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM