问题描述
我有这项服务
[OperationContract]
[WebGet(UriTemplate = "/GetData")]
List<FieldInfo> GetSerializedData();
和web.config
and 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>
为什么在客户端仅生成一个端点?
Why on the client side only one endpoint is generated ?
<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.但是当它具有text/html内容时,如何在asp.net页面内容类型中将其设置为application/json.我对理解有疑问.
My point is to execute service method from asp.net page codebehind and wcf return data in soap or json depends on ContentType. But how to set in asp.net page content type to application/json when it have text/html content. I have problem with understand it.
推荐答案
因为WCF不会为非SOAP端点发出元数据.与WSDL和MEX for SOAP不同,"REST"端点没有广泛使用的元数据格式(WADL是其中一种,但WCF使用率不高,并且未由WCF实施),因此在添加服务参考"(或svcutil)上只能看到元数据中有一个端点,只有一个将被创建.
Because WCF does not emit metadata for non-SOAP endpoints. Unlike WSDL and MEX for SOAP, there's no widely used metadata format for "REST" endpoints (WADL is one of them, but it's not much used and not implemented by WCF), so on the Add Service Reference (or svcutil) will only see one endpoint in the metadata and only that one will be created.
JSON与XML是序列化类型的决定; JSON和SOAP并不是JSON(SOAP是一个定义明确的协议,其中包含有关请求外观的规则)-请参见 WCF动态响应格式.您的webHttBinding
端点将执行此操作(根据传入的请求返回JSON或XML),因为您启用了自动格式选择,但是使用此服务的方式不需要与WCF客户端一起使用-使用WebClient
,HttpWebRequest
应该可以正常工作.
JSON vs XML is a serialization type decision; JSON vs SOAP is not (SOAP is a well-defined protocol, with rules for what the request should look like) - see more information on WCF Dynamic Response Format. Your webHttBinding
-endpoint will do that (return JSON or XML based on the incoming request), since you enabled auto format selection, but the way you'd consume this service doesn't need to be with a WCF client - using WebClient
, HttpWebRequest
should work out just fine.
这篇关于两个端点(soap,json)和一个服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!