C# svcutil.exe或测试wcf服务JSON输出?
这篇文章是我上一篇文章的结尾(我想我应该开始一个新问题) 我正在尝试创建和测试一个输出JSON的简单(对你来说很简单而不是对我来说很简单)WCF web服务。我相信(在帮助下)这个应用程序是可以的,但我需要测试它,我不知道如何测试 我将在下面附加我的代码,但下面是发生的情况,如果我运行它加载的应用程序并获得“欢迎使用ASP.NET”页面,如果在.svc中运行应用程序,它加载并获得该页面: 您已经创建了一个服务 要测试此服务,您需要创建一个客户端并使用它来 打电话给服务部。您可以使用中的svcutil.exe工具来执行此操作 具有以下语法的命令行: svcutil.exe 有人告诉我可以看到JSON输出(见下面的代码),但我得到的只是: 找不到该网页 任何建议都会很好,我很抱歉现在对这一切都有点陌生,请你也许能比平时更详细地解释一下,哈哈 这是我的代码和坦克非常 Person.csC# svcutil.exe或测试wcf服务JSON输出?,c#,wcf,json,visual-studio-2010,C#,Wcf,Json,Visual Studio 2010,这篇文章是我上一篇文章的结尾(我想我应该开始一个新问题) 我正在尝试创建和测试一个输出JSON的简单(对你来说很简单而不是对我来说很简单)WCF web服务。我相信(在帮助下)这个应用程序是可以的,但我需要测试它,我不知道如何测试 我将在下面附加我的代码,但下面是发生的情况,如果我运行它加载的应用程序并获得“欢迎使用ASP.NET”页面,如果在.svc中运行应用程序,它加载并获得该页面: 您已经创建了一个服务 要测试此服务,您需要创建一个客户端并使用它来 打电话给服务部。您可以使用中的svcut
[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;
}
}
MyTestServices.svc.cs
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = `AspNetCompatibilityRequirementsMode.Allowed)]`
public class TestService
{
[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;
}
[ServiceContract(Namespace=”“)]
[AspNetCompatibilityRequirements(RequirementsMode=`AspNetCompatibilityRequirementsMode.Allowed)]`
公共类测试服务
{
[经营合同]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
公共列表GetResults()
{
列表结果=新列表();
结果:增加(新人员(“佩顿”、“曼宁”,35));
结果:增加(新的人(“Drew”,“Brees”,31));
结果:增加(新的人(“托尼”,“罗莫”,29));
返回结果;
}
MyTestService.svc
<%@ ServiceHost Language="C#"
Service="TestService.TestService"
Factory="System.ServiceModel.Activation.ServiceHostFactory" %>
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>
<bindings />
<client />
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
我始终使用调试请求。使用新的WCF Web Api,您可以将客户端的accept标头设置为application/JSON,并且响应将被格式化为JSON。我始终使用调试请求。使用新的WCF Web Api,您可以将客户端的accept标头设置为application/JSON,并且响应将是表单尝试为JSON。我看不到您的接口,但是如果您希望它是rest,那么您缺少了一些东西
//Add this to your method
[WebGet(UriTemplate="/Results")]
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;
}
//将其添加到方法中
[WebGet(UriTemplate=“/Results”)]
公共列表GetResults()
{
列表结果=新列表();
结果:增加(新人员(“佩顿”、“曼宁”,35));
结果:增加(新的人(“Drew”,“Brees”,31));
结果:增加(新的人(“托尼”,“罗莫”,29));
返回结果;
}
现在您应该可以使用调用它了。只需将uri临时更改为您想要的
您可能还需要将工厂更改为WebServiceHostFactory
<%@ ServiceHost Service="TestService.MyTestService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
我看不到您的界面,但如果您希望它是rest,您缺少一些东西
//Add this to your method
[WebGet(UriTemplate="/Results")]
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;
}
//将其添加到方法中
[WebGet(UriTemplate=“/Results”)]
公共列表GetResults()
{
列表结果=新列表();
结果:增加(新人员(“佩顿”、“曼宁”,35));
结果:增加(新的人(“Drew”,“Brees”,31));
结果:增加(新的人(“托尼”,“罗莫”,29));
返回结果;
}
现在您应该可以使用调用它了。只需将uri临时更改为您想要的
您可能还需要将工厂更改为WebServiceHostFactory
<%@ ServiceHost Service="TestService.MyTestService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
我不是WCF专家,因此我无法告诉您当前应用程序的错误,但我可以指导您使用代码设置一个非常简单的示例,该示例将在Fiddler中返回结果
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WebApplication1
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
List<Person> GetResults();
}
public class TestService : ITestService
{
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;
}
}
[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;
}
}
}
您不需要.svc文件,因此启动应用程序,记下ASP.NET开发服务器使用的根url。在fiddler的“请求生成器”选项卡上输入您的url:
单击“执行”按钮,您将看到您的服务结果:
希望您能利用此信息了解需要更新哪些服务才能使其运行。祝您好运!我不是WCF专家,因此我无法告诉您当前应用程序的错误,但我可以指导您使用代码设置一个非常简单的示例,该示例将在Fiddler中返回结果
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WebApplication1
{
[ServiceContract]
public interface ITestService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
List<Person> GetResults();
}
public class TestService : ITestService
{
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;
}
}
[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;
}
}
}
您不需要.svc文件,因此启动应用程序,记下ASP.NET开发服务器使用的根url。在fiddler的“请求生成器”选项卡上输入您的url:
单击“执行”按钮,您将看到您的服务结果:
希望您能用它来找出需要更新哪些服务才能使其运行。祝您好运!您想构建REST服务、SOAP服务还是两者都要?如果您还不确定,这可能会有所帮助。呃,好问题,REST我相信您想构建REST服务、SOAP服务还是两者都要?如果您还不确定,这是ight help.erm好问题,REST我相信这里是fiddler文档的链接。对此很抱歉。为了能够检查来回发送的http数据包,我使用了一个名为fiddler的http调试器,它充当代理,因此对你的应用程序是透明的。很酷,它允许更改请求并将它们重新发送到你的服务。这是允许您更改,例如,您的客户端通过accept标头通知服务器接受的返回类型,因此我们喜欢JSON。文档告诉您有关设置和使用的所有信息。Thnx Jamie