ASP.Net

WCF 메서드명 문자열로 넘겨서 해당 메서드 호출하기

캡틴노랑이 2015. 10. 29. 10:12
반응형

WCF의 EndPoint의 메서드를 문자열로 호출하고 싶을 때 다음과 같이 사용하면된다.


protected int ServiceInt<TClient, TChannel>(string methodName, params object[] methodParameters)       

    where TClient : class, ICommunicationObject

    where TChannel : class        

{

    var client = Activator.CreateInstance<TClient>();


    DataSet ds = new DataSet();


    BasicHttpBinding binding = new BasicHttpBinding();

    //EndpointAddress endPoint = new EndpointAddress("http://localhost/WebService/WCFSample.svc");

    EndpointAddress endPoint = new EndpointAddress(ServiceEndPointSearch(typeof(TChannel).FullName.Replace("Test.", "")));

    

    ChannelFactory<TChannel> channel = new ChannelFactory<TChannel>(binding, endPoint);


    TChannel wcfClient = channel.CreateChannel();


    Type type = typeof(TChannel);

    MethodInfo mi = type.GetMethod(methodName);

    int result = (int)mi.Invoke(wcfClient, methodParameters);


    ((IClientChannel)wcfClient).Close();


    return result;

}

반응형