2013-04-22 13 views
8

Ho creato un servizio wcf. Funziona bene quando sto usando semplicemente in .net aggiungendo come servizio web. Ma voglio renderlo in grado di utilizzare l'app per iPhone come chiamata JSON. Per il test l'ho usato in .net con JSON ma non funziona.Abilita servizio WCF da utilizzare con JSON

so che questo tipo di domanda è stato chiesto prima, ho cercato in questa soluzione non può trovare per me.

la mia configurazione:

<system.serviceModel> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="servicebehavior"> 
     <serviceMetadata httpsGetEnabled="true" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="endpointBehavior"> 
     <enableWebScript /> 
     <webHttp defaultBodyStyle="Wrapped" defaultOutgoingResponseFormat="Json" /> 
    </behavior> 
    </endpointBehaviors> 

</behaviors> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
<services> 
    <service name="MyService" behaviorConfiguration="servicebehavior"> 
    <endpoint address="" 
       behaviorConfiguration="endpointBehavior" 
       binding="webHttpBinding" 
       contract="IMyService" /> 
    </service> 
</services> 

codice di interfaccia:

[ServiceContract] 
public interface IGolfPyramidService 
{ 



    [WebInvoke(UriTemplate = "/Test", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] 
    [OperationContract] 
    string Test(); 

} 

Myservice.cs codice:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyService : IMyService 
{   
    public string Test() 
    { 
     return "success"; 
    } 
} 

voglio rendere possibile chiamare il metodo utilizzando formato url come: http://example.com/MyService.svc/test

+0

perché stai usando WebInvoke sulla vostra interfaccia e non WebGet? – Erwin

+0

Grazie per la risposta .. per il mio caso voglio ottenere risposta in JSON e ho avuto problemi con il metodo WebGet. Il metodo WebInvoke ha funzionato per me. – Finisher001

+1

Se si chiama il servizio web dal browser che si utilizza GET, non POST. – Erwin

risposta

11

se sei un principiante, questo ti guiderà a creare un servizio web abilitato per json e xml che può essere utilizzato da IOS e Android.
http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

+0

ho ancora un piccolo problema ... sto ricevendo i dati di output ma non nel formato JSON. Ho specificato ResponseFormat = WebMessageFormat.Json come descritto nell'articolo ma dà un risultato in formato semplice non in JSON – Finisher001

+0

hai provato da IOS o Android – Arshad

7

Perché si utilizza un metodo post per ottenere un valore stringa semplice? Prova questo esempio che dovrebbe funzionare normalmente.

configurazione

<system.serviceModel> 
<services> 
    <service behaviorConfiguration="RestServiceBehavior" name="WcfService1.MyService"> 
    <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJSONP" contract="WcfService1.IMyService" /> 
    </service> 
</services> 
<bindings> 
    <webHttpBinding> 
    <binding name="webHttpBindingWithJSONP" /> 
    </webHttpBinding> 
</bindings> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="web"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior name="RestServiceBehavior"> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false"/> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 

IMyService.cs

namespace WcfService1 
{ 
[ServiceContract] 
public interface IMyService 
{ 
    [WebGet(UriTemplate = "Test", 
     ResponseFormat = WebMessageFormat.Json 
    )] 
    [OperationContract] 
    string Test(); 
} 
} 

MyService.svc.cs

namespace WcfService1 
{ 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyService : IMyService 
{ 
    public string Test() 
    { 
     return "Test"; 
    } 
} 
} 
+0

sebbene la risposta precedente abbia funzionato .. ho provato la tua soluzione anche con WEBGET e ha funzionato anche per me .. Il problema nel mio codice sembra essere in 2 posti 1. ho impostato httpsgetenabled = true ora l'ho cambiato in httpgetenabled = true 2. avevo appena inserito i riferimenti nel file di configurazione web senza namespace penso che entrambi fossero i problemi – Finisher001

Problemi correlati