我有一個返回json的WCF服務。我想用Fiddle測試它(這是我在下面的教程中使用的應用程序)。問題是,例如,在調用「AddAngajat」時出現HTTP 404錯誤。使用JSON響應測試WCF
的服務實現:
[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>
關於如何解決此問題的任何想法?其他測試方法呢?
我使用Fiddler作爲客戶端。我的服務現在由本地IIS託管,所以我從URL中刪除了端口。 http://localhost/testJSON/Service.svc返回好了,但我怎麼能發送一個帖子請求,我如何發送參數? – 2012-03-27 09:39:18
它不是WCF REST嗎? – Aliostad 2012-03-27 09:55:08
正在工作.....非常感謝。我是C#和widnows服務中的一個非常早期的開局者,所以我不知道是否是肥皂或休息服務。我會盡力學習更多。非常感謝。 – 2012-03-27 10:00:26