通过添加服务引用后生成的代码,可以得知首先要设置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
 

.net core 下调用.net framework框架的WCF方法写法的更多相关文章

  1. 一个.NET Core下的开源插件框架

    插件模式历史悠久,各种中大型软件基本上都会实现插件机制,以此支持功能扩展,从开发部署层面,插件机制也可实现功能解耦,对于并行开发.项目部署.功能定制等都有比较大的优势. 在.NET Core下,一般我 ...

  2. .NET Core 下调用WebAPI

    前言 今天我们介绍多种客户端调用WebApi的方式,可以是原生写的,也可以借助.NET 框架下的其他HTTP库.我们一起来看看它们之间的一些异同吧- RestSharp 首先要介绍的就是这款REST ...

  3. python-django rest framework框架之dispatch方法源码分析

    1.Django的 CBV 中在请求到来之后,都要执行dispatch方法,dispatch方法根据请求方式不同触发 get/post/put等方法 class APIView(View): def ...

  4. 如何在ASP.NET Core中应用Entity Framework

    注:本文提到的代码示例下载地址> How to using Entity Framework DB first in ASP.NET Core 如何在ASP.NET Core中应用Entity ...

  5. Net Core下多种ORM框架特性及性能对比

    在.NET Framework下有许多ORM框架,最著名的无外乎是Entity Framework,它拥有悠久的历史以及便捷的语法,在占有率上一路领先.但随着Dapper的出现,它的地位受到了威胁,本 ...

  6. Orchard Core Framework:ASP.NET Core 模块化,多租户框架

    Orchard Core Framework:ASP.NET Core 模块化,多租户框架 上一篇编写Orchard Core一分钟搭建ASP.NET Core CMS ,介绍ASP.NET Core ...

  7. .NET Core下的日志(1):记录日志信息

    记录各种级别的日志是所有应用不可或缺的功能.关于日志记录的实现,我们有太多第三方框架可供选择,比如Log4Net.NLog.Loggr和Serilog 等,当然我们还可以选择微软原生的诊断机制(相关A ...

  8. 简析.NET Core 以及与 .NET Framework的关系

    简析.NET Core 以及与 .NET Framework的关系 一 .NET 的 Framework 们 二 .NET Core的到来 1. Runtime 2. Unified BCL 3. W ...

  9. 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】

    基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...

随机推荐

  1. 剧本--ansible

    剧本不喜欢, 1.1 编写剧本规范:(PYyaml语法格式文件) 剧本中有层级划分 每个层级都要用两个空格进行区分 第一级标题 第二级标题 第三级标题 强调注意:一定使用ansible软件配置剧本时, ...

  2. 5.SpringMVC

    1.SpringMVC概述 概述: SpringMVC是基于请求驱动,围绕一个核心Servlet 转发请求到对应的Controller而设计的优点:是一个典型的教科书式的MVC构架,易学易用提供了清晰 ...

  3. spring配置文件中util:properties和context:property-placeholder

    util:properties和context:property-placeholder标签都可以用来获取外部配置文件中的内容 1.util:properties 它是以声明bean方式来使用,创建了 ...

  4. Head First 设计模式笔记(适配器)

    1.定义: 将一个类的接口转换成客户期望的另外一个接口.适配器让原来不兼容的类可以合作无间. 例子:插座转接头. 2.类图: 3.说明: 埋坑 4.例子 埋坑

  5. java编程--03介绍关于日期常用的计算

    /** * 获取2个日期之间的天数差 * d2-d1 * @return * @throws Exception * @Description: */ public static int getDif ...

  6. Murano Weekly Meeting 2015.09.15

    Meeting time: 2015.September.15th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting s ...

  7. 使用codedom自动生成代码

    刚刚接触自动代码生成,便小试牛刀,解决了项目中的一些问题. 问题:我们的项目分成很多层次,当增加一个方法的时候,会显得异常繁琐,但每个层次之间的调用大同小异,所以尝试使用代码生成.现在假设有Engin ...

  8. Hash表的原理

    哈希的概念:Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩 ...

  9. React.js 小书 Lesson3 - 前端组件化(二):优化 DOM 操作

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson3 转载请注明出处,保留原文链接和作者信息. 看看上一节我们的代码,仔细留意一下 change ...

  10. linq 两个字段排序

    在linq中排序方法有: OrderBy()  --对某列升序排序 ThenBy()    --某列升序后对另一列后续升序排序 OrderByDescending() --对某列降序排序 ThenBy ...