[英]svcutil.exe or testing wcf service JSON ouput?
这篇文章是我最后一个问题的背面(我想我会开始一个新问题)。
我正在尝试创建和测试一个简单的(简单到你不是我大声笑)WCF Web服务输出JSON。 我相信(有帮助)应用程序是好的,但我需要测试它,我不知道如何。
我将附上我的代码,但是这里发生了什么,如果我运行应用程序它加载http:// localhost:52002 /我欢迎来到ASP.NET页面,如果我在我的时候运行应用程序。 svc它加载http:// localhost:52002 / MyTestService.svc ,我得到页面:
您已创建了一项服务。
要测试此服务,您需要创建一个客户端并使用它来调用该服务。 您可以使用命令行中的svcutil.exe工具执行此操作,语法如下:
svcutil.exe http:// localhost:52002 / MyTestService.svc?wsdl
我被告知我可以http:// localhost:52002 / MyTestService.svc / GetResults查看JSON输出(参见下面的代码),但我得到的是:
无法找到该网页
任何建议都会很棒,我现在为所有这些都是新的道歉,并请你告诉你可能会把事情拼出来,然后你通常会大声笑
这是我的代码和坦克非常多
Person.cs
[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;
}
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>
我总是使用fiddler来调试请求。 使用新的Web Api for WCF,您可以将客户端的accept标头设置为application / JSON,响应将格式化为JSON。
我没有看到你的界面,但如果你想让它休息,你会错过一些东西。
//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;
}
现在您应该可以使用http:// localhost:52002 / MyTestService.svc / Results来调用它。 只需将uri tempalte改为你想要的那样。
您可能还需要将工厂更改为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;
}
}
}
更新web.config
文件:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="test.svc"
service="WebApplication1.TestService"
factory="System.ServiceModel.Activation.WebServiceHostFactory" />
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
</configuration>
您不需要.svc文件,因此启动应用程序,记下ASP.NET Development Server使用的根URL。 在fiddler的Request Builder选项卡上输入您的URL:
单击执行按钮,您应该看到您的服务结果:
希望您可以使用它来确定您需要更新服务以使其运行。 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.