专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WCF

WCF高速上手(二)

发布时间:2011-06-23 13:50:12 文章来源:www.iduyao.cn 采编人员:星星草
WCF快速上手(二)

一、代码结构:

二、服务接口Contract和实体类Domain

INoticeService:

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

namespace Contract
{
    /// <summary>
    /// 通知公告
    /// </summary>
    [ServiceContract]
    public interface INoticeService
    {
        /// <summary>
        /// 获取一条公告信息
        /// </summary>
        [OperationContract]
        Notice GetNotice();
    }
}
View Code

Notice:

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

namespace Domain
{
    /// <summary>
    /// 通知公告
    /// </summary>
    public class Notice
    {
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        public byte[] Data { get; set; }
    }
}
View Code

三、服务WcfService和宿主WcfHost

NoticeService:

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

namespace WcfService
{
    /// <summary>
    /// 通知公告服务
    /// </summary>
    public class NoticeService : INoticeService
    {
        /// <summary>
        /// 获取一条公告信息
        /// </summary>
        public Notice GetNotice()
        {
            Notice notice = new Notice();
            notice.Title = "测试标题";
            notice.Content = "测试内容";
            notice.Data = new byte[200000];
            for (int i = 0; i < 200000; i++)
            {
                notice.Data[i] = (byte)100;
            }
            return notice;
        }
    }
}
View Code

Program:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WcfService;

namespace WcfServer
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "Service";
            string[] fileArr = Directory.GetFiles(path);
            int port = int.Parse(ConfigurationManager.AppSettings["ServerPort"]);

            foreach (string fileName in fileArr)
            {
                string url = string.Format("http://localhost:{0}/Service/{1}", port, Path.GetFileName(fileName));
                Uri[] uri = new Uri[] { new Uri(url) };

                ServiceHost host = new ServiceHost(typeof(NoticeService), uri);
                host.Open();
            }

            Console.WriteLine("服务成功启动");
            Console.Read();
        }
    }
}
View Code

服务端App.config配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="ServerPort" value="8998"/>
  </appSettings>
  <system.serviceModel>
    <services>
      <service name="WcfService.NoticeService" behaviorConfiguration="ServiceBehavior" >
        <endpoint  contract="Contract.INoticeService" address="" binding="basicHttpBinding" bindingConfiguration="ServiceBinding">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="ServiceBinding" sendTimeout="00:00:20" maxReceivedMessageSize="2147483646">
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
View Code

四、客户端

NoticeProxy:

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

namespace WcfClient
{
    /// <summary>
    /// 通知公告
    /// </summary>
    public class NoticeProxy : INoticeService
    {
        private ChannelFactory<INoticeService> channelFactory;
        private INoticeService server;

        public NoticeProxy()
        {
            channelFactory = new ChannelFactory<INoticeService>("NoticeService");
            server = channelFactory.CreateChannel();
        }

        /// <summary>
        /// 获取一条公告信息
        /// </summary>
        public Notice GetNotice()
        {
            return server.GetNotice();
        }
    }
}
View Code

客户端调用服务:

using Domain;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WcfClient;

namespace WinformClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            try
            {
                NoticeProxy proxy = new NoticeProxy();
                Notice notice = proxy.GetNotice();
                MessageBox.Show(notice.Data.Length.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
View Code

客户端App.config配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ServiceBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <basicHttpBinding>
        <binding name="ServiceBinding"  maxReceivedMessageSize="2147483646">
          <readerQuotas maxArrayLength="65242880" maxStringContentLength="65242880"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint name="NoticeService" contract="Contract.INoticeService"  address="http://localhost:8998/Service/NoticeService.svc" behaviorConfiguration="ServiceBehavior"  binding="basicHttpBinding" bindingConfiguration="ServiceBinding" />
    </client>
  </system.serviceModel>
</configuration>
View Code

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: