2012-03-27 52 views
2

我有一個返回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> 

關於如何解決此問題的任何想法?其他測試方法呢?

回答

1

您需要撥打http://localhost:8732/testJSON/json/AddAngajat,它應該工作。

否則包括您的客戶端代碼(或代碼段)。

UPDATE

你提到的這個變化,URL必須是此http://localhost/testJSON/Service.svc/json/AddAngajat

+0

我使用Fiddler作爲客戶端。我的服務現在由本地IIS託管,所以我從URL中刪除了端口。 http://localhost/testJSON/Service.svc返回好了,但我怎麼能發送一個帖子請求,我如何發送參數? – 2012-03-27 09:39:18

+0

它不是WCF REST嗎? – Aliostad 2012-03-27 09:55:08

+0

正在工作.....非常感謝。我是C#和widnows服務中的一個非常早期的開局者,所以我不知道是否是肥皂或休息服務。我會盡力學習更多。非常感謝。 – 2012-03-27 10:00:26

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。

我不確定您是否可以對來自提琴手的卡西尼託管的服務執行GET/POST調用。我寧願將其託管在IIS中,然後使用fiddler來調用該服務。如果您不想在IIS中託管,則使用帶有HttpWebRequest的.NET客戶端來調用其餘服務。

+0

我的服務位於本地機器的IIS中。如果我從瀏覽器中調用http://localhost/testJSON/Service.svc/json/GetAllAngajati,我會得到「方法不允許」。作爲迴應。從Fiddler我想嘗試:POST + http://localhost/testJSON/Service.svc/web/AddAngajat + HTTP/1.1,我發送{ 「idAngajat」:「angajat1」, 「nume」:「nume1」 }請求標題:User-Agent:Fiddler Host:localhost Content-Type:application/json; charset = utf-8 內容長度:41 我仍然得到404。 – 2012-03-27 09:52:42