Gentelmens,
我正在使用WCF服务,我希望将结果视为JSON。 但我对如何做到这一点没有任何想法。我已将方法标记为
[WebGet(ResponseFormat = WebMessageFormat.Json)]
但这对我没有帮助。在“Wcf测试客户端”中,我将结果视为xml。
我试图将行为(带有Json的WebHttp)添加到端点但我有错误:
The endpoint at 'http://localhost:8732/Design_Time_Addresses/PlayingWithWCF2/Service1/' does not have a Binding
with the None MessageVersion. 'System.ServiceModel.Description.WebHttpBehavior' is only intended for use
with WebHttpBinding or similar bindings.
DataContract:
[DataContract]
public class Foo
{
[DataMember]
public string Name { get; set; }
}
接口:
[ServiceContract]
public interface IFooService
{
[OperationContract]
List<Foo> GetList();
}
服务:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class FooSerivce : IFooService
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public List<Foo> GetList()
{
return new List<Foo> {new Foo {Name = "Name1"}, new Foo {Name = "Name2"}};
}
}
的Web.config:
<system.serviceModel>
<services>
<service name="PlayingWithWCF2.FooSerivce">
<endpoint address="" behaviorConfiguration="" binding="wsHttpBinding"
contract="PlayingWithWCF2.IFooService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/PlayingWithWCF2/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
你能给我一些建议我如何解决这个问题吗?
P.S。对不起,如果问题很愚蠢,但我是WCF的新手
答案 0 :(得分:3)
要返回JSON,您必须使用WCF测试客户端不支持的REST服务。您已定义服务以支持REST,但未将其配置为公开REST端点。您需要此配置:
<system.serviceModel>
<services>
<service name="PlayingWithWCF2.FooSerivce">
<endpoint address="webHttp" binding="webHttpBinding" contract="PlayingWithWCF2.IFooService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/PlayingWithWCF2/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
答案 1 :(得分:0)
我相信您的问题在于您的web.config,您的绑定配置应该设置为webHttp。 在stackoverflow上查看以下question/answer