通过添加服务引用后生成的代码,可以得知首先要设置Basic连接写法的属性,并且设置WCF服务的地址:

我在这里建立工厂类如下:

  1. using System;
  2. using System.ServiceModel;
  3. using System.ServiceModel.Channels;
  4. using ShoppingMall.Enums;
  5.  
  6. namespace ShoppingMall.ClientBll
  7. {
  8. public class EndpointFactory
  9. {
  10. private static Binding GetBindingForEndpoint(EndpointConfigurationEnums endpointConfiguration)
  11. {
  12. if ((endpointConfiguration == EndpointConfigurationEnums.BasicHttpBinding_ICustomerService))
  13. {
  14. BasicHttpBinding result = new BasicHttpBinding();
  15. result.MaxBufferSize = int.MaxValue;
  16. result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
  17. result.MaxReceivedMessageSize = int.MaxValue;
  18. result.AllowCookies = true;
  19. return result;
  20. }
  21. throw new InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration));
  22. }
  23.  
  24. private static EndpointAddress GetEndpointAddress(EndpointConfigurationEnums endpointConfiguration)
  25. {
  26. if ((endpointConfiguration == EndpointConfigurationEnums.BasicHttpBinding_ICustomerService))
  27. {
  28. return new EndpointAddress("http://你的地址");
  29. }
  30. throw new InvalidOperationException(string.Format("找不到名称为“{0}”的终结点。", endpointConfiguration));
  31. }
  32.  
  33. public static Binding GetDefaultBinding()
  34. {
  35. return GetBindingForEndpoint(EndpointConfigurationEnums.BasicHttpBinding_ICustomerService);
  36. }
  37.  
  38. public static EndpointAddress GetDefaultEndpointAddress()
  39. {
  40. return GetEndpointAddress(EndpointConfigurationEnums.BasicHttpBinding_ICustomerService);
  41. }
  42. }
  43.  
  44. }

然后,在客户端调用时需要调用类继承ClientBase类并且继承WCF的接口,该类的类型是服WCF接口的类型

并且要再客户端调用类的构造参数中继承ClientBase的构造方法,实例化Basic以及WCF服务的地址

最后通过继承ClientBase的Channel方式调用服务方法,完全代码如下:

  1. using Microsoft.Extensions.Configuration;
  2. using ShoppingMall.IService;
  3. using ShoppingMall.Model;
  4. using System.Collections.Generic;
  5. using System.ServiceModel;
  6.  
  7. namespace ShoppingMall.ClientBll
  8. {
  9. public class CustomerBll:ClientBase<ICustomerService>, ICustomerService
  10. {
  11. public IConfigurationRoot configuration;
  12. public CustomerBll() : base(EndpointFactory.GetDefaultBinding(), EndpointFactory.GetDefaultEndpointAddress())
  13. {
  14.  
  15. }
  16.  
  17. public List<Buyer> GetBuyerList()
  18. {
  19. return Channel.GetBuyerList();
  20. }
  21.  
  22. public bool InsertBuyerInfo(Buyer info)
  23. {
  24. return Channel.InsertBuyerInfo(info);
  25. }
  26.  
  27. }
  28. }

其次,WCF的服务接口应该有对应标记,例如:

  1. using ShoppingMall.Model;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4.  
  5. namespace ShoppingMall.IService
  6. {
  7. /// <summary>
  8. /// 用户信息处理服务
  9. /// </summary>
  10. [ServiceContract]
  11. public interface ICustomerService
  12. {
  13. /// <summary>
  14. /// 获取买家列表
  15. /// </summary>
  16. /// <returns></returns>
  17. [OperationContract]
  18. List<Buyer> GetBuyerList();
  19.  
  20. /// <summary>
  21. /// 添加买家信息
  22. /// </summary>
  23. /// <returns></returns>
  24. [OperationContract]
  25. bool InsertBuyerInfo(Buyer info);
  26. }
  27.  
  28. }

这样,接口标记:

  1. ServiceContract
    方法标记:
  1. OperationContract
  1.  

.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. django ORM 连表查询2

    set() 更新model对象的关联对象 book_obj=models.Book.objects.first() book_obj.authors.set([2,3]) 把book_obj这个对象重 ...

  2. sqlserver 服务器监控

    1.表锁 查看被锁的表:select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from ...

  3. JWT(JSON Web Token)原理简介

    原文:http://www.fengchang.cc/post/114 参考了一下这篇文章:https://medium.com/vandium-software/5-easy-steps-to-un ...

  4. linux 6 安装 使用 XtraBackup

    帮助文档:https://www.cnblogs.com/imweihao/p/7290026.html ---Yum安装 官网地址:https://www.percona.com/doc/perco ...

  5. Spring注入的反射解释

    对于如下配置片段:  <bean id="id" class="lee.Aclass">  <!--property配置需要依赖注入的属性-- ...

  6. OS---文件结构

    1.概述 1.1 对于任何一个文件,都存在以下2种形式结构: 文件的逻辑结构: 从用户的角度出发所观察到的文件组织形式,独立于文件的物理特性: 文件的物理结构(文件存储结构): 文件在外存上的存储组织 ...

  7. DexClassLoader和PathClassLoader

    Android的ClassLoader体系 在Android中可以跟java一样实现动态加载jar,但是Android使用Dalvik VM,不能直接加载java打包jar的byte code,需要通 ...

  8. windows 7下安装tomcat6 web服务器

    因为项目中要使用Mondrian提供ROLAP应用,而Mondrian是运行在tomcat上的. 一. 软件获取: http://tomcat.apache.org/ 二. 安装步骤: 运行可执行程序 ...

  9. 04-spring中的aop演示

    1 xml配置 1 导包 2 准备对象 package www.test.service; public interface UserService { public abstract void ad ...

  10. Linux apache 添加 mod_rewrite模块

    apache已安装完毕,手动添加mod_rewrite模块  #find . -name mod_rewrite.c //在apache的源码安装目录中寻找mod_rewrite.c文件 #cd mo ...