wcf - 两个端点(soap、json)和一个服务方法

标签 wcf json soap asp.net-4.0

我有这项服务

        [OperationContract]
    [WebGet(UriTemplate = "/GetData")]
    List<FieldInfo> GetSerializedData();

和 web.config

<system.web>
    <webServices>
      <protocols>
        <add name="HttpGet" />
        <add name="HttpPost" />
      </protocols>
    </webServices>
    <httpRuntime  executionTimeout="90" maxRequestLength="1048576" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100"/>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingSettings" maxBufferPoolSize="524288" maxReceivedMessageSize="654321" sendTimeout="00:10:00" closeTimeout="00:01:00" openTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </webHttpBinding>
      <wsHttpBinding>
        <binding name="wsHttpBindingSettings" maxBufferPoolSize="524288" maxReceivedMessageSize="654321" sendTimeout="00:10:00" closeTimeout="00:01:00" openTimeout="00:10:00" receiveTimeout="00:10:00">
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="MetadataBehavior" name="ServiceModel.Service">
        <endpoint name="soap" address="soap" behaviorConfiguration="Default" binding="wsHttpBinding"
          bindingConfiguration="wsHttpBindingSettings" contract="ServiceModel.IService" />
        <endpoint name="Json" address="json" behaviorConfiguration="JSON" binding="webHttpBinding"
        bindingConfiguration="webHttpBindingSettings" contract="ServiceModel.IService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://service.com/Service.svc/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MetadataBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="JSON">
          <webHttp automaticFormatSelectionEnabled="true"/>
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
        </behavior>
        <behavior name="Default">
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

为什么在客户端只生成一个端点?

    <client>
  <endpoint address="http://service.renault.com/Service.svc/soap"
    binding="wsHttpBinding" bindingConfiguration="soap" contract="ServiceReference1.IService"
    name="soap" />
</client>

我的观点是从 asp.net 页面代码隐藏中执行服务方法,并且 wcf 返回 SOAP 或 json 中的数据取决于 ContentType。但是当asp.net页面有text/html内容时,如何将其内容类型设置为application/json。我无法理解它。

最佳答案

Why on the client side only one endpoint is generated?

因为 WCF 不发出非 SOAP 端点的元数据。与 SOAP 的 WSDL 和 MEX 不同,“REST”端点没有广泛使用的元数据格式(WADL 是其中之一,但使用不多且未由 WCF 实现),因此在添加服务引用(或 svcutil)上只会看到元数据中只有一个端点,并且只会创建该端点。

I want to use WCF feature which select proper serialization type depends on ContentType of request

JSON 与 XML 是序列化类型的决定; JSON 与 SOAP 不是(SOAP 是一个定义明确的协议(protocol),具有请求应是什么样子的规则) - 请参阅 WCF Dynamic Response Format 的更多信息。您的 webHttBinding 端点将执行此操作(根据传入请求返回 JSON 或 XML),因为您启用了自动格式选择,但您使用此服务的方式不需要使用WCF 客户端 - 使用 WebClientHttpWebRequest 应该可以正常工作。

关于wcf - 两个端点(soap、json)和一个服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359759/

相关文章:

数组上的 C# OutOfMemoryException

c# - WCF 错误 : 'It is likely that certificate ' my cert' may not have a private key that is capable of key exchange

php - Opencart:Ajax json响应未知字符

php - 从使用 php 构建的 Web 服务中获取空响应

soap - 返回错误的 JAX-WS 服务器端 SOAPHandler 在 WebSphere v8 上获取 "Internal Error"

php - SoapClient 到 nusoap,Server 没有识别 HTTP Header SOAPAction 的值

c# - WCF 中的回调?

wcf - 从控制台应用程序通过 HTTPS/SSL 使用 WCF 服务

c# - C# 中的 Foreach Json 对象

jquery - 如何在 JQuery $.each 函数中编辑全局变量?