玩转C科技.NET

从学会做人开始认识这个世界!http://volnet.github.io

导航

[WCF]How to Hosting?

理清楚Host和Client的关系显得非常重要。
今天在VS2005上安装了WCF扩展,才发现原来2005上的扩展与VS2008的wcf模板不太一样,汗一个先……
2005的WcfServiceLibrary:
Code

在2005的WcfServiceLibrary中提供了MyServiceHost.cs,倒是很好地将一部分功能给封装起来了。
测试代码如下:
    class Program
    
{
        
static void Main(string[] args)
        
{
            
//启动Host
            MyServiceHost.StartService();

            
//构建Client端的Proxy
            System.ServiceModel.ChannelFactory<WCFServiceLibrary1.IService1> channel
                
= new System.ServiceModel.ChannelFactory<WCFServiceLibrary1.IService1>("wsEndPoint");   //值得注意的是这里wsEndPoint是指Client的配置(详见App.config)
            WCFServiceLibrary1.IService1 IService1Client = channel.CreateChannel();

            
//利用Client调用服务操作
            Console.WriteLine(IService1Client.MyOperation1("volnet"));

            
//停止Host
            MyServiceHost.StopService();
        }

    }
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<client>
      
<endpoint address="http://localhost:8080/service1" contract="WCFServiceLibrary1.IService1" binding="wsHttpBinding" name="wsEndPoint" />
    
</client>
    
<services>
      
<service name="WCFServiceLibrary1.service1">
        
<endpoint contract="WCFServiceLibrary1.IService1" binding="wsHttpBinding" />
      
</service>
    
</services>
  
</system.serviceModel>
</configuration>
注意:client与services的不同。

通常我们有以下一些办法使用服务:
1、在2008中默认添加的WcfServiceLibrary可以被Start a new Instance。这样我们只要在Client端(比如一个ConsoleApplication)Add Service Reference,将会默认创建ServiceReference1的代理(当然是可以改的了),在Client的操作中(例如ConsoleApplication的Main方法)可以直接使用这个代理。
2、在2005中默认添加的WcfServiceLibrary是不带App.config的,当然也可以通过配置而实现。同样可以在Client端去Hosting一个Service(如本文)。因此更需要注意Host与Client的关系。

posted on 2007-11-30 12:08  volnet(可以叫我大V)  阅读(1523)  评论(0编辑  收藏  举报

使用Live Messenger联系我
关闭