WCF包括3部分:client(需要连接到哪里,需要调用什么),service(宿主,及其消息的公开,地址的公开),wcf服务库(提供契约名称,及其怎么干)

步骤:

1.新建wcf库,其中提供一个契约,接口:IService1  实现 类:Service1  

//接口中定义一个方法  GetData;  
namespace WcfServiceLibrary1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);

//[OperationContract]
//CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: 在此添加您的服务操作
}

实现类中对于接口中的方法进行实现:

namespace WcfServiceLibrary1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

2.新建一个控制台的服务端,需要引用system.servicemodel 和wcfservicelibrary1这两个类及其使用对应命名空间;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ServiceModel;
using WcfServiceLibrary1;

namespace service
{
class Program
{
static void Main(string[] args)
{
//服务行为的定义
System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(Service1));

////添加服务节点的地址
//host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "http://127.0.0.1:1920/Service1");

////数据发布

//if (host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>()==null)
//{
// //创建服务行为
// System.ServiceModel.Description.ServiceMetadataBehavior behavior = new System.ServiceModel.Description.ServiceMetadataBehavior();

// //是否发布元数据以使用http/get请求进行检索
// behavior.HttpGetEnabled = true;

// //把地址记录上去
// behavior.HttpGetUrl = new Uri("http://127.0.0.1:1920/Service1/metadata");

// //绑定到宿主上
// host.Description.Behaviors.Add(behavior);
//}

host.Opened += delegate {

Console.WriteLine("start_service...");
};

host.Open();

Console.ReadKey();

}

//对于采用Appconfig中进行读取的情况,如上代码,那么config文件如下:

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metabehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:1920/Service1/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>

<services>
<service behaviorConfiguration="metabehavior" name="WcfServiceLibrary1.Service1">
<endpoint address="http://127.0.0.1:1920/Service1"
binding="wsHttpBinding" bindingConfiguration=""
contract="WcfServiceLibrary1.IService1">

</endpoint>

</service>
</services>
</system.serviceModel>

3.对于客户端,有两种方式一种引用服务,另一种是通过ChannelFactory进行调用的;

第一看引用服务的client:

需要引用类system.servicemodel同样要被引用;

其次需要右击服务引用,将上面的公开的地址(http://127.0.0.1:1920/Service1/metadata)输入,然后转到,比如生成的命名空间为上面的:ServiceReference1

然后代码如下:

namespace client
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

Console.WriteLine(client.GetData(4));

Console.ReadKey();

最后config中由于添加服务引用会自动生成一些关于servicemodel的config文件如下:

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>

第二种client的形式如下:

首先需要引用wcfservicelibrary1(也就是上面第一次建立的wcf类和服务端引用呢同一个) 并且添加system.servicemodel的类及其引用

namespace channel_client
{
class Program
{
static void Main(string[] args)
{
////基于地址和绑定类型创造一个对象
//ChannelFactory<IService1> client_factory = new ChannelFactory<IService1>(new WSHttpBinding(), "http://127.0.0.1:1920/Service1");

////创建服务代理对象
//IService1 proxy = client_factory.CreateChannel();

//Console.WriteLine(proxy.GetData(4));

//基于config中文件中进行读取的情况

ChannelFactory<IService1> client_factory = new ChannelFactory<IService1>("testgetdata");

IService1 proxy = client_factory.CreateChannel();

Console.WriteLine(proxy.GetData(4));

Console.ReadKey();

如果是基于配置文件的地址及其类型,那么需要配置如下:

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<client>
<endpoint name="testgetdata" address="http://127.0.0.1:1920/Service1" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">

</endpoint>
</client>
</system.serviceModel>
</configuration>

wcf_first的更多相关文章

随机推荐

  1. jquery 蔚蓝网

    $(document).ready(function(){ //提交表单 $("#registerBtn").click(function(){ var email=documen ...

  2. Shell脚本出现$'\r': command not found

    Centos7下执行shell脚本报错如下 [root@ip---- ~]# sh install_zabbix_agent.sh install_zabbix_agent.: $'\r': comm ...

  3. gitlab 安装和使用

    正常 团队开发 不可能吧代码托管给 github 或者码云之类的 三方托管机构. 然后  原始的 git 没有图形用户界面. 这时候我们可以选择 gitlab . 安装环境 centos7 1 安装依 ...

  4. elasticsearch -- Logstash实现mysql同步数据到elasticsearch

    配置 安装插件由于这里是从mysql同步数据到elasticsearch,所以需要安装jdbc的入插件和elasticsearch的出插件:logstash-input-jdbc.logstash-o ...

  5. SQL优化经验

    SQL 优化经验总结34条   我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享!   (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效 ...

  6. 作业---修改haproxy配置文件

    #查询 f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding="utf-8") ha ...

  7. 用crontab部署定时任务

    1.(centos)安装crontab服务 yum install crontabs 2.启动.停止.重启.重载服务 /sbin/service crond start /sbin/service c ...

  8. HTML 理解标签 - meta

    meta标签 , 常用的有以下几个属性: 1. name : 比较常见的一些属性 author: 就是这个文档的作者名称,可以用自由的格式去定义 description: 包括一个关于页面内容的缩略而 ...

  9. HTML5 script 标签的 crossorigin 和integrity属性的作用

    Bootstrap 4 依赖的基础库中出现了两个新的属性 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.m ...

  10. 通用mapper版+SpringBoot+MyBatis框架+mysql数据库的整合

    转:https://blog.csdn.net/qq_35153200/article/details/79538440 开发环境: 开发工具:Intellij IDEA 2017.2.3 JDK : ...