Web服务返回空白页而不是JSON对象

时间:2012-08-23 14:43:46

标签: c# .net json wcf

我第一次被要求开发WCF Web服务作为项目。 Web服务相当简单,它应该只返回一个JSON对象。 问题是浏览器(chrome,f irefox)在尝试使用浏览器测试服务时显示空白页面。 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;
        }  
    }
}

2 个答案:

答案 0 :(得分:1)

尝试以下方法:

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。 (如果我是对的)

答案 1 :(得分:1)

要在webbrowser中使用,您需要实现webHttpBinding而不是wsHttpBinding,或者与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> 

也在Config中:

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