C#调用wcf服务

作为一个测试,我们建一个解决方案,目录结构如下:

IService1:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace wcfTest.wcf
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        void DoWork();
        [OperationContract]
        string HelloWorld();
    }
}
复制代码

Service1:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace wcfTest.wcf
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
    public class Service1 : IService1
    {
        public void DoWork()
        {

        }

        public string HelloWorld()
        {
            return "hellow";
        }
    }
}
复制代码

ConsoleApplication1=》Program:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference2;
 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client client = new Service1Client();

            // 使用 "client" 变量在服务上调用操作。
            client.DoWork();
            string str = client.HelloWorld();
            Console.WriteLine("{0}", str);
            // 始终关闭客户端。
            client.Close();
        }
    }
}
复制代码

测试可行:

 由于是wcf服务,所以应用理应是分开部署的

例子,在另外的解决方案里面调用:

目录结构:

 调用代码:

复制代码
[WebMethod]
        public string GetMultiDimensionalData(string sql_conn, string mdx_conn, string USERCODE, string mdxJson)
        { 
            //获取多维数据库数据
            //
            string ret = "";
            Service1Client client = new Service1Client();

            // 使用 "client" 变量在服务上调用操作。
            //client.DoWork();
            string str = client.HelloWorld();
            ret = str;
            return ret; 
        }
复制代码

 是可行的;

 传输集合List:

服务:

复制代码
public List<string> tObject()
        {
            List<string> list = new List<string>();
            list.Add("sdasda");
            list.Add("sdasda");
            return list;
        }
复制代码
 [OperationContract]
        List<string> tObject();

调用端:

复制代码
   [WebMethod]
        public string[] GetMultiDimensionalData(string sql_conn, string mdx_conn, string USERCODE, string mdxJson)
        { 
            //获取多维数据库数据
            //
            string ret = "";
            Service1Client client = new Service1Client();

            // 使用 "client" 变量在服务上调用操作。
            //client.DoWork();
            string str = client.HelloWorld();
            ret = str;
            string[] str_arr =  client.tObject();

            return str_arr; 
        }
复制代码

测试结果:

 可行;


猜你喜欢

转载自blog.csdn.net/tink_tl/article/details/51364790