C# 如何使json WCF正常工作
我已经挣扎了很多时间,花了很多时间,但无法让它工作,花了几个小时后,现在我能够看到元数据,但无法成功调用该操作。下面是步骤和代码C# 如何使json WCF正常工作,c#,json,wcf,.net-4.0,wcf-rest,C#,Json,Wcf,.net 4.0,Wcf Rest,我已经挣扎了很多时间,花了很多时间,但无法让它工作,花了几个小时后,现在我能够看到元数据,但无法成功调用该操作。下面是步骤和代码 创建WCF服务库项目 现在输入代码 服务合同和数据合同 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.W
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace JsonWCFTest
{
[ServiceContract]
public interface IJsonService
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "data/{id}")]
Product GetProduct(string id);
}
[DataContract(Name = "product")]
public class Product
{
[DataMember(Name = "id")]
public string Id { get; set; }
}
}
服务。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace JsonWCFTest
{
public class JsonService:IJsonService
{
public Product GetProduct(string id)
{
return new Product {Id = " you have entered " + id};
}
}
}
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="service.svc" service="JsonWCFTest.JsonService"/>
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="JsonWCFTest.JsonService" behaviorConfiguration="jsonTestServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost" />
</baseAddresses>
</host>
<endpoint address="jsonTestEndPoint" behaviorConfiguration="jsonTestEndPointBehavior"
binding="webHttpBinding" contract="JsonWCFTest.IJsonService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="jsonTestServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonTestEndPointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
当我使用此url时
http://localhost/service.svc/data/1
它在internet explorer中给了我以下错误
“/”应用程序中出现服务器错误。找不到资源。
描述:HTTP404。您正在寻找的资源(或其
依赖项)可能已被删除、名称已更改或
暂时不可用。请查看下面的URL并进行修改
确保它拼写正确
请求的URL:/service.svc/data/1
您连接到了错误的地址。使用已设置的配置文件,您应该连接到
http://localhost/jsonTestEndPoint/data/1
您需要使用BaseAddress
+EndpointAddress
+FunctionAddress
来获取完整的url
我可能有点生疏,如果上面的地址不起作用,那么该地址将被删除
http://localhost/service.svc/jsonTestEndPoint/data/1
您连接到了错误的地址。使用已设置的配置文件,您应该连接到
http://localhost/jsonTestEndPoint/data/1
您需要使用BaseAddress
+EndpointAddress
+FunctionAddress
来获取完整的url
我可能有点生疏,如果上面的地址不起作用,那么该地址将被删除
http://localhost/service.svc/jsonTestEndPoint/data/1
我很好奇你为什么要用WebInvoke来获取信息?为什么不使用WebGet()并将WebMessageFormat设置为Json)?@Mark B,我不知道,因为我正在进行一次演练,所以我必须对此进行研究。如果你能提出建议,请告诉我最好的做法。我想你总是使用WebGet()来处理所有GET请求。将WebInvoke()用于所有其他HTTP“动词”,如POST、DELETE或PUT。您仍然可以使用JSONI的ResponseFormat属性。我很好奇为什么要使用WebInvoke作为GET?为什么不使用WebGet()并将WebMessageFormat设置为Json)?@Mark B,我不知道,因为我正在进行一次演练,所以我必须对此进行研究。如果你能提出建议,请告诉我最好的做法。我想你总是使用WebGet()来处理所有GET请求。将WebInvoke()用于所有其他HTTP“动词”,如POST、DELETE或PUT。您仍然可以使用JsonTanks的ResponseFormat属性来获得帮助。第二个建议链接有效,但还是一个v小问题,我如何在我的Win forms应用程序或Windows Phone mango应用程序中使用此链接如果这不能回答您的问题,您可能需要打开一个新问题,而只是使用类似或的类来“访问”网页,页面返回的结果是JSON,您可以使用自己选择的JSON库对其进行解析。您正在做的工作的术语是(请看“像URI一样公开目录结构”,看起来很熟悉吗?)非常感谢您的帮助。第二个建议链接有效,但还是一个v小问题,我如何在我的Win forms应用程序或Windows Phone mango应用程序中使用此链接如果这不能回答您的问题,您可能需要打开一个新问题,而只是使用类似或的类来“访问”网页,页面返回的结果是JSON,您可以使用自己选择的JSON库解析它