Microsoft MVP성태의 닷넷 이야기
WCF RESTful Service에서 enum을 parameter로 쓰는방법 [링크 복사], [링크+제목 복사],
조회: 15317
글쓴 사람
개태
홈페이지
첨부 파일
 

안녕하세요.

성태님에게 얻은 정보로 많은 도움을 받고 있습니다.^^

이번에 1개의 Service 함수로 OperationContract와 RESTful API를 같이 구현하고자 합니다.
문제는, Parameter로 enum타입을 받을시 OperationContract는 별 문제가 없지만..
RESTful API의 UriTemplate에 맵핑시는 에러가 나더군요..ㅠㅠ

굳이 해결하자면 RESTful API용 Service함수를 따로 만들어서, string 또는 int로 값을받아서 enum으로 변환 하면되지만,
이번 목표는 하나의 Service함수로 Soap메시지 반환과 RESTful API 호출시 json 반환을 목표로 하고 있습니다.
혹시 해결방법 알 수 있을까요?

=======================================================================================================================================

아래는 에러 내용 입니다.

'/' 응용 프로그램에 서버 오류가 있습니다.
'ILeService' 계약의 'GetCode' 작업에 'string' 형식을 사용하지 않는 경로 변수 'type'이(가) 있습니다. UriTemplate 경로 세그먼트에 대한 변수에는 'string' 형식이 있어야 합니다.
설명: 현재 웹 요청을 실행하는 동안 처리되지 않은 예외가 발생했습니다. 스택 추적을 검토하여 발생한 오류 및 코드에서 오류가 발생한 위치에 대한 자세한 정보를 확인하십시오.

예외 정보: System.InvalidOperationException: 'ILeService' 계약의 'GetCode' 작업에 'string' 형식을 사용하지 않는 경로 변수 'type'이(가) 있습니다. UriTemplate 경로 세그먼트에 대한 변수에는 'string' 형식이 있어야 합니다.

소스 오류:
현재 웹 요청을 실행하는 동안 처리되지 않은 예외가 생성되었습니다. 아래의 예외 스택 추적을 사용하여 예외의 원인 및 위치 정보를 확인할 수 있습니다.

스택 추적:
[InvalidOperationException: 'ILeService' 계약의 'GetCode' 작업에 'string' 형식을 사용하지 않는 경로 변수 'type'이(가) 있습니다. UriTemplate 경로 세그먼트에 대한 변수에는 'string' 형식이 있어야 합니다.]
   System.ServiceModel.Dispatcher.UriTemplateClientFormatter.Populate(Dictionary`2& pathMapping, Dictionary`2& queryMapping, Int32& totalNumUTVars, UriTemplate& uriTemplate, OperationDescription operationDescription, QueryStringConverter qsc, String contractName) +778
   System.ServiceModel.Description.WebHttpBehavior.GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint) +236
   System.ServiceModel.Description.WebHttpBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) +1746
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +3270
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +65
   System.ServiceModel.ServiceHostBase.OnBeginOpen() +35
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +49
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +308
   System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +110
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +641

[ServiceActivationException: 컴파일 중 발생한 예외로 인해 '/LeService.svc' 서비스를 활성화할 수 없습니다. 예외 메시지: 'ILeService' 계약의 'GetCode' 작업에 'string' 형식을 사용하지 않는 경로 변수 'type'이(가) 있습니다. UriTemplate 경로 세그먼트에 대한 변수에는 'string' 형식이 있어야 합니다..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +500298
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +174
   System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +351498
   System.Web.AsyncEventExecutionStep.InvokeEndHandler(IAsyncResult ar) +156
   System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +9900796

=======================================================================================================================================

아래는 구현 코드 입니다.

namespace Test.Service
{
    partial interface ILeService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "code/{type}/{code}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        CodeMaster GetCode(CodeType type, string code);
    }

    partial class LeService : ILeService
    {
        public CodeMaster GetCode(CodeType type, string code)
        {
            var db = new CodeMasterRepository();

            return db.SelectCode(type, code);
        }
    }

    [DataContract]
    public enum CodeType
    {
        [EnumMember]
        Department = 0,

        [EnumMember]
        JobGrade = 1,

        [EnumMember]
        Role = 2,
    }
}








[최초 등록일: ]
[최종 수정일: 5/4/2019]


비밀번호

댓글 작성자
 



2019-05-04 04시48분
이건가요?

Passing Enum & Custom Classes to WCF RESTful service using UriTemplate
; https://stackoverflow.com/questions/31184719/passing-enum-custom-classes-to-wcf-restful-service-using-uritemplate
정성태
2019-05-04 11시42분
[개태] 벌서 답변 달아주셨네요.ㅎㅎ

올려주신 링크는 이미 확인했던 링크인데, 답변에 보면 enum은 QueryString에서 원래 정상작동 한다고 하더군요.
전 안되는데 말이죠..ㅠㅠ
[guest]

... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
979왕초보7/1/201117945Apache + IIS Redirect [2]
9776/16/201122268웹컨트롤 어느거 쓰는게 좋나요 [1]
976박성준6/13/201117744VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201120023메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201117552firebird install건 [1]
970임동찬5/18/201118345ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201119993WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201119424안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201122160C# using문 관련 [9]
964김동미4/18/201119767wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201121140    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201118973        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201117351wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201116965TFS 사용관련 [1]
961임동찬4/7/201117320XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201118853XML Schema Editor [4]
959immm3/24/201116631로그인 연동 어려운 건가요? [1]
958꼭지3/3/201118692Supporting compressed request in WCF 3.5 [5]
957임동찬2/21/201119198WCF channel faulted 관련 [2]
956윤용한2/18/201122638WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201119300TFS 에서 소스 영구 제거 방법 [1]
954한장우2/16/201117019atl activeX 질문이요~ [1]
952박용운2/16/201117863IE8.0에서 BHO [1]
953박용운2/16/201117867    답변글 [답변]: IE8.0에서 BHO
951임동찬2/11/201117911WCF Service Reference [1]
950이성환2/9/201119358Windows application 프로젝트를 참조 했을 때 생성되는 실행파일을 직접 실행 불가능하도록 하고 싶습니다. [6]파일 다운로드1
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...