ChatGPT解决这个技术问题 Extra ChatGPT

WCF 服务的 REST / SOAP 端点

我有一个 WCF 服务,我想将它作为 RESTfull 服务和 SOAP 服务公开。以前有人做过这样的事情吗?

好问题和好答案。

4
4 revs, 3 users 98%

您可以在两个不同的端点中公开服务。 SOAP 可以使用支持 SOAP 的绑定,例如 basicHttpBinding,RESTful 可以使用 webHttpBinding。我假设您的 REST 服务将采用 JSON 格式,在这种情况下,您需要使用以下行为配置来配置两个端点

<endpointBehaviors>
  <behavior name="jsonBehavior">
    <enableWebScript/>
  </behavior>
</endpointBehaviors>

您的场景中的端点配置示例是

<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="ITestService"/>
  </service>
</services>

因此,该服务将在

http://www.example.com/soap

http://www.example.com/json

将 [WebGet] 应用于操作合约,使其成为 RESTful。例如

public interface ITestService
{
   [OperationContract]
   [WebGet]
   string HelloWorld(string text)
}

注意,如果 REST 服务不是 JSON 格式,则操作的参数不能包含复杂类型。

回复 SOAP 和 RESTful POX(XML) 的帖子

对于作为返回格式的普通旧 XML,这是一个适用于 SOAP 和 XML 的示例。

[ServiceContract(Namespace = "http://test")]
public interface ITestService
{
    [OperationContract]
    [WebGet(UriTemplate = "accounts/{id}")]
    Account[] GetAccount(string id);
}

REST 普通旧 XML 的 POX 行为

<behavior name="poxBehavior">
  <webHttp/>
</behavior>

端点

<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>
  </service>
</services>

服务将在

http://www.example.com/soap

http://www.example.com/xml

REST 请求在浏览器中尝试,

http://www.example.com/xml/accounts/A123

添加服务引用后 SOAP 服务的 SOAP 请求客户端端点配置,

  <client>
    <endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
      contract="ITestService" name="BasicHttpBinding_ITestService" />
  </client>

在 C# 中

TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");

另一种方法是公开两个不同的服务合同,每个服务合同都有特定的配置。这可能会在代码级别生成一些重复项,但是在一天结束时,您希望使其正常工作。


当我将 .svc 托管在 IIS 中的某个虚拟目录(如 someserver/myvirtualdir/service.svc)中时,情况如何?我应该如何访问它?
我想更进一步,为 JSON 地址添加到 HTTPS 的绑定。我怎么做? stackoverflow.com/questions/18213472/…
当我尝试引用我的服务接口时,这表示我的合同 IEvents 无效: 。我的 IEvents 在界面上有一个 [ServiceContract] 属性,所以不知道为什么。
我可以让 localhost:44652/MyResource/json 工作,但我无法获得工作身份 localhost:44652/MyResource/98/json。我尝试添加“/{id}”的 UriTemplate,也尝试过“events/{id}”但是当我尝试访问服务时找不到它。只有第一个有效,不知道如何获得后者去工作。
它如何在没有物理文件的情况下工作?我似乎收到了 404 错误,一定是遗漏了什么
T
Tuomas Hietanen

这篇文章已经得到了“社区 wiki”的一个很好的回答,我也推荐看看 Rick Strahl 的网络博客,有很多关于 WCF Rest 的好文章,比如 this

我用两者来获得这种 MyService 服务......然后我可以使用来自 jQuery 的 REST 接口或来自 Java 的 SOAP。

这是来自我的 Web.Config:

<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-codebehind,不需要接口):

    /// <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){
    //...

我很想知道您认为通过添加这些 WebGet 和 WebInvoke 属性会获得什么好处。
您可以通过浏览器发出请求:localhost/MyService.svc/MyXmlResource/test 并明确说出格式 Json 或 Xml。如果您希望使用相同的方法来响应两者,这里有一个链接:blogs.msdn.com/dotnetinterop/archive/2008/11/04/…
这是出于测试目的。只是看看您的端点是否正常工作。你看过 SoapUI 吗? soapui.org
@TuomasHietanen - 我没有通过使用 webHttp 行为获得 JSON 类型的响应,但是使用 enableWebScript 我确实获得了 JSON 类型的响应。我确实将 ResponseFormat 设置为 WebMessageFormat.Json。另一方面,如果我使用 enableWebScript 行为,我将无法使用 URItemplate。有任何想法吗?
@CoffeeAddict - 为什么要使用接口?只是为了有接口?你永远不会重用这个接口。这更简单。
m
mythz

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

Hello World 示例展示了如何创建一个简单的 with 服务(无需配置):

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

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

处理不同的 REST 动词也很简单,这是一个完整的 REST 服务 CRUD 应用程序,位于 C# 的 1 页中(少于配置 WCF 所需的时间;):

前端 TODO 应用

后端 REST 服务实现


F
FMFF

MSDN现在似乎有一篇文章:

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

介绍:

默认情况下,Windows Communication Foundation (WCF) 使终结点仅对 SOAP 客户端可用。在如何:创建基本 WCF Web HTTP 服务中,端点可供非 SOAP 客户端使用。有时您可能希望以两种方式提供相同的合同,作为 Web 端点和作为 SOAP 端点。本主题显示了如何执行此操作的示例。


J
Jailson Evora

我们必须将行为配置定义到 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>

在服务的接口中,我们使用其属性定义操作。

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 system.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。


按预期工作正常
N
Nayas Subramanian

这就是我为使它工作所做的。确保将 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>