WCF JSON实现

WCF JSON实现,json,wcf,Json,Wcf,我试图实现一个返回JSON的非常简单的WCF服务。我已经试了6个小时了,但还是不行。我希望你能帮我解决这个问题 人 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; namespace Tcf.AtX.Services { [DataContract] public class Pers

我试图实现一个返回JSON的非常简单的WCF服务。我已经试了6个小时了,但还是不行。我希望你能帮我解决这个问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Tcf.AtX.Services
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tcf.AtX.Broadcasting;

namespace Tcf.AtX.Services
{
    public class BroadcastService : IBroadcastService
    {
        /// <summary>
        /// Broadcasts the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Person Broadcast(string message)
        {
            return new Person() { Name = message };
        }
    }
}
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Tcf.AtX.Services.BroadcastService">
        <endpoint address="" binding="webHttpBinding" contract="Tcf.AtX.Services.IBroadcastService" behaviorConfiguration="json"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/Tcf.AtX.Services/BroadcastService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="json">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
服务合同

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace Tcf.AtX.Services
{
    [ServiceContract]
    public interface IBroadcastService
    {
        /// <summary>
        /// Broadcasts the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(Method="GET", ResponseFormat= WebMessageFormat.Json)]
        Person Broadcast(string message);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.ServiceModel;
使用System.ServiceModel.Web;
命名空间Tcf.AtX.Services
{
[服务合同]
公共接口IBroadcastService
{
/// 
///广播指定的消息。
/// 
///信息。
/// 
[经营合同]
[WebInvoke(Method=“GET”,ResponseFormat=WebMessageFormat.Json)]
个人广播(字符串消息);
}
}
服务实施

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Tcf.AtX.Services
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tcf.AtX.Broadcasting;

namespace Tcf.AtX.Services
{
    public class BroadcastService : IBroadcastService
    {
        /// <summary>
        /// Broadcasts the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Person Broadcast(string message)
        {
            return new Person() { Name = message };
        }
    }
}
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Tcf.AtX.Services.BroadcastService">
        <endpoint address="" binding="webHttpBinding" contract="Tcf.AtX.Services.IBroadcastService" behaviorConfiguration="json"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/Tcf.AtX.Services/BroadcastService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="json">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Tcf.AtX.Broadcasting;
命名空间Tcf.AtX.Services
{
公共类广播服务:IBroadcastService
{
/// 
///广播指定的消息。
/// 
///信息。
/// 
公众广播(字符串消息)
{
返回newperson(){Name=message};
}
}
}
配置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Tcf.AtX.Services
{
    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tcf.AtX.Broadcasting;

namespace Tcf.AtX.Services
{
    public class BroadcastService : IBroadcastService
    {
        /// <summary>
        /// Broadcasts the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public Person Broadcast(string message)
        {
            return new Person() { Name = message };
        }
    }
}
<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Tcf.AtX.Services.BroadcastService">
        <endpoint address="" binding="webHttpBinding" contract="Tcf.AtX.Services.IBroadcastService" behaviorConfiguration="json"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/Tcf.AtX.Services/BroadcastService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="json">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

问题是我在测试客户机中看不到服务,因此无法测试我的方法。 我自己也编写了一个测试客户机,但是当我将我的服务引用到我的项目时,我无法创建它的实例

有人能给我解释一下我做错了什么吗

致以最良好的祝愿


Rob

测试客户端不适用于非SOAP端点(即,您拥有的使用WebHttpBinding的端点)。试着简单地创建一个程序,尝试调用您的操作,类似下面的代码

WebClient c = new WebClient();
Console.WriteLine(
    c.DownloadString(
        "http://localhost:8732/Design_Time_Addresses/Tcf.AtX.Services/Broadcast?message=MyMessage"));

还有一件事,您需要将
[WebInvoke(Method=“GET”)]
属性更改为
[WebGet]

好的,我会尝试一下。添加web引用不起作用。我只获得名称空间,无法创建serviceYes的实例,原因与测试客户端不工作相同。非SOAP端点不公开它们的元数据,因此这些工具(测试客户端、添加引用)不知道如何使用这些端点!我想知道。。。。我需要一个Web服务,这样我就可以在Mono for Android应用程序中使用它,但这是首选的方法吗?没有“正确”或“错误”类型的服务/端点,这取决于客户端的工具和您的偏好。如果您有一个SOAP堆栈,并且喜欢有一个类型化代理,那么使用SOAP端点是一个不错的选择。如果您想要“纯”HTTP(较小的消息),不想要代理类的开销,或者客户机中没有好的SOAP库,那么使用JSON(或POX-Plain Old XML)端点是一个更好的选择。我知道解决这个问题没有正确或错误的方法,因为这取决于具体情况。我会仔细考虑并决定使用哪个实现。谢谢你的回复。