WCF服务仅在json响应的情况下将localhost转换为www.localhost.com

本文关键字:localhost 转换 com www 情况下 服务 json 响应 WCF | 更新日期: 2023-09-27 17:50:45

我创建了一个简单的wcf服务,它有3个方法。这些方法中的每一个都可以在单个[WebGet]属性的情况下工作,但是如果我添加(ResponseFormat = WebMessageFormat.Json),它将本地主机转换为www.localhost.com地址。

这是我的配置:

<?xml version="1.0"?>
<configuration>
  <appSettings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <useRequestHeadersForMetadataAddress />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <services>
      <service name="PromoCenyParseService.PromoCenyParseService">
      <endpoint kind="webHttpEndpoint"
       contract="PromoCenyParseService.IPromoCenyParseService" />
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

服务接口:

[ServiceContract]
public interface IPromoCenyParseService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    Product ParseItem(string url);
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    List<Product> ParsePage(string urlPage);
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    int GetNumOfPages(string urlPage);
}

你能告诉我我做错了什么吗?

更新:

添加终点,无结果。仍在处理xml,未处理json

更新2:

我发现这个问题只存在于函数:

ParseItem

ParsePage

函数返回正常值。

我已经尝试了各种端点配置等

问题在哪里?

更新3:

我已经发现,它只发生在函数返回对象是DataContract

WCF服务仅在json响应的情况下将localhost转换为www.localhost.com

我发现了一个问题。

DateTime对象存在问题,该对象存在于我的数据合约中。我想我的服务器上有不同的时区或不同的默认日期格式。

这个问题的解决方案:

[DataMember]
public DateTime DiscountStart
{
    get { return this._discountStart.ToUniversalTime(); }
    set { this._discountStart = value; }
}

试试这个:

<?xml version="1.0"?>
<configuration>
  <appSettings/>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <useRequestHeadersForMetadataAddress />
        </behavior>
      </serviceBehaviors>
    </behaviors>
   <!-- <protocolMapping> I think you don't need this
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping> -->
    <services>
      <service name="PromoCenyParseService.PromoCenyParseService">
      <endpoint address="localohost"
                binding="basicHttpBinding"
                contract="PromoCenyParseService.IPromoCenyParseService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

如果这仍然不起作用,打开浏览器,转到"http://localhost?wsdl "并在这里发布结果。