WCF - Server, Client

seung-jae hwang·2019년 3월 21일
0

WCFUP

목록 보기
1/5

Server

static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000");

        WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);
        //svcHost.AddServiceEndpoint(typeof(ICalculator), new WebHttpBinding(), "Http");

        try
        {
            svcHost.Open();

            Console.WriteLine("Service is running");
            Console.WriteLine("Press enter to quit...");
            Console.ReadLine();

            svcHost.Close();
        }
        catch (CommunicationException cex)
        {
            Console.WriteLine("An exception occurred: {0}", cex.Message);
            Console.ReadLine();
            svcHost.Abort();
        }
    }
    

Client

    private void button1_Click(object sender, EventArgs e)
    {
        /*
         브라우저 : http://localhost:8000/addEx?x=2&y=5

GET http://localhost:8000/addEx?x=2&y=5 HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Whale/1.4.64.6 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,^/; q = 0.8
Accept - Encoding: gzip, deflate, br
Accept - Language: ko - KR,ko; q = 0.9,en - US; q = 0.8,en; q = 0.7
*/
this.Text = "";
ChannelFactory factory = new ChannelFactory();
// Address
string address = "http://localhost:8000";
factory.Endpoint.Address = new EndpointAddress(address);
// Binding : WEB 사용
factory.Endpoint.Binding = new WebHttpBinding();
// Contract 설정
factory.Endpoint.Contract.ContractType = typeof(ICalculator);
// Channel Factory 만들기
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
ICalculator channel = factory.CreateChannel();
// Server 쪽 함수 호출
var result = channel.AddEx(2, 3);
// Close Channel
((ICommunicationObject)channel).Close();
this.Text = result.ToString();
}

0개의 댓글