開發環境:VS2008,c#
1.新建個WCF服務網站
文件-新建-網站-WCF服務
2,運行一下,提示配置WEB.CONFIG,點擊確認.
3,打開web.config增加如下節點:
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
</serviceHostingEnvironment>
endpoint 中增加 behaviorConfiguration="webBehavior"
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
處理完以上3處之后,web.config就OK了.
4,IService.cs 增加:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
List<CompositeType> Test();
Service.cs 增加:
public List<CompositeType> Test()
{
List<CompositeType> lst = new List<CompositeType>();
CompositeType type = new CompositeType();
type.BoolValue = true;
type.StringValue = "22";
lst.Add(type);
CompositeType type2 = new CompositeType();
type2.BoolValue = false;
type2.StringValue = "33";
lst.Add(type2);
return lst;
}
CompositeType類:
[DataContract]
public class CompositeType
{ bool boolValue = true; string stringValue = "Hello ";
[DataMember]
public bool BoolValue { get { return boolValue; } set { boolValue = value; } }
[DataMember]
public string StringValue { get { return stringValue; } set { stringValue = value; } }
}
5,Service.cs 增加
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
6,運行測試,如:
http://localhost:1177/WCFData/Service.svc
手動輸入:http://localhost:1177/WCFData/Service.svc/Test
返回:[{"BoolValue":true,"StringValue":"22"},{"BoolValue":false,"StringValue":"33"}]
測試成功!