WCF快速上手(二)
2024-10-16 19:09:16
服务端是CS程序,客户端(调用者)是BS程序
一、代码结构:
二、服务接口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();
}
}
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; }
}
}
三、服务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[];
for (int i = ; i < ; i++)
{
notice.Data[i] = (byte);
}
return notice;
}
}
}
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();
}
}
}
服务端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>
四、客户端
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();
}
}
}
客户端调用服务:
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);
}
}
}
}
客户端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>
最新文章
- Linux主机上使用交叉编译移植u-boot到树莓派
- 深入理解Spring--动手实现一个简单的SpringIOC容器
- centos 研究
- java URL实现调用其他系统发送报文并获取返回数据
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
- 关于int,long,long long
- Beautiful 疑问小记
- 使用socket.io开发简单群聊功能
- SharePoint 使用PowerShell恢复误删的网站集
- PHP如何让apache支持.htaccess 解决Internal Server Error The server …错误
- spring管理事务需要注意的
- django获取ajax的post复杂对象
- maven安装配置及使用maven创建一个web项目
- db mysql / mysql cluster 5.7.19 / my.cnf / thread_pool_stall_limit
- Spring+Struts+Mybatis+Shiro整合配置
- mysql5.6.13通用二进制格式安装并使用amoeba实现对mysql5.6数据库读写分离
- CF876 F 思维 枚举
- SQL数据插入字符串时转义函数
- 42. Trapping Rain Water *HARD*
- 【python爬虫】常见面试题之http协议问题总结