简体   繁体   English

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

[英]svcutil.exe or testing wcf service JSON ouput?

this post is off the back of my last question (thought i'd start a new question). 这篇文章是我最后一个问题的背面(我想我会开始一个新问题)。

i am trying to create and test a simple (simple to you not me lol) WCF web service that outputs JSON. 我正在尝试创建和测试一个简单的(简单到你不是我大声笑)WCF Web服务输出JSON。 I beleive (with help) the application is ok but i need to test it and ia not sure how. 我相信(有帮助)应用程序是好的,但我需要测试它,我不知道如何。

I'll attach my code below but here is what is happening, if i run the application it loads http://localhost:52002/ and i get the welcome to ASP.NET page, if run the application while i am in my .svc it loads http://localhost:52002/MyTestService.svc and i get the page: 我将附上我的代码,但是这里发生了什么,如果我运行应用程序它加载http:// localhost:52002 /我欢迎来到ASP.NET页面,如果我在我的时候运行应用程序。 svc它加载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工具执行此操作,语法如下:

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

I have been told i can http://localhost:52002/MyTestService.svc/GetResults to see the JSON output (see code below) but all i get is: 我被告知我可以http:// localhost:52002 / MyTestService.svc / GetResults查看JSON输出(参见下面的代码),但我得到的是:

The webpage cannot be found 无法找到该网页

Any suggestions would be great, i apologies now for being abit new to all this and please ask you to maybe spell things out abit more then you normally would lol 任何建议都会很棒,我现在为所有这些都是新的道歉,并请你告诉你可能会把事情拼出来,然后你通常会大声笑

Here is my code and tanks very much 这是我的代码和坦克非常多

Person.cs 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 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 MyTestService.svc

<%@ ServiceHost Language="C#"
    Service="TestService.TestService"
    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>
    <bindings />
    <client />
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

I always use fiddler to debug the request. 我总是使用fiddler来调试请求。 With the new Web Api for WCF you can set the accept header for the client to application/JSON and the response will be formatted as JSON. 使用新的Web Api for WCF,您可以将客户端的accept标头设置为application / JSON,响应将格式化为JSON。

I do not see your interface, but you are missing some stuff if you want it to be 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;
}

Now you should be able to call it using http://localhost:52002/MyTestService.svc/Results . 现在您应该可以使用http:// localhost:52002 / MyTestService.svc / Results来调用它。 Just change the uri tempalte to what you want it to be. 只需将uri tempalte改为你想要的那样。

You may also need to change the factory to be the WebServiceHostFactory. 您可能还需要将工厂更改为WebServiceHostFactory。

<%@ ServiceHost Service="TestService.MyTestService"
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

I am not a WCF expert, so I cannot tell you what is wrong with your current application, but I can give you instructions for setting up a very simple example using your code, which will return a result in Fiddler. 我不是WCF专家,所以我不能告诉你当前应用程序有什么问题,但我可以给你一些使用你的代码设置一个非常简单的例子的说明,这将在Fiddler中返回一个结果。

  1. Create a new ASP.NET Empty Web Application 创建一个新的ASP.NET Web应用程序
  2. Add a code file with the following: 添加以下代码文件:

Normally I wouldn't put everything in one file, but for simplicity... 通常我不会把所有东西放在一个文件中,但为了简单起见......

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;
        }
    }
}

Update the web.config file: 更新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>

You don't need a .svc file, so start the application, making note of the root url used by the ASP.NET Development Server. 您不需要.svc文件,因此启动应用程序,记下ASP.NET Development Server使用的根URL。 On the Request Builder tab of fiddler enter your url: 在fiddler的Request Builder选项卡上输入您的URL:

在此输入图像描述

Click the Execute button and you should see your service results: 单击执行按钮,您应该看到您的服务结果:

在此输入图像描述

Hopefully you can use this to figure out what you need to update on your service to get it running. 希望您可以使用它来确定您需要更新服务以使其运行。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM