WCF 请求答复模式

本文主要介绍如何使用请求答复模式

在这里插入图片描述

 一、Service端

1、定义一个接口,并且添加一个ServiceContract特性,并且在方法上天上一个OperationContract特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Mytest
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口“IFlyService”。
    [ServiceContract]
    public interface IFlyService
    {
        [OperationContract]
        string Fly();
    }
}

2、定义一个实现类,继承于第一步的IFlyService接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Mytest
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“FlyService ”。
    public class FlyService : IFlyService
    {
        public string Fly()
        {
            return "You can fly";
        }
    }
}

3、定义配置文件,其中contract为接口,name为接口实现类

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service name="Mytest.FlyService">
                <endpoint address="" binding="basicHttpBinding" contract="Mytest.IFlyService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8733/Design_Time_Addresses/Mytest/Service1/" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

4、开启服务(宿主可以为控制台程序,IIS或Winform,当前选择Console控制台程序)

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

namespace Mytest
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(FlyService));
            host.Open();
            Console.WriteLine("服务启动成功");
            Console.ReadLine();
        }
    }
}

二、客户端

1、添加服务引用

 2、请求服务

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            FlyServiceClient client = new FlyServiceClient();
            string result = client.Fly();
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

运行结果如图

当前为basicHttpBinding通信模式可以改为netTcpBinding并且修改address

 <endpoint address="net.tcp://localhost/my/service" binding="netTcpBinding" contract="Mytest.IFlyService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>

更新服务引用即可

三、MSMQ

 MSMQ(容量受硬盘大小限制)将客户端与service切开了,客户端只要将消息发送到消息队列即可,服务端自动获取消息

1、更改通信方式

 <endpoint address="net.msmq://localhost/my/service" binding="netMsmqBinding" contract="Mytest.IFlyService">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>

2、如果没有安装消息队列,首先安装windows功能,流程如下

控制面板-》windows功能-》MSMQ服务器

3、去找MSMQ:在计算机管理面板找到MSMQ,在MSMQ新建一个队列

4、必须为单工访问

 a)服务必须为返回Void

 b)IsOneWay = true

5、修改配置文件中安全性--改为None,并添加到endpoint节点 bindingConfiguration="mybinding"

		<bindings>
			<netMsmqBinding>
				<binding name="mybinding">
					<security mode="None"></security>
				</binding>
			</netMsmqBinding>
		</bindings>

6、MSMQ模式不支持端口号访问,所以address 不允许出现端口号 

7、客户端删除配置重新更新

可以总结为basicHttpBinding适合.net与其他平台通信(如java等),netTcpBinding适合两个.net程序跨机器访问,MSMQ适合构建离线访问

四、上述方式都是同步调用,下面说一下异步调用方式

1、配置服务引用改为-》生成异步操作

 2、

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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            FlyServiceClient client = new FlyServiceClient();
            client.BeginFly((obj) =>
            {
                var myclient = (FlyServiceClient)obj.AsyncState;
                myclient.EndFly(obj);
            }, client);

            //string result = client.Fly();
           /// Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个月太忙没时间看C++

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值