结合WCF SOAP和REST

时间:2012-02-16 09:50:45

标签: c# wcf soap

REST项目工作正常,可以通过以下地址访问:

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

我的REST项目中也有WCF SOAP项目,这两个项目分别在不同的文件夹“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);
    }
}  

REST部分:

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.  

编辑2

现在我发现了真正的问题。

当web.config中的ASP.NET兼容模式== true时,SOAP无法工作,而REST需要它。我该怎么办?感谢

2 个答案:

答案 0 :(得分:3)

我有一个REST项目,因为暴露了REST和SOAP服务。现在,我为一些客户端访问的SOAP服务放置了一个.svc文件。

下面的屏幕截图给出了我的项目的文件夹结构,global.asax中的路由配置,访问Rest服务和访问.svc文件的输出(SOAP服务)

sample screenshot

<强>更新 请找我的web.Config(我的应用程序托管在IIS上):

web.config

请找到我的实现我的接口ISampleService的类:

class

答案 1 :(得分: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>

这种方法可以很好地工作而不会被配置

带走