Wcf简单实例1
一、客户端添加服务引用,并调用
1.使用客户端代理同步调用
static void TestTwo() { /*********同步访问********/ Person.PersonServiceClient client = new Person.PersonServiceClient(); string result = client.DoWork(new[] { 1, 3, 5 }); Console.WriteLine(result); DateTime serverTime = client.GetServerTime(); Console.WriteLine(serverTime); Dictionary<string, int> dic = client.GetDic(); foreach (var item in dic) { Console.WriteLine(item.Key + ":" + item.Value); } //服务店List集合,对应客户端数组 string[] strList = client.GetListStr(); Console.WriteLine(string.Join(",", strList)); Person.Person[] list = client.GetList(); foreach (var item in list) { Console.WriteLine(item.ID + "-" + item.Name + "-" + item.Birthday); } //关闭连接 client.Close(); }
2.使用客户端代理异步调用
static void TestThree() { /******异步访问******/ Person.PersonServiceClient client = new Person.PersonServiceClient(); Task<string> result = client.DoWorkAsync(new[] { 1, 3, 5 }); Task<DateTime> serverTime = client.GetServerTimeAsync(); Console.WriteLine(result.Result); Console.WriteLine(serverTime.Result); //关闭连接 client.Close(); Console.WriteLine("end"); }
客户端配置:

<system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IService1" /> <binding name="BasicHttpBinding_IPersonService" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:60957/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceOne.IService1" name="BasicHttpBinding_IService1" /> <endpoint address="http://localhost:60957/PersonService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPersonService" contract="Person.IPersonService" name="BasicHttpBinding_IPersonService" /> </client> </system.serviceModel>
二、接口契约和数据契约
[ServiceContract] public interface IPersonService { [OperationContract] string DoWork(int[] numbers); [OperationContract] DateTime GetServerTime(); [OperationContract] List<string> GetListStr(); [OperationContract] List<Person> GetList(); [OperationContract] Dictionary<string, int> GetDic(); } [DataContract] public class Person { [DataMember] public int ID { get; set; } [DataMember] public string Name { get; set; } [DataMember] public DateTime Birthday { get; set; } }
三、接口实现

public class PersonService : IPersonService { public string DoWork(int[] numbers) { return string.Join("|", numbers); } public Dictionary<string, int> GetDic() { Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("one", 1); dic.Add("two", 2); return dic; } public List<Person> GetList() { List<Person> list = new List<Person>(); list.Add(new Person() { ID = 1, Name = "张三", Birthday = new DateTime(1990, 1, 1) }); list.Add(new Person() { ID = 2, Name = "李四", Birthday = new DateTime(1991, 1, 1) }); return list; } public DateTime GetServerTime() { return DateTime.Now; } public List<string> GetListStr() { List<string> list = new List<string>(); list.Add("张三"); list.Add("王芳"); return list; } }
服务端使用默认配置
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2014-04-28 修改注册表来修改IE的设置---资料汇总