Return of .NET  
宠辱不惊,静观堂前花开花落;去留无意,漫随天外云卷云舒。

信不信由你,无需配置,两个console就好了

service side

1.定义ServiceContract:

2.new a ServiceHost

3. add endpoint

 

using System.ServiceModel;

namespace Service
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Uri address 
= new Uri("http://localhost:8000/myservice");
            
using (ServiceHost host = new ServiceHost(typeof(MyService), address))
            {
                host.AddServiceEndpoint(
typeof(IMyService), new BasicHttpBinding(), address);
                host.Open();
                Console.ReadLine();
            }
        }

    }

    [ServiceContract]
    
public interface IMyService
    {
        [OperationContract]
        
string GetService();
    }

    
public class MyService : IMyService
    {

        
public string GetService()
        {
            
return "Got Service!";
        }
    }
}

client side: get proxy, service 调用

 

using System.ServiceModel;

namespace Client
{
    
class Program
    {
        
static void Main(string[] args)
        {
            IMyService proxy 
= ChannelFactory<IMyService>.CreateChannel(new BasicHttpBinding(),
                
new EndpointAddress("http://localhost:8000/myservice"));
            Console.WriteLine(proxy.GetService());
            Console.ReadLine();
        }
    }

    [ServiceContract]
    
public interface IMyService
    {
        [OperationContract]
        
string GetService();
    }

}

 

 

 

posted on 2011-03-23 13:51  消化酶  阅读(230)  评论(0编辑  收藏  举报