我有一个返回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>
有关如何解决此问题的任何想法?其他测试方法呢?
答案 0 :(得分:2)
来自提琴手的请求如下所示:
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。
我不确定您是否可以对来自fiddler的Cassini上托管的服务执行GET / POST调用。我宁愿在IIS中托管它,然后使用fiddler来调用服务。如果你不想在IIS中托管,那么使用带有HttpWebRequest的.NET客户端来调用其余的服务。
答案 1 :(得分:1)
您需要致电http://localhost:8732/testJSON/json/AddAngajat
,它应该有效。
否则请包含您的客户端代码(或代码段)。
根据您提到的更改,网址必须为此http://localhost/testJSON/Service.svc/json/AddAngajat