[英]Hosting WCF soap and rest endpoints side by side
我寫了一個服務,我想通過休息和肥皂暴露。 我讀到的關於WCF 4.0的一切都說我只需要暴露2個具有不同行為的端點來完成這項工作。 但我無法讓它發揮作用。
這是我的服務合同:
[ServiceContract]
public interface MyService
{
[OperationContract]
[WebGet(UriTemplate="data/{value}")]
string GetData(string value);
}
這是我的web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MyService">
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>
<endpoint address="rest" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="MyService" />
<endpoint address="soap" behaviorConfiguration="soapBehavior" binding="basicHttpBinding" contract="MyService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp automaticFormatSelectionEnabled="true" helpEnabled="true" />
</behavior>
<behavior name="soapBehavior" />
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
我正在使用路由來定義我的服務URL:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("dns", new ServiceHostFactory(), typeof(MyService)));
}
}
有什么我在這里做錯了嗎? 我真的可以使用一些幫助。
我從來沒有找到在配置中執行此操作的“正確”方法,但是能夠使用路由引擎來實現此目的。
我的全局asax文件現在看起來像這樣:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("my/soap", new ServiceHostFactory(), typeof(MyService)));
RouteTable.Routes.Add(new ServiceRoute("my/rest", new WebServiceHostFactory(), typeof(MyService)));
}
}
和我的配置像這樣:(啟用其余的幫助頁面)
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint automaticFormatSelectionEnabled="true" helpEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
我喜歡這更符合asp.net MVC模型,並且需要很少的配置。 另外這樣做允許我從我的項目中完全刪除.svc文件,這也是一個加IMO。
你是如何托管你的WCF服務的? 在IIS中,您需要一個虛擬目錄和一個MyService.svc
文件來啟用服務激活。
如果您暫時刪除ServiceRoute(為了簡化問題),您應該能夠通過以下方式訪問您的SOAP服務端點:
http://YourServer:Port/YourVirtualDirectory/YourService.svc/soap
你的REST服務應該在
http://YourServer:Port/YourVirtualDirectory/YourService.svc/rest/data/{value}
(您為{value}
提供了一些任意值)。
究竟什么不適合你的情況?
您可以使用WCF測試客戶端嘗試測試SOAP端點,同時您應該能夠在任何瀏覽器中訪問REST URL。
這可以在配置中進行。 來自msdn論壇帖子的用戶Ladislav Mrnka: http ://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4e95575f-1097-4190-80dd-7a0f96d73f6e
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="iSell.Prospects.ProspectBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="iSell.Prospects.ProspectBehavior" name="iSell.Prospects.ProspectService">
<endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding" contract="iSell.Prospects.ProspectService" />
<endpoint address="soap" binding="basicHttpBinding" contract="iSell.Prospects.ProspectService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.