내 솔루션은 다음과 같습니다.
최소 Visual Studio 2012가 필요합니다.
WCF 서비스 라이브러리 프로젝트를 만듭니다.
데이터 계약 래퍼를 자동으로 생성하려면 Xsd 파일을 포함하십시오.
은 다음과 같이 당신의 ServiceContract와 클래스를 작성한다 :
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace RestSoapTest
{
public class Service1 : IService1
{
public List<CompositeType> GetData(string paramA, string paramB)
{
List<CompositeType> output = new List<CompositeType>();
output.Add(new CompositeType()
{
Key = paramA,
Value = paramB,
});
output.Add(new CompositeType()
{
Key = paramA,
Value = paramB,
});
output.Add(new CompositeType()
{
Key = paramA,
Value = paramB,
});
return output;
}
public void PutData(string paramA, string paramB, List<CompositeType> data)
{
throw new NotImplementedException();
}
}
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "GetData/{paramA}/{paramB}")]
List<CompositeType> GetData(string paramA, string paramB);
[OperationContract]
[WebInvoke(UriTemplate = "PutData/{paramA}/{paramB}")]
void PutData(string paramA, string paramB, List<CompositeType> data);
}
[DataContract]
public class CompositeType
{
[DataMember]
public string Key { get; set; }
[DataMember]
public string Value { get; set; }
}
}
(어느 쪽의 손으로 당신의 DataContract 쓰기 또는 XSD로 제어가)
, 웹 호스트 프로젝트를 추가 WCF 프로젝트를 참조하고이 추가 web.config 파일 :
이제
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="standardRest" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" helpEnabled="true"/>
</webHttpEndpoint>
<mexEndpoint>
<standardEndpoint name="standardMex"/>
</mexEndpoint>
</standardEndpoints>
<services>
<service name="RestSoapTest.Service1">
<endpoint name="rest" address="" kind="webHttpEndpoint" endpointConfiguration="standardRest" contract="RestSoapTest.IService1"/>
<endpoint name="mex" address="mex" kind="mexEndpoint" endpointConfiguration="standardMex" contract="RestSoapTest.IService1"/>
<endpoint name="soap" address="soap" binding="basicHttpBinding" contract="RestSoapTest.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="RestSoapTest.svc" service="RestSoapTest.Service1"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>
할 수 있습니다 브라우저 :
/RestSoapTest.svc = SOAP 엔드 포인트/도움말 페이지
/RestSoapTest.svc?wsdl = SOAP 메타 데이터
/RestSoapTest.svc/help = 자동 나머지 도움말 페이지
/RestSoapTest.svc/url/ 방법 /에 = 방법을 얻기 위해, REST 작업을
을 실행에 WebGet을 사용하고 UriTemplate 및 방법 프로토 타입 일치의 매개 변수 수를 확인 그렇지 않으면 당신은 넣어 방법에 대해 오류
를 얻을 수 있습니다, WebInvoke의를 사용하고 보장 그 UriTemplate의 매개 변수 개수 (프로토 타입의 개수 +1).
이렇게하면 매핑되지 않은 단일 매개 변수가 WCF 프레임 워크에서 HTTP 게시물의 요청 본문을 통해 들어오는 것으로 간주됩니다.
본문을 통해 들어오는 하나 이상의 매개 변수가 필요한 경우 매개 변수 형식을 래핑으로 설정해야합니다. 기본값은 Bare입니다. 래핑 된 자동은 parameterName/value 속성에 여러 매개 변수를 래핑합니다.