WCF基本概念&以winfrom为宿主发布WCF

本文介绍了Windows Communication Foundation(WCF)的基本概念,包括地址、绑定和契约,并展示了如何在WinForm应用中作为服务宿主发布WCF。通过创建WCF服务库项目,设置不同类型的绑定(如BasicHttpBinding、WSHttpBinding和NetTcpBinding),并在WinForm应用中启动和关闭服务。此外,还说明了客户端如何通过添加服务引用来调用WCF服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.WCF的基本概念  (ABC)

A 地址 Address  服务对外的地址  如: http://192.168.0.73:8888/WCFServer/UserService/ 

B 绑定 Binding  决定服务的细节   常用的有
  BasicHttpBinding 用http进行传输,数据格式为text/xml
  WSHttpBinding    比BasicHttpBinding更加安全
  NetTcpBinding    最适合做跨机器的安全绑定

C 契约 Contract 服务的定义(抽象) ,对应服务的接口 如: IUserService

 eg: WCF的一个配置(配置文件一般在新建contract时生成)

  <system.serviceModel>
    <bindings />
    <client />
    <services>
      <service name="WCFServer.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.0.73:8888/WCFServer/Service1/"   />
          </baseAddresses>
        </host>
        <endpoint address=""  binding="wsHttpBinding" contract="WCFServer.IService1">
        </endpoint>
      </service>

      <service name="WCFServer.UserService">
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.0.73:8888/WCFServer/UserService/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="wsHttpBinding" contract="WCFServer.IUserService"></endpoint>
      </service>
     
    </services>
    <behaviors>

      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false 并删除上面的元数据终结点  -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
            以避免泄漏异常信息-->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>



2. 以winform程序作为WCF的契约宿主

 1. 新建一个WCF服务库项目,在里面可以新建契约(IUserService)和实现(UserService),新建完成后会在app.config中生成 WCF的配置文件。
    
     
 2. winfrom程序启动WCF

 (1)新建winfom项目
  
namespace WCFServerHost
{
    public partial class Form1 : Form
    {
        
        private ServiceHost serviceHost = null;
        private ServiceHost userHost = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string btnText = button1.Text.Trim();
            if (btnText.Equals("启动服务"))
            {
                if (null == serviceHost && null == userHost)
                {
                 serviceHost = new ServiceHost(typeof(WCFServer.Service1));
                 userHost = new ServiceHost(typeof(WCFServer.UserService));  //定义了两个契约,都需要启动
                 //绑定 Binding
                 System.ServiceModel.Channels.Binding httpBinding = new BasicHttpBinding();
                 //终结点 EndPoint 
                 serviceHost.AddServiceEndpoint(typeof(WCFServer.IService1), httpBinding, "http://192.168.0.73:8888/WCFServer/MyService1/");
                 userHost.AddServiceEndpoint(typeof(WCFServer.IUserService), httpBinding, "http://192.168.0.73:8888/WCFServer/MyUserService/");
                 
                 serviceHost.Open(); //启动服务
                 userHost.Open();
                 button1.Text = "停止服务";
                 lblResult.Text = "服务已经启动";
                }
            }
            else {
                serviceHost.Close();
                userHost.Close();
                serviceHost = null;
                userHost = null;
                button1.Text = "启动服务";
                lblResult.Text = "服务已经停止";
            }
        }
    }
}

 (2) 将WCF服务库中的app.config 拷贝到winform项目下,并将WCF服务库中的app.config配置文件删除

 (3) 运行winFrom工程下的 bin/debug 下面的 exe(这一步是为了生成客户端代理,需要启动Host)  

3  客户端调用, 新建一个控制台程序,添加服务引用 ,如:http://192.168.0.73:8888/WCFServer/Service1/
     调用如下:

     
namespace WCFClient
{
    class Program
    {
        static void Main(string[] args)
        {
            WCFServer1.IService1 wcfServerClient = new WCFServer1.Service1Client("BasicHttpBinding_IService1");
            string result =  wcfServerClient.GetData(123);
            Console.WriteLine(result);

            UserService.IUserService userServiceClient = new UserService.UserServiceClient("BasicHttpBinding_IUserService");
            int userId = 0 ;
            int responseId = userServiceClient.CheckUser(out userId, "test", "123456");
            if(responseId==1){
                Console.WriteLine(userId);
            }
            Console.ReadLine();
        }
    }
}

     
   
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sust2012

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

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

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

打赏作者

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

抵扣说明:

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

余额充值