.net core 下调用.net 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);
    }


}

这样,接口标记:

ServiceContract。
方法标记:
OperationContract
 
posted @ 2018-05-16 22:49  洛晨随风  阅读(649)  评论(0编辑  收藏  举报