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

안녕하세요.

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

이번에 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]

... 16  17  18  19  20  21  22  23  24  25  26  [27]  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
5216WPF9/8/20198736WPF에서 XAML Islands를 사용하여 Win2D를 사용하니 그래픽 품질이 저하됩니다. [2]파일 다운로드1
5215허송세월9/5/20198546중복실행 방지 관련 문의 [2]파일 다운로드1
5214Jang...9/4/20198150[DB 테이블의 데이터 변경에 대한 알림 처리] SQL-Server말고 MySQL은 불가능하겠죠? [1]
5213진우8/31/20197414c# 람다 변수 캡쳐 문의 [2]
5212심성보8/29/20199069Clipboard내 여러개의 이미지를 PictureBox로 불러오는 문제 [2]
5211최휘철8/24/20198253CLR20r3 관련된 윈도우 오류입니다. ㅠㅠ 도와주세요. / 아래글 관련하여 관련 파일 올려 드려요^^ [1]파일 다운로드1
5210최휘철8/23/201912175CLR20r3 관련된 윈도우 오류입니다. ㅠㅠ 도와주세요. [5]
5209세퉁8/21/20197811폰트 파일 속성 값을 가져오는 방법 질문 드립니다. [2]파일 다운로드1
5208홍길동8/19/20198624DebugDiag에서 .Net의 Stack Trace를 Windbg에서는 어떻게 볼 수 있나요? [3]
5207민성8/16/20196963네 소스 전체를 올리도록 하겠습니다. [2]파일 다운로드1
5206민성8/14/20197104전 재현 가능하다고 봤는데 다시올리도록 하겠습니다. [1]
5205miny...8/14/20197955안녕하세요 .WPF ListBox시 체크박스가 있는데 체크박스에서 체크가 되었는지 알수 있는 방법이 있을까요? [1]
5204영민8/8/201910721안녕하세요 디버깅시 콘솔창을 띠어서 볼수가 없나요? [7]
5202민성8/6/20197472WPF에서 <Application.Resources에 xaml에 있는 icon 값을 저장하고 xaml에 불러다 사용하고 싶은데요 [1]
5201김대훈8/3/20197097상속시 생성자에 대해 질문드립니다 [3]
5200농상7/30/20199791foreach로 데이터 변경 [2]
5190오리다람7/20/20197188질문드립니다. [3]
5189진우7/19/20197712C# 스레드풀 코어별 실행 문의 [2]
5188황태관7/19/20196923비주얼베이직 2019 실행 할때 마다.. [3]
5187플하7/19/20199749UWP 관련 궁금한 사항에 대해서 [1]
5186김대훈7/14/20198345박싱과 언박싱에 대해 [2]
5185농상7/13/20197203Nullable에 대해서 [1]
5184김대훈7/4/20197090저자님의 책을 다 본후에는 [2]
51837/2/20197797.NET Compact Freamwork 컨트롤러 더블버퍼링 [1]
5182wpf ...7/2/20197989wpf 질문 드립니다. [1]파일 다운로드1
51817/1/20198405DataGridview Doublebuffer 에 대해서 질문드립니다. [2]
... 16  17  18  19  20  21  22  23  24  25  26  [27]  28  29  30  ...