C#中winform下利用ArcEngine调用ArcGIS Server发布的服务 AE 10

时间:2021-12-12 04:30:36

开发环境:vs2010 + AE 10 测试

public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop); ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.Engine);

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获得服务对象名称[注意地址和名称;test:发布服务的自定义名称]
            IAGSServerObjectName pServerObjectName = GetMapServer("http://172.18.70.254:8081/ArcGIS/services", "test", false);
            IName pName = (IName)pServerObjectName;
            //访问地图服务
            IAGSServerObject pServerObject = (IAGSServerObject)pName.Open();
            IMapServer pMapServer = (IMapServer)pServerObject;

            IMapServerLayer pMapServerLayer = new MapServerLayer() as IMapServerLayer;
            //连接地图服务

            pMapServerLayer.ServerConnect(pServerObjectName, pMapServer.DefaultMapName);
            //添加数据图层

            axMapControl1.AddLayer(pMapServerLayer as ILayer);

            axMapControl1.Refresh();
        }

        public IAGSServerObjectName GetMapServer(string pHostOrUrl, string pServiceName, bool pIsLAN)
        {
            //设置连接属性
            IPropertySet pPropertySet = new PropertySet();
            if (pIsLAN)
                pPropertySet.SetProperty("machine", pHostOrUrl);
            else
                pPropertySet.SetProperty("url", pHostOrUrl);

            //打开连接

            IAGSServerConnectionFactory pFactory = new AGSServerConnectionFactory();
            //Type factoryType = Type.GetTypeFromProgID(
            //    "esriGISClient.AGSServerConnectionFactory");
            //IAGSServerConnectionFactory agsFactory = (IAGSServerConnectionFactory)
            //    Activator.CreateInstance(factoryType);
            IAGSServerConnection pConnection = pFactory.Open(pPropertySet, 0);

            //Get the image server.
            IAGSEnumServerObjectName pServerObjectNames = pConnection.ServerObjectNames;
            pServerObjectNames.Reset();
            IAGSServerObjectName ServerObjectName = pServerObjectNames.Next();
            while (ServerObjectName != null)
            {
                if ((ServerObjectName.Name.ToLower() == pServiceName.ToLower()) &&
                    (ServerObjectName.Type == "MapServer"))
                {

                    break;
                }
                ServerObjectName = pServerObjectNames.Next();
            }

            //返回对象
            return ServerObjectName;
        }