这几天想把SharpMap的数据源改为服务器向客户端提供数据,看到园子里很多朋友在研究WCF.故而心痒难耐,也想赶一把时髦!

     WCF的构架图:

SharpMap数据使用WCF传输

     应用到项目中的层次模型:

SharpMap数据使用WCF传输

相信各位看官通过名字就可以了解他的层次了 .

Contract就是WCF中最重要的数据契约层了.其代码如下:

    }

作为Service,在Contract上签字的方式就是实现这样的一个Interface。下面的Service得到code

    }

以上的内容是不是很简单,这样我们只需要一个宿主.也就是将这些服务提供给客户端的服务源.

Host的本质就是把一个Service 置于一个运行中的进程中,并以Endpoint的形式暴露出来,并开始监听来自Client端的请求。

        }

这里重中之重的就是App.config文件了.其实我们也可以不用它,只是这样可以避免程序的改动所带来的系统重新编译.比如我们把原来存在Intranet的Service放到Internet上,原来可能基于TCP的Binding必须改成基于Http的Binding,在这种情况下,我们依然可修改配置文件就可以了,这样的改动通常是很小的,并且修改了配置文件之后我们不需要对现有的代码进行重编译和重部署,它们可以其实生效。

>

通过以上的代码我们就构建出了一个提供SharpMap地图数据传输的服务器端.那么我们怎么才能获得这些服务呢?

其实也很简单,只需要在客户端编写一个类继承Contract中的接口。

,IGetData
    {
        internal WCFClient():base(){}


        
#region IGetData 成员

        
//获取图片地址
        public string GetPICAddress()
        {
            
return this.Channel.GetPICAddress();
        }

        
//获取VectorLayer样式列表
        public byte[] GetVStyleAddress(string DataBase, ref int VLayerCount)
        {
            
return this.Channel.GetVStyleAddress(DataBase,ref VLayerCount);
        }

        
//获取LabelLayer样式列表
        public byte[] GetLStyleAddress(string DataBase, ref int LLayerCount)
        {
            
return this.Channel.GetLStyleAddress(DataBase, ref LLayerCount);
        }

        
//获取地图渲染列表
        public byte[] ReadXML(string DataBase, string XMLName)
        {
            
return this.Channel.ReadXML(DataBase, XMLName);
        }

        
//获取地图数据源
        public byte[] MapDataBase(string address)
        {
            
return this.Channel.MapDataBase(address);
        }

        
//返回地图连接地址
        public byte[] GetMapDataSource(GetVDataSource VDataSource)
        {
            
return this.Channel.GetMapDataSource(VDataSource);
        }

        
#endregion

        
//返回VectorLayer样式列表
        public DataSet GetVStyle(string DataBase, ref int VLayerCount)
        {
            
return (DataSet)Compression.DeserializeData(GetVStyleAddress(DataBase, ref VLayerCount));
        }

        
//返回地图渲染列表
        public DataSet GetXML(string DataBase, string XMLName)
        {
            
return (DataSet)Compression.DeserializeData(ReadXML(DataBase, XMLName));
        }

        
//返回LabelLayer样式列表
        public DataSet GetLStyle(string DataBase, ref int LLayerCount)
        {
            
return (DataSet)Compression.DeserializeData(GetLStyleAddress(DataBase, ref LLayerCount));
        }
    }

哈哈是不是很简单。可是我们还需要在客户端写一个App.config文件。

他的作用是通过App.config文件来获取Hosting提供的服务。

>

现在可以说是大功告成了!

 由于是第一次实验,对WCF的很多功能上不是很了解。希望大家拍砖,以便于我的进步!

 

参考资源

[原创]我的WCF之旅(1):创建一个简单的WCF程序

相关文章:

  • 2021-08-03
  • 2021-09-14
  • 2021-08-14
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2021-11-23
  • 2021-07-01
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
相关资源
相似解决方案