2012-02-16 119 views
2

其餘項目工作正常,這可以通過這個地址訪問:結合WCF SOAP和REST

http://localhost:8525/Device/Login?deviceID=testid&password=a&serialNum=testserial

我也有WCF SOAP項目在我的REST的項目,這兩個項目都在不同的文件夾分離,「SOAP」和「REST」。

我的問題是,當我把這個代碼:

private void RegisterRoutes() 
{ 
    RouteTable.Routes.Add(new ServiceRoute("Device", new WebServiceHostFactory(), typeof(Rest.DeviceComponent)));    
} 

我無法訪問現在SOAP服務,我可以通過這個地址來訪問前:

http://localhost:8525/DeviceComponent.svc(使用WCFTest客戶端)

這裏是WebConfig

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
    <handlers> 
     <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd"/> 
    </handlers> 
    </system.webServer> 
</configuration> 

和裏面的Global.asax.cs

private void RegisterRoutes() 
{ 
    RouteTable.Routes.Add(new ServiceRoute("Device", new WebServiceHostFactory(), typeof(Rest.DeviceComponent))); 
}  

SOAP合同樣本

namespace TSDEVICE.SoapSVC.Interface 
{ 
    [ServiceContract] 
    public interface IDeviceComponent 
    { 
     [OperationContract] 
     Session Login(string deviceID, string password, string serialNum, string ip); 
     [OperationContract] 
     bool Logout(DeviceSession session); 
     [OperationContract] 
     bool IsLatestVersion(DeviceSession session, int version); 
     [OperationContract] 
     byte[] DownloadLatest(DeviceSession details); 
     [OperationContract] 
     DateTime GetServerTime(DeviceSession session, long branchID); 
     [OperationContract] 
     bool AddDevice(UserSession session, Device deviceitem); 
     [OperationContract] 
     bool RemoveDevice(UserSession session, long deviceID); 
    } 
} 

,其餘部分:

namespace TSDEVICE.Rest 
{ 
    [ServiceContract] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    public class DeviceComponent 
    { 
     [WebInvoke(UriTemplate = "Login?deviceID={deviceID}&password={password}&serialNum={serialNum}", Method = "POST")] 
     [OperationContract] 
     public TMODELDEVICE.Entities.Session Login(string deviceID, string password, string serialNum) 
     { 
      string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
      TMODELDEVICE.Logic.DeviceComponent restDC = new TMODELDEVICE.Logic.DeviceComponent(); 
      return restDC.Login(deviceID, password, serialNum, ip); 
     } 

     public string Sample() 
     { 
      return "Hello"; 
     } 
    } 
} 

我訪問SOAP和REST,我該怎麼做?非常感謝!

編輯

當我嘗試 「設置爲起始頁」 .svc文件,我得到這個錯誤:

Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata. 

EDIT 2

現在我發現真正的問題。

當web.config中的ASP.NET兼容模式爲true時,SOAP無法工作,而REST需要它。我該怎麼做?謝謝

+0

當你說你在REST項目中有WCF SOAP項目時,你是指單個解決方案中的2個不同的項目還是2個項目中的2個不同的項目?另外嘗試移動你的svc文件出路徑匹配的路線集合,應該沒問題。 – Rajesh 2012-02-16 10:01:42

+0

2個項目中的服務。 – fiberOptics 2012-02-16 10:04:29

回答

3

我有一個REST項目,作爲REST和SOAP服務公開。現在我爲SOAP服務放置了一個.svc文件供某些客戶端訪問。

下面的截圖給我的項目,在Global.asax中的路由配置,輸出訪問REST服務和訪問.svc文件(SOAP服務)

sample screenshot

更新的文件夾結構: 請找到我的網站。配置(我的應用程序託管在IIS):

web.config

請找我的類,它實現我的接口ISampleService:

class

+0

看來我們做了幾乎相同的方式,可能是我的webconfig有一些缺失?你可以告訴你webConfig嗎?謝謝。我會嘗試你做的。 – fiberOptics 2012-02-16 10:12:01

+0

用web.config的屏幕快照更新了我的答案 – Rajesh 2012-02-16 10:15:29

+0

我試圖按照你的方式,然後找到錯誤原因。看到我編輯的問題。 – fiberOptics 2012-02-16 10:49:07

0

雖然我很欣賞上面列出的解決方案 - 我有一個發現如果您不想過問問題並遵循KISS原則,則管理/部署要容易得多。

服務合同:IService.cs

namespace DontTazeMe.Bro 
{ 
    [ServiceContract] 
    public interface IService 
    { 
     [OperationContract] 
     [WebGet] 
     List<GeoMapData> GetToTheChopper(); 
    } 
} 

實現:Service.cs

namespace DontTazeMe.Bro 
{ 
    public class WSDLService : IService 
    { 
     public List<GeoMapData> GetToTheChopper() 
     { 
      return ItsNotEasyBeingChessy.Instance.GetToTheChopperGeoData(); 
     } 
    } 

    public class RESTService : WSDLService 
    { 
     // Let's move along folks, nothing to see here... 
     // Seriously though - there is no need to duplicate the effort made in 
     // the WSDLService class as it can be inherited and by proxy implementing 
     // the appropriate contract 
    } 
} 

配置

<system.serviceModel> 
    <services> 
     <!-- SOAP Service --> 
     <service name="DontTazeMe.Bro.WSDLService"> 
     <endpoint address="" binding="basicHttpBinding" contract="DontTazeMe.Bro.IService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8733/DontTazeMe.Bro/Service/" /> 
      </baseAddresses> 
     </host> 
     </service> 
     <service name="DontTazeMe.Bro.RESTService"> 
     <endpoint address="" binding="webHttpBinding" contract="DontTazeMe.Bro.IService" behaviorConfiguration="Restful" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8733/DontTazeMe.Bro/Rest/Service/" /> 
      </baseAddresses> 
     </host> 
     </service> 
     <behaviors> 
     <endpointBehaviors> 
     <behavior name="Restful"> 
      <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped" /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

這種方法工作得很好,沒有被帶走配置