【问题标题】:Web service returns blank page instead of JSON objectWeb 服务返回空白页而不是 JSON 对象
【发布时间】:2012-08-23 14:43:45
【问题描述】:

我第一次被要求开发一个 WCF Web 服务作为一个项目。 Web 服务相当简单,它应该只返回一个 JSON 对象。 问题是浏览器(chrome,firefox)在尝试使用浏览器测试服务时显示空白页面。 WcfTestClient 正确显示 JSON 输出。

附上我的代码和 Web.config

那我做错了什么? 提前致谢。

Web.config:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="WcfService3.Service1" behaviorConfiguration="WcfService3.Service1Behavior">
        <!-- Service Endpoints -->
        <endpoint address="" binding="wsHttpBinding" contract="WcfService3.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService3.Service1Behavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

IService1.cs:

namespace WcfService3
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(string username, string password);
    }

    [DataContract]
    public class Data
    {
        [DataMember]
        public string Username { get; set; }

        [DataMember]
        public string Password { get; set; }
    }
}

Service1.svc.cs:

namespace WcfService3
{
    public class Service1 : IService1
    {
        [WebInvoke(Method = "GET", 
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "data/{user}/{pass}")]
        public string GetData(string user, string pass)
        {
            Data UserData = new Data()
            {
                Username = user,
                Password = pass
            };

            MemoryStream stream = new MemoryStream();
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Data));
            ser.WriteObject(stream, UserData);
            string json = Encoding.Default.GetString(stream.ToArray());
            return json;
        }  
    }
}

【问题讨论】:

    标签: c# .net json wcf


    【解决方案1】:

    要在网络浏览器中使用,您需要实现 webHttpBinding 而不是,或者与 wsHttpBinding 一起实现。

    <services> 
      <service name="WcfService3.Service1" behaviorConfiguration="WcfService3.Service1Behavior"> 
        <!-- Service Endpoints --> 
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="WcfService3.IService1"> 
          <identity> 
            <dns value="localhost"/> 
          </identity> 
        </endpoint> 
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
      </service> 
    </services> 
    

    也在配置中:

      <endpointBehaviors> 
        <behavior name="webBehavior"> 
          <webHttp /> 
        </behavior> 
      </endpointBehaviors> 
    

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      namespace WcfService3
      {
          public class Service1 : IService1
          {
              [WebInvoke(Method = "GET", 
              RequestFormat = WebMessageFormat.Json,
              ResponseFormat = WebMessageFormat.Json,
              UriTemplate = "data/{user}/{pass}")]
              public Data GetData(string user, string pass)
              {
                  Data UserData = new Data()
                  {
                      Username = user,
                      Password = pass
                  };
      
                  return UserData;
              }  
          }
      }
      

      您将响应格式定义为 Json,因此 Wcf 服务会将您的返回对象转换为 Json。 (如果我是正确的)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 2012-02-26
        • 2023-04-05
        • 2021-04-05
        • 2020-10-28
        • 2012-06-20
        相关资源
        最近更新 更多