wcf-hello world

service

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

using Server;
public class CalculateService : ICalculate
{

    #region ICalculate 成员

    public string GetCalculate()
    {
        return Guid.NewGuid().ToString();
    }

    #endregion
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Server
{
    [ServiceContract]
    public interface ICalculate
    {
        [OperationContract]
        string GetCalculate();
    }
}

 

client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Client
{
    public class Client
    {
        public static void Main(String[] args)
        {
            new Client().Test();
        }
        public void Test()
        {
            ServiceEndpoint endpoint =  new System.ServiceModel.Description.ServiceEndpoint(ContractDescription.GetContract(typeof(ICalculate)));

            endpoint.Binding = new BasicHttpBinding();
            endpoint.Address = new EndpointAddress("http://127.0.0.1:8888/cal");

            ChannelFactory<ICalculate> channelFactory = new ChannelFactory<ICalculate>(endpoint);
            string result = channelFactory.CreateChannel().GetCalculate();


            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Client
{
    [ServiceContract]
    public interface ICalculate
    {
        [OperationContract]
        string GetCalculate();
    }
}

 

host

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using Server;

namespace ServerConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(CalculateService));

            ServiceEndpoint serviceEndPoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ICalculate)));
            serviceEndPoint.Address = new EndpointAddress("http://127.0.0.1:8888/cal");
            serviceEndPoint.Binding = new BasicHttpBinding();

            host.Description.Endpoints.Add(serviceEndPoint);

            host.Opened += new EventHandler(host_Opened);

            try
            {
                host.Open();
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                host.Abort();
            }
            finally 
            {
                host.Close(); 
            }
        }

        static void host_Opened(object sender, EventArgs e)
        {
            Console.WriteLine("servece have stareted....");
        }
    }
}

使用配置文件

 ChannelFactory<ICalculate> channelFactory = new ChannelFactory<ICalculate>("mycalculateclient");
            string result = channelFactory.CreateChannel().GetCalculate();
 ServiceHost host = new ServiceHost(typeof(CalculateService));
            host.Opened += new EventHandler(host_Opened);

 

posted @ 2014-03-01 15:07  feidaochuanqing  阅读(153)  评论(0编辑  收藏  举报