2

我有一个返回 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();
        }
    }

应用程序配置:

 <?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>

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

4

2 回答 2

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。

Am not sure if you can perform a GET/POST call to a service that is hosted on Cassini from fiddler. I would rather host it in IIS and then use fiddler to invoke the service. If you dont want to host in IIS then use a .NET client with HttpWebRequest to invoke the rest service.

于 2012-03-27T09:47:21.690 回答
1

你需要打电话http://localhost:8732/testJSON/json/AddAngajat,它应该工作。

否则包括您的客户端代码(或片段)。

更新

通过您提到的更改,URL 必须是这个http://localhost/testJSON/Service.svc/json/AddAngajat

于 2012-03-27T09:24:18.083 回答