c# - svcutil.exe 或测试 wcf 服务 JSON 输出?

标签 c# wcf json visual-studio-2010

这篇文章是我最后一篇 question 的文章(以为我会开始一个新问题)。

我正在尝试创建和测试一个输出 JSON 的简单(对你来说很简单,对我来说很简单)WCF Web 服务。我相信(在帮助下)应用程序没问题,但我需要对其进行测试,但我不确定如何进行测试。

我将在下面附上我的代码,但这是发生了什么,如果我运行应用程序,它会加载 http://localhost:52002/如果在我的 .svc 中运行应用程序,我会收到 ASP.NET 页面的欢迎,它会加载 http://localhost:52002/MyTestService.svc我得到了页面:

You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

svcutil.exe http://localhost:52002/MyTestService.svc?wsdl

有人告诉我可以 http://localhost:52002/MyTestService.svc/GetResults查看 JSON 输出(参见下面的代码)但我得到的是:

The webpage cannot be found

任何建议都很好,我很抱歉现在对这一切有点陌生,请你把事情拼写得比平时多一点,哈哈

下面是我的代码和坦克非常多

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调试请求。使用新的 WCF Web Api,您可以将客户端的接受 header 设置为 application/JSON,响应将被格式化为 JSON。

关于c# - svcutil.exe 或测试 wcf 服务 JSON 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7851616/

相关文章:

c# - 为什么在我的 WebClient DownloadFileAsync 方法中下载空文件?

WCF错误: Relative end point addresses

c# - WCF 与 SOAP HTTPS 和双工绑定(bind)

javascript - 同时为每个div添加不同的变量

ios - 在 iOS 中使用扭曲的 JSON 调用 Web 服务

c# - 如何在 C# 方法中返回对象和操作结果

c# - WPF MediaElement如何从暂停处继续播放?

c# - 为什么 typeof(Object[,][]).Name 等于 "Object[][,]"?

c# - 如何通过 MSMQ(使用 WCF)发送 XDocument?

java - 使用 Jackson 反序列化 JSON 时去除空格