ASP WCF(跨平台跨应用程序通信)

89 篇文章 0 订阅

WCF编程学习:https://www.cnblogs.com/xfrog/archive/2010/05/12/1733713.html

WCF(Windows Communication Foundation),和WebService功能一样,用于跨平台跨应用程序通信。WCF是面向接口(契约)的。

WebService服务是部署在Web应用程序上的,IIS服务启动后,WebService就启动了。WCF可以部署在Web应用程序上,也可以部署在控制台应用程序上,也可以部署在WinForm应用程序上。当所在的应用程序启动后,WCF服务也就启动了。

添加引用:System.ServiceModel(客户端和服务端都要添加该引用)

WCF服务端:

IUserInfoService.cs(契约接口):

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;  //添加该引用
using System.Text;
using System.Threading.Tasks;

namespace IBLL
{ 
    [ServiceContract]  //服务契约(接口)
    public interface IUserInfoService
    {
        [OperationContract]  //契约(接口)
        int Add(int a,int b);
    }
}
UserInfoService.cs(实现契约接口的类):
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class UserInfoService : IUserInfoService  //实现接口(满足契约)
    {
        public int Add(int a, int b)
        {
            return a+b;
        }
    }
}
App.config(服务端的配置):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration">  <!--UserInfoService是实现契约接口的类-->
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/"/>   <!--WCF服务对外公布的地址-->
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint>  <!--契约(接口)-->
      </service>  <!--service可以有多个service服务-->
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfiguration">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
Program.cs(控制台应用程序,bin/debug/Host.exe以管理员身份运行,运行之后WCF服务就会同时启动):
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;   //添加该引用
using System.Text;
using System.Threading.Tasks;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(BLL.UserInfoService)))   
            {
                host.Open();  //启动WCF服务
                Console.ReadKey();
            }
        }
    }
}


客户端引用远程WCF服务第一种方式:

VS开发人员命令提示(以管理员身份运行)--D:--D:\Client(进入客户端应用程序的目录)--输入命令:svcutil http://127.0.0.1:8000/?wsdl /o:UserInfoServiceClient.cs (下载服务文件,并生成客户端类。127.0.0.1是WCF服务的地址,UserInfoServiceClient是自定义的类名)

选择客户端项目--显示所有文件--UserInfoServiceClient--右击--包括在项目中--output.config--右击--包括在项目中;删除App.config,将output.config重命名成App.config。UserInfoServiceClient.cs类中也要添加System.ServiceModel引用,该类中也定义了一个契约(接口),可以注释掉,使用自己定义的契约(接口)。

App.config(客户端的配置):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IUserInfoService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IUserInfoService" contract="IBLL.IUserInfoService"
                name="BasicHttpBinding_IUserInfoService" />   <!--也需要引用契约(接口)。客户端和服务端都需要契约(接口)(IBLL.IUserInfoService)-->
        </client>
    </system.serviceModel>
</configuration>

Program.cs(客户端控制台应用程序):

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

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            UserInfoServiceClient client = new UserInfoServiceClient();  //这是从远程服务端下载生成的类。
            int sum = client.Add(3,6);  //调用远程WCF服务的Add函数。契约接口中定义的函数。
            Console.WriteLine(sum.ToString());  //要保证远程的WCF服务已开启。
            Console.ReadKey();
        }
    }
}

客户端引用远程WCF服务第二种方式(和WebService的客户端的方式相同):

添加服务引用--http://localhost:8000/--确定(App.config自动生成配置)
Program.cs(客户端应用程序调用远程WCF服务的方式):

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

namespace Client2
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.UserInfoServiceClient client = new ServiceReference1.UserInfoServiceClient(); //ServiceReference1是添加服务引用时自定义的命名空间。
            int sum = client.Add(3,6);  //调用远程WCF服务的Add函数。契约接口中定义的函数。
            Console.WriteLine(sum.ToString());   //要保证远程的WCF服务已开启。
            Console.ReadKey();
        }
    }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值