我有一个WCF服务,我想把它同时公开为RESTfull服务和SOAP服务。 有人做过类似的事吗?


当前回答

MSDN现在似乎对此有一篇文章:

https://msdn.microsoft.com/en-us/library/bb412196 (v = vs.110) . aspx

人物介绍:

默认情况下,Windows Communication Foundation (WCF)使端点仅对SOAP客户机可用。在“如何:创建基本WCF Web HTTP服务”中,端点对非soap客户端可用。有时,您可能希望使相同的契约以Web端点和SOAP端点两种方式可用。本主题展示了如何做到这一点的示例。

其他回答

我们必须定义REST端点的行为配置

<endpointBehaviors>
  <behavior name="restfulBehavior">
   <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped" automaticFormatSelectionEnabled="False" />
  </behavior>
</endpointBehaviors>

还有一项服务

<serviceBehaviors>
   <behavior>
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
   </behavior>
</serviceBehaviors>

在行为之后,下一步是绑定。例如,basicHttpBinding到SOAP端点,webHttpBinding到REST端点。

<bindings>
   <basicHttpBinding>
     <binding name="soapService" />
   </basicHttpBinding>
   <webHttpBinding>
     <binding name="jsonp" crossDomainScriptAccessEnabled="true" />
   </webHttpBinding>
</bindings>

最后,我们必须在服务定义中定义端点2。注意对于地址=“”的端点,到哪里去REST服务是没有必要的。

<services>
  <service name="ComposerWcf.ComposerService">
    <endpoint address="" behaviorConfiguration="restfulBehavior" binding="webHttpBinding" bindingConfiguration="jsonp" name="jsonService" contract="ComposerWcf.Interface.IComposerService" />
    <endpoint address="soap" binding="basicHttpBinding" name="soapService" contract="ComposerWcf.Interface.IComposerService" />
    <endpoint address="mex" binding="mexHttpBinding" name="metadata" contract="IMetadataExchange" />
  </service>
</services>

在服务的Interface中,我们用操作的属性定义操作。

namespace ComposerWcf.Interface
{
    [ServiceContract]
    public interface IComposerService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/autenticationInfo/{app_id}/{access_token}", ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        Task<UserCacheComplexType_RootObject> autenticationInfo(string app_id, string access_token);
    }
}

加入各方,这将是我们的WCF系统。serviceModel定义。

<system.serviceModel>

  <behaviors>
    <endpointBehaviors>
      <behavior name="restfulBehavior">
        <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped" automaticFormatSelectionEnabled="False" />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>

  <bindings>
    <basicHttpBinding>
      <binding name="soapService" />
    </basicHttpBinding>
    <webHttpBinding>
      <binding name="jsonp" crossDomainScriptAccessEnabled="true" />
    </webHttpBinding>
  </bindings>

  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>

  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

  <services>
    <service name="ComposerWcf.ComposerService">
      <endpoint address="" behaviorConfiguration="restfulBehavior" binding="webHttpBinding" bindingConfiguration="jsonp" name="jsonService" contract="ComposerWcf.Interface.IComposerService" />
      <endpoint address="soap" binding="basicHttpBinding" name="soapService" contract="ComposerWcf.Interface.IComposerService" />
      <endpoint address="mex" binding="mexHttpBinding" name="metadata" contract="IMetadataExchange" />
    </service>
  </services>

</system.serviceModel>

为了测试这两个端点,我们可以将WCFClient用于SOAP,将PostMan用于REST。

我就是这么做的。确保你把 webHttp automaticFormatSelectionEnabled="true"内部端点行为。

[ServiceContract]
public interface ITestService
{

    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/product", ResponseFormat = WebMessageFormat.Json)]
    string GetData();
}

public class TestService : ITestService
{
    public string GetJsonData()
    {
        return "I am good...";
    }
}

内部服务模式

   <service name="TechCity.Business.TestService">

    <endpoint address="soap" binding="basicHttpBinding" name="SoapTest"
      bindingName="BasicSoap" contract="TechCity.Interfaces.ITestService" />
    <endpoint address="mex"
              contract="IMetadataExchange" binding="mexHttpBinding"/>
    <endpoint behaviorConfiguration="jsonBehavior" binding="webHttpBinding"
              name="Http" contract="TechCity.Interfaces.ITestService" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8739/test" />
      </baseAddresses>
    </host>
  </service>

端点的行为

  <endpointBehaviors>
    <behavior name="jsonBehavior">
      <webHttp automaticFormatSelectionEnabled="true"  />
      <!-- use JSON serialization -->
    </behavior>
  </endpointBehaviors>

MSDN现在似乎对此有一篇文章:

https://msdn.microsoft.com/en-us/library/bb412196 (v = vs.110) . aspx

人物介绍:

默认情况下,Windows Communication Foundation (WCF)使端点仅对SOAP客户机可用。在“如何:创建基本WCF Web HTTP服务”中,端点对非soap客户端可用。有时,您可能希望使相同的契约以Web端点和SOAP端点两种方式可用。本主题展示了如何做到这一点的示例。

这篇文章已经有一个很好的答案,由“社区维基”,我也推荐看看Rick Strahl的网络博客,有很多关于WCF Rest这样的好文章。

我用这两种方式来获得这种我的服务…然后我可以使用来自jQuery的rest接口或来自Java的SOAP。

这是我的网页。配置:

<system.serviceModel>
 <services>
  <service name="MyService" behaviorConfiguration="MyServiceBehavior">
   <endpoint name="rest" address="" binding="webHttpBinding" contract="MyService" behaviorConfiguration="restBehavior"/>
   <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>
   <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="MyService"/>
  </service>
 </services>
 <behaviors>
  <serviceBehaviors>
   <behavior name="MyServiceBehavior">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true" />
   </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
   <behavior name="restBehavior">
    <webHttp/>
   </behavior>
  </endpointBehaviors>
 </behaviors>
</system.serviceModel>

这是我的服务舱。svc后台代码,不需要接口):

    /// <summary> MyService documentation here ;) </summary>
[ServiceContract(Name = "MyService", Namespace = "http://myservice/", SessionMode = SessionMode.NotAllowed)]
//[ServiceKnownType(typeof (IList<MyDataContractTypes>))]
[ServiceBehavior(Name = "MyService", Namespace = "http://myservice/")]
public class MyService
{
    [OperationContract(Name = "MyResource1")]
    [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "MyXmlResource/{key}")]
    public string MyResource1(string key)
    {
        return "Test: " + key;
    }

    [OperationContract(Name = "MyResource2")]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource/{key}")]
    public string MyResource2(string key)
    {
        return "Test: " + key;
    }
}

实际上我只使用Json或Xml,但这两个都是为了演示目的。这些是获取数据的get请求。要插入数据,我将使用带有属性的方法:

[OperationContract(Name = "MyResourceSave")]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource")]
public string MyResourceSave(string thing){
    //...

如果你只想开发一个单一的web服务,并将其托管在许多不同的端点上(即SOAP + REST,带有XML、JSON、CSV、HTML输出)。您还应该考虑使用ServiceStack,我正是为此目的构建的ServiceStack,其中您开发的每个服务都自动在SOAP和REST端点上可用,无需任何配置。

Hello World示例展示了如何创建一个简单的with service(不需要配置):

public class Hello {
    public string Name { get; set; }
}

public class HelloResponse {
    public string Result { get; set; }
}

public class HelloService : IService
{
    public object Any(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}

不需要其他配置,该服务可以立即与REST一起使用:

肥皂 XML JSON 超文本标记语言

它还内置了一个友好的HTML输出(当使用具有Accept:text/ HTML的HTTP客户端调用时,例如浏览器),因此您能够更好地可视化您的服务输出。

处理不同的REST动词也很简单,这里有一个完整的REST服务CRUD应用程序,只需1页c#(比配置WCF还少;):

前端TODO应用程序 后端REST服务实现