WCF 在应用程序承接WCF服务

namespace Microsoft.ServiceModel.Samples
{
    
using System;
    
using System.ServiceModel;
    
using System.ServiceModel.Description;
    
using System.ServiceModel.Channels;

    
class Program
    {
        
static void Main(string[] args)
        {
            
//为服务的基址创建 Uri 实例。
            Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

            
//创建一个新的 ServiceHost 实例以承载服务。
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

            
try
            {
                
//添加公开服务的终结点。
                selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");

                
//启用元数据交换。
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled 
= true;
                selfHost.Description.Behaviors.Add(smb);

                
//打开 ServiceHost 并等待传入消息。
                selfHost.Open();
                Console.WriteLine(
"The service is ready.");
                Console.WriteLine(
"Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                
// Close the ServiceHostBase to shutdown the service.
                selfHost.Close();
            }
            
catch (CommunicationException ce)
            {
                Console.WriteLine(
"An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }

        }
    }
}
posted @ 2011-03-27 15:13  MrNobody_123456  阅读(122)  评论(0)    收藏  举报