.net core 下调用.nett framework框架的WCF方法写法
通过添加服务引用后生成的代码,可以得知首先要设置Basic连接写法的属性,并且设置WCF服务的地址:
我在这里建立工厂类如下:
using System; using System.ServiceModel; using System.ServiceModel.Channels; using ShoppingMall.Enums; namespace ShoppingMall.ClientBll { public class EndpointFactory { private static Binding GetBindingForEndpoint(EndpointConfigurationEnums endpointConfiguration) { if ((endpointConfiguration == EndpointConfigurationEnums.BasicHttpBinding_ICustomerService)) { BasicHttpBinding result = new BasicHttpBinding(); result.MaxBufferSize = int.MaxValue; result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max; result.MaxReceivedMessageSize = int.MaxValue; result.AllowCookies = true; return result; } throw new InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration)); } private static EndpointAddress GetEndpointAddress(EndpointConfigurationEnums endpointConfiguration) { if ((endpointConfiguration == EndpointConfigurationEnums.BasicHttpBinding_ICustomerService)) { return new EndpointAddress("http://你的地址"); } throw new InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration)); } public static Binding GetDefaultBinding() { return GetBindingForEndpoint(EndpointConfigurationEnums.BasicHttpBinding_ICustomerService); } public static EndpointAddress GetDefaultEndpointAddress() { return GetEndpointAddress(EndpointConfigurationEnums.BasicHttpBinding_ICustomerService); } } }
然后,在客户端调用时需要调用类继承ClientBase类并且继承WCF的接口,该类的类型是服WCF接口的类型
并且要再客户端调用类的构造参数中继承ClientBase的构造方法,实例化Basic以及WCF服务的地址
最后通过继承ClientBase的Channel方式调用服务方法,完全代码如下:
using Microsoft.Extensions.Configuration; using ShoppingMall.IService; using ShoppingMall.Model; using System.Collections.Generic; using System.ServiceModel; namespace ShoppingMall.ClientBll { public class CustomerBll:ClientBase<ICustomerService>, ICustomerService { public IConfigurationRoot configuration; public CustomerBll() : base(EndpointFactory.GetDefaultBinding(), EndpointFactory.GetDefaultEndpointAddress()) { } public List<Buyer> GetBuyerList() { return Channel.GetBuyerList(); } public bool InsertBuyerInfo(Buyer info) { return Channel.InsertBuyerInfo(info); } } }
其次,WCF的服务接口应该有对应标记,例如:
using ShoppingMall.Model; using System.Collections.Generic; using System.ServiceModel; namespace ShoppingMall.IService { /// <summary> /// 用户信息处理服务 /// </summary> [ServiceContract] public interface ICustomerService { /// <summary> /// 获取买家列表 /// </summary> /// <returns></returns> [OperationContract] List<Buyer> GetBuyerList(); /// <summary> /// 添加买家信息 /// </summary> /// <returns></returns> [OperationContract] bool InsertBuyerInfo(Buyer info); } }
这样,接口标记:
<span>ServiceContract。<br />方法标记:<br />
<span>OperationContract
<span>
相关推荐
joynet00 2020-06-04
似水流年梦 2019-12-09
Bonrui编程路 2019-12-02
yangyong 2013-06-02
草原上一间小屋 2013-03-29
ajaxDreamer 2011-09-20
slivelight 2011-05-26
lijinjinxuzhou 2011-05-26
llzzyy 2018-12-20
liuyiy 2012-11-20
标绘交流群 2011-09-23
甲骨园 2011-07-04
酷讯科技 2010-05-28
luobailian 2009-12-08
banli0 2009-12-07
老骥伏枥 2009-12-07
jasonoiu 2009-11-09
xienue 2009-11-06
jiaoly 2009-11-06