Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF服务仅在json响应的情况下将localhost转换为www.localhost.com_C#_Json_Wcf - Fatal编程技术网

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

C# WCF服务仅在json响应的情况下将localhost转换为www.localhost.com,c#,json,wcf,C#,Json,Wcf,我创建了简单的wcf服务,它有3种方法。这些方法中的每个ov都在单个[WebGet]属性的情况下工作,但如果我添加(ResponseFormat=WebMessageFormat.Json),它将本地主机转换为www.localhost.com地址 这是我的配置: <?xml version="1.0"?> <configuration> <appSettings/> <system.web> <compilation deb

我创建了简单的wcf服务,它有3种方法。这些方法中的每个ov都在单个[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>

此函数返回正常值

我尝试过各种端点配置等

问题在哪里

更新3:


我发现,只有当函数返回的对象是DataContract时,才会发生这种情况。

尝试以下方法:

<?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
“并将结果发布在此处。

我发现了问题

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

解决这个问题:

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

您不需要在配置中定义端点吗?在这里,您可以将主机名设置为localhostI发现,只有当函数返回DataContract对象时才会发生这种情况。现在有什么解决方案吗?您真的添加了端点标记的address和baseaddress属性了吗?上面的代码没有反映这一点。您的代码或配置中是否有使用字符串“localhost”的地方?如果是,请在你的问题中显示。我甚至复制粘贴了你的配置。它打破了一切:P现在,我有一个基于webHttpBinding的完全工作的xml响应服务。localhost和的问题仍然存在。可以在webHttpBinding上做出Json响应吗?是的,我做到了。虽然我使用了restful行为,但它仍然重定向到www.localhost.com,但奇怪的是,它只在Json响应的情况下重定向。还有别的解决办法吗?
ParsePage
<?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>
[DataMember]
public DateTime DiscountStart
{
    get { return this._discountStart.ToUniversalTime(); }
    set { this._discountStart = value; }
}