wcf_first
WCF包括3部分:client(需要连接到哪里,需要调用什么),service(宿主,及其消息的公开,地址的公开),wcf服务库(提供契约名称,及其怎么干)
步骤:
1.新建wcf库,其中提供一个契约,接口:IService1 实现 类:Service1
//接口中定义一个方法 GetData;
namespace WcfServiceLibrary1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
//[OperationContract]
//CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: 在此添加您的服务操作
}
实现类中对于接口中的方法进行实现:
namespace WcfServiceLibrary1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
2.新建一个控制台的服务端,需要引用system.servicemodel 和wcfservicelibrary1这两个类及其使用对应命名空间;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace service
{
class Program
{
static void Main(string[] args)
{
//服务行为的定义
System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(Service1));
////添加服务节点的地址
//host.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "http://127.0.0.1:1920/Service1");
////数据发布
//if (host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>()==null)
//{
// //创建服务行为
// System.ServiceModel.Description.ServiceMetadataBehavior behavior = new System.ServiceModel.Description.ServiceMetadataBehavior();
// //是否发布元数据以使用http/get请求进行检索
// behavior.HttpGetEnabled = true;
// //把地址记录上去
// behavior.HttpGetUrl = new Uri("http://127.0.0.1:1920/Service1/metadata");
// //绑定到宿主上
// host.Description.Behaviors.Add(behavior);
//}
host.Opened += delegate {
Console.WriteLine("start_service...");
};
host.Open();
Console.ReadKey();
}
//对于采用Appconfig中进行读取的情况,如上代码,那么config文件如下:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metabehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:1920/Service1/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metabehavior" name="WcfServiceLibrary1.Service1">
<endpoint address="http://127.0.0.1:1920/Service1"
binding="wsHttpBinding" bindingConfiguration=""
contract="WcfServiceLibrary1.IService1">
</endpoint>
</service>
</services>
</system.serviceModel>
3.对于客户端,有两种方式一种引用服务,另一种是通过ChannelFactory进行调用的;
第一看引用服务的client:
需要引用类system.servicemodel同样要被引用;
其次需要右击服务引用,将上面的公开的地址(http://127.0.0.1:1920/Service1/metadata)输入,然后转到,比如生成的命名空间为上面的:ServiceReference1
然后代码如下:
namespace client
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
Console.WriteLine(client.GetData(4));
Console.ReadKey();
最后config中由于添加服务引用会自动生成一些关于servicemodel的config文件如下:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
第二种client的形式如下:
首先需要引用wcfservicelibrary1(也就是上面第一次建立的wcf类和服务端引用呢同一个) 并且添加system.servicemodel的类及其引用
namespace channel_client
{
class Program
{
static void Main(string[] args)
{
////基于地址和绑定类型创造一个对象
//ChannelFactory<IService1> client_factory = new ChannelFactory<IService1>(new WSHttpBinding(), "http://127.0.0.1:1920/Service1");
////创建服务代理对象
//IService1 proxy = client_factory.CreateChannel();
//Console.WriteLine(proxy.GetData(4));
//基于config中文件中进行读取的情况
ChannelFactory<IService1> client_factory = new ChannelFactory<IService1>("testgetdata");
IService1 proxy = client_factory.CreateChannel();
Console.WriteLine(proxy.GetData(4));
Console.ReadKey();
如果是基于配置文件的地址及其类型,那么需要配置如下:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<client>
<endpoint name="testgetdata" address="http://127.0.0.1:1920/Service1" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
</endpoint>
</client>
</system.serviceModel>
</configuration>
wcf_first的更多相关文章
随机推荐
- python随笔--复习专用
<!doctype html> blockquote:first-child, #write > div:first-child, #write > figure:first- ...
- java web(一):tomcat服务器的安装和简单介绍,与eclipse关联
一:下载tomcat安装包和安装 这个百度一下就可以了. 安装完以后进入tomcat的安装路径查看 如图所示:有几个目录简单介绍下 bin目录: 存放运行tomcat服务器的相关命令. conf目 ...
- tomcat使用自签名证书实现https加密访问
部署好java环境和tomcat之后 执行以下语句 #生成证书,keytool是java工具命令,-genkey生成证书,-alias证书名称,-keyalg应该是指算法,-keystore是证书存储 ...
- Java高级特性 第11节 JUnit 3.x和JUnit 4.x测试框架
一.软件测试 1.软件测试的概念及分类 软件测试是使用人工或者自动手段来运行或测试某个系统的过程,其目的在于检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别.它是帮助识别开发完成(中间或最终 ...
- DNS 负载均衡
相关文章: 文章 网址 一个域名可以绑定多个IP吗?由此引发的调查 https://ask.zkbhj.com/?/article/139
- 18.18 Datasheet Note
18.18.1 DM9000A datasheet Ethernet Controller with General Processor Interface Ethernet interface pr ...
- Spring Boot 全局异常处理
Spring Boot版本 1.5 @ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExcept ...
- Windows 10的最新版1803版本ISO下载
Windows 10推出已经有几年时间了,笔者一直在用这个新版本.据说Windows 10以后只会推出新的更新,而不会有新的操作系统推出,所以Windows 10的更新就显得重要了.这次给大家推荐一个 ...
- Hanlp自然语言处理工具之词法分析器
本章是接前两篇<分词工具Hanlp基于感知机的中文分词框架>和<基于结构化感知机的词性标注与命名实体识别框架>的.本系统将同时进行中文分词.词性标注与命名实体识别3个任务的子系 ...
- 一维信号频谱图仿真——matlab
程序1: %在MATLAB中是用连续信号在等时间间隔点的样值来近似地表示连续信号的,当采样时间间隔足够小时,这些离散的采样值就能较好地近似出连续信号,matlab中连续信号的显示实际上还是离散信号的显 ...