2012-02-16 159 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> 

这种方法工作得很好,没有被带走配置