3

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

附件是我的代码和 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; }
    }
}

服务1.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;
        }  
    }
}
4

2 回答 2

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

于 2012-08-23T15:02:45.010 回答
1

要在 webbrowser 中使用,您需要实现 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> 
于 2012-08-23T15:02:59.970 回答