发出JSON的WCF Web服务(Noob建议) [英] WCF Web Service that emits JSON (Noob advice)
问题描述
我对C#,XAML,Visual Studio等世界相当了解,我需要一点帮助来了解如何创建一个可以发出JSON的简单WCF Web应用程序,我也想知道如何进行测试该应用程序的输出.
I'm pretty knew to the world of C#, XAML, Visual Studio etc and i need abit of help to understand how i can create a simple WCF web application that emits JSON, i'd also like to know how to test the output of that application aswell.
我一直在寻找并跟踪几个不同的博客,但到目前为止还没有运气.我在此找到了另一条帖子,但这还是我的尝试失败大声笑
I've been hunting around and followed a couple of different blogs but had no luck so far. I found another post on here about this, but again my attempts fail lol
我认为我现在正处于一个非常困惑的境地,通过查看太多不同的示例,并需要这个出色网站的帮助:)
I think i am now at the point where i have just confused myself totally by looking at too many different examples and need some help from this great site :)
我将尽力为您提供尽可能多的代码,希望我已经做了一件可以挽救的帽子!
I'll try and provide you with as much code as i have already and hopefully i've done somethignt hat can be rescued!!
在此先感谢您的帮助!
Person.cs
Person.cs
namespace TestService
{
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public int Age { get; set; }
public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
}
}
}
MyTestService.svc.cs
MyTestService.svc.cs
namespace TestService
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TestService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string GetResults()
{
List<Person> results = new List<Person>();
results.Add(new Person("Peyton", "Manning", 35));
results.Add(new Person("Drew", "Brees", 31));
results.Add(new Person("Tony", "Romo", 29));
// Serialize the results as JSON
DataContractJsonSerializer serializer = new DataContractJsonSerializer(results.GetType());
MemoryStream memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, results);
// Return the results serialized as JSON
string json = Encoding.Default.GetString(memoryStream.ToArray());
return json;
}
}
MyTestService.svc
MyTestService.svc
<%@ ServiceHost Language="C#"
Service="TestService.MyTestService"
Factory="System.ServiceModel.Activation.ServiceHostFactory" %>
web.config
web.config
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
推荐答案
您将不需要序列化要返回的对象,.NET将在后台为您处理所有这一切.
You will not need to serialize the object you wish to return, .NET will handle this all for your behind the scenes.
像这样修改GetResults
方法:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public List<Person> GetResults()
{
List<Person> results = new List<Person>();
results.Add(new Person("Peyton", "Manning", 35));
results.Add(new Person("Drew", "Brees", 31));
results.Add(new Person("Tony", "Romo", 29));
return results;
}
希望这会有所帮助.
这篇关于发出JSON的WCF Web服务(Noob建议)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!