【问题标题】:Test WCF with JSON Response使用 JSON 响应测试 WCF
【发布时间】:2012-04-10 19:29:30
【问题描述】:

我有一个返回 json 的 WCF 服务。我想使用 Fiddle 对其进行测试(这是我正在关注的教程中使用的应用程序)。问题是我在调用“AddAngajat”时遇到 HTTP 404 错误。

服务的实现:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, AddressFilterMode=AddressFilterMode.Any)]
    public class AngajatService : InterfaceAngajat
    {
        List<Angajat> listaAngajati =null;

        public AngajatService()
        {
            if (listaAngajati == null)
                listaAngajati = new List<Angajat>();
        }

        [WebInvoke(Method="POST", UriTemplate = "AddAngajat", ResponseFormat=WebMessageFormat.Json)]
        public void AddAngajat(Angajat angajat)
        {
            listaAngajati.Add(angajat);
        }
        public Angajat[] GetAllAngajati()
        {
            listaAngajati.Add(new Angajat() { idAngajat = "2f34", nume = "nume1" });
            return listaAngajati.ToArray();
        }
    }

App.config:

 <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="testJSON.AngajatService">
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <endpoint address="json" behaviorConfiguration="JSONEndpointBehavior"
          binding="webHttpBinding" bindingConfiguration="" contract="testJSON.InterfaceAngajat" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/testJSON/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="JSONEndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

关于如何解决这个问题的任何想法?其他测试方法?

【问题讨论】:

    标签: c# json wcf http-status-code-404 http-error


    【解决方案1】:

    您需要致电http://localhost:8732/testJSON/json/AddAngajat,它应该可以工作。

    否则包括您的客户端代码(或 sn-p)。

    更新

    根据您提到的更改,URL 必须是 http://localhost/testJSON/Service.svc/json/AddAngajat

    【讨论】:

    • 我使用 Fiddler 作为客户端。我的服务现在由本地 IIS 托管,所以我从 url 中删除了端口。 localhost/testJSON/Service.svc 返回正常,但是如何发送 post 请求以及如何发送参数?
    • 正在工作......非常感谢。我是 C# 和寡妇服务的新手,所以我不知道是肥皂服务还是休息服务。我会努力学习更多。非常感谢。
    【解决方案2】:

    fiddler 的请求如下所示:

    POST http://localhost/VirtualDirectoryName/testJson/service.svc/json/Addangajat HTTP/1.1
    Content-Type: application/json
    Content-Length: 47
    
    {"Id":6,"StringValue":"from client rajeshwin7"}
    

    假设对象“Angajat”的结构具有 2 个属性 Id 和 StringValue。

    不确定您是否可以从提琴手对托管在 Cassini 上的服务执行 GET/POST 调用。我宁愿将它托管在 IIS 中,然后使用 fiddler 调用该服务。如果您不想在 IIS 中托管,请使用带有 HttpWebRequest 的 .NET 客户端来调用其余服务。

    【讨论】:

    • 我的服务托管在本地机器上的 IIS 中。如果我从浏览器调用localhost/testJSON/Service.svc/json/GetAllAngajati,我会得到“方法不允许”。作为回应。从 Fiddler 我正在尝试:POST + localhost/testJSON/Service.svc/web/AddAngajat + HTTP/1.1 我正在发送 { "idAngajat":"angajat1", "nume":"nume1" } 请求标头: User-Agent: Fiddler Host: localhost Content-Type :应用程序/json; charset=utf-8 Content-Length: 41 我还是得到 404。
    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多