我有这项服务
[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 客户端 - 使用 WebClient
、HttpWebRequest
应该可以正常工作。
关于wcf - 两个端点(soap、json)和一个服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7359759/