WCF

2012-04-09 6 views
2

私はこれを返すWCF Webサービスを開発していますとJSON配列を生成します。私はこの方法でそれを生成するために行うことができますどのようにWCF

public class RestServiceImpl : IRestServiceImpl 
    { 
     public List<FormContract> allForms() 
     { 
      List<FormContract> list = null; 
      using (var vAdmEntities = new ADMDatabase.ADMEntities()) 
      { 
       list = new List<FormContract>(); 
       foreach (var form in vAdmEntities.Form) 
       { 
        FormContract formC = new FormContract 
        { 
         FormName = form.name.Trim(), 
         FormId = form.formId 
        }; 
        list.Add(formC); 
       } 
      } 

      return list; 
     } 
    } 

{ 
    "allFormsResult": [ 
     { 
      "FormId": 1, 
      "FormName": "Formulario 1" 
     }, 
     { 
      "FormId": 2, 
      "FormName": "Formulario 2" 
     }, 
     { 
      "FormId": 3, 
      "FormName": "Formulario 3" 
     } 
    ] 
} 

これはコードです?

[ 
    { 
     "FormId": 1, 
     "FormName": "Formulario 1" 
    }, 
    { 
     "FormId": 2, 
     "FormName": "Formulario 2" 
    }, 
    { 
     "FormId": 3, 
     "FormName": "Formulario 3" 
    } 
] 
+0

「List 」ではなく、「FormContract []」を返すようにしてください。 –

+0

いいえ、動作しません。 – VansFannel

答えて

4

問題はここにある:

namespace ADM 
{ 
    [ServiceContract] 
    public interface IRestServiceImpl 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      UriTemplate = "forms/")] 
     List<FormContract> allForms(); 
    } 
} 

私はこのようにそれを使用する必要があります。

namespace ADM 
{ 
    [ServiceContract] 
    public interface IRestServiceImpl 
    { 
     [OperationContract] 
     [WebInvoke(Method = "GET", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Bare, 
      UriTemplate = "forms/")] 
     List<FormContract> allForms(); 
    } 
} 

BodyStyleを変更する:

BodyStyle = WebMessageBodyStyle.Bare 
0

この動作も可能セ契約に直接属性を追加する必要はありません。

<services> 
    <service name="MyServiceNameSpace.MyServiceClass"> 
    <endpoint 
     address="http://yourservicedomain.ext/MyServiceClass.svc/" 
     binding="webHttpBinding" 
     contract="MyServiceNameSpace.MyServiceContract" 
     behaviorConfiguration="MyEndpointBehavoir" 
     listenUri="/" />   
    </service>  
</services> 

<behaviors> 
    <endpointBehaviors> 
    <behavior name="MyEndpointBehavoir"> 
     <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare"/> 
    </behavior>   
    </endpointBehaviors> 
</behaviors>