UI层调用WCF服务实例(源码)

  WCF原理性的东西,暂时还没有深入研究,只是在公司的项目中使用到了,会调用,然后再多做了一些了解,现在将它抽出来了一个小实例,写了一个WCF的demo。

  我写的这个WCF.Demo主要包括数据契约和服务契约,UI客户端层和Host宿主层,基于http和net.tcp两种协议通信。

  不多说,直接贴一张层次图片先,最后提供源码下载。

  

 

  数据契约层(DataContracts)代码:

  

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

namespace WCF.DataContracts
{
    [DataContract()]
    public class CustomerData
    {
        [DataMember()]
        public string Name { set; get; }
        [DataMember()]
        public string Sex { set; get; }
    }
}

 

服务契约接口层(WCF.ServiceContracts)

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

namespace WCF.ServiceContracts
{
    [ServiceContract()]
    public interface ICustomerService
    {
        [OperationContract()]
        string ShowInfo();

    }
}

服务契约实现层(WCF.Services)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCF.DataContracts;
using WCF.ServiceContracts;

namespace WCF.Services
{
    public class CustomerService : ICustomerService
    {
        public string ShowInfo()
        {
            CustomerData data = new CustomerData()
            {
                Name = "admin",
                Sex = ""
            };

            return "姓名是:" + data.Name + ",性别是:" + data.Sex;
        }
    }
}

宿主层(两个文件)

Program

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

namespace WCF.Hosting
{
    class Program
    {
        static void Main(string[] args)
        {

            ServiceHost hostForHello = new ServiceHost(typeof(CustomerService));


            hostForHello.Open();
            Console.WriteLine("WCF服务启动成功!");
            Console.ReadLine();

        }
    }
}

app.config

<?xml version="1.0"?>

<configuration>

  <system.serviceModel>

    <services>

      <service name="WCF.Services.CustomerService" behaviorConfiguration="mex">
        <host>
          <baseAddresses>
            <add baseAddress="http://192.168.1.100:64566/CustomerService"/>
            <add baseAddress="net.tcp://192.168.1.100:64567/CustomerService"/>

          </baseAddresses>
        </host>
        <endpoint binding="wsDualHttpBinding" contract="WCF.ServiceContracts.ICustomerService" address="mex" /> 
        <endpoint address="net.tcp://192.168.1.100:64567/CustomerService" binding="netTcpBinding"
                  bindingConfiguration="tcpWindowsSecurity" name="helloEndPoint"
                  contract="WCF.ServiceContracts.ICustomerService"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />

      </service>

    </services>

    <bindings>



      <netTcpBinding>

        <binding name="tcpWindowsSecurity">

        </binding>

      </netTcpBinding>

    </bindings>



    <behaviors>
      <serviceBehaviors>
        <behavior name="mex">
          <serviceMetadata  />
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>



  <startup>

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

  </startup>

</configuration>

客户端调用层(WCF.Client)

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCF.ServiceContracts;

namespace WCF.Client
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            using (ChannelFactory<ICustomerService> channelFactory = new ChannelFactory<ICustomerService>("helloEndPoint"))
            {
                ICustomerService helloService = channelFactory.CreateChannel();
                using (helloService as IDisposable)
                {
                    TextBox1.Text = helloService.ShowInfo();
                }
            }
        }
    }
}

客户端配置文件

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <system.serviceModel>

    <bindings>

      <netTcpBinding>

        <binding name="tcpWindowsSecurity">

        </binding>

      </netTcpBinding>

    </bindings>

    <client>

      <endpoint name="helloEndPoint" address="net.tcp://192.168.1.100:64567/CustomerService"

          binding="netTcpBinding" bindingConfiguration="tcpWindowsSecurity"

          contract="WCF.ServiceContracts.ICustomerService" />

    </client>

  </system.serviceModel>
  
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

</configuration>

源码下载

转载于:https://www.cnblogs.com/renzaijianghu/p/3493264.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值