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的更多相关文章
随机推荐
- ShellSort
#include <bits/stdc++.h> using namespace std; #define MAXSIZE 200000 typedef int KeyType; type ...
- 关于地形altas的处理
前几天在群里跟人讨论地形atlas的问题,因为Blade也是用的4x4的atlas(16张纹理),但是大概是几年前做的,所以一些细节忘记了,在这里做下备忘. 1.atlas就是图集,图册,把多个纹理打 ...
- JS制作图片切换
<!DOCTYPE html> <html> <head> <title>纯JS制作简单的图片切换</title> <meta cha ...
- IO文件流
定义:流是一种抽象的概念,通过流的方式组成无结构字符和字符序列,从流中取数据的操作进行输入输出.[io流的作用就是用流的方式进行输入输出] 常用语法: 1. 首先引用using.system.io ...
- JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系
郭晨 软件151 1531610114 [整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 visio文件下载 概述 一个JavaEE的项 ...
- 18.14 构建Linux根文件系统
18.14.1 Busybox1.7.0之init程序分析 1.读取配置文件 2.解析配置文件 3.执行用户程序(根据配置文件中指定的内容) 配置文件: 1.指定应用程序 2.何时执行 busybox ...
- C语言之一维数组与指针
一维数组: 假如有一维数组如下: ]; 该数组有3个元素,数据类型为char型,地址空间如下. 如果想访问数据,直接使用a[0].a[1].a[2]取出相应地址空间的值即可 一级指针: 指针即地址,c ...
- 【java】package
总结: 包与包之间进行访问,被访问的包中的类以及类中的成员,需要public修饰. 不同包中的子类还可以直接访问父类中被protected权限修饰的成员. 包与包之间可以使用的权限只有两种,publi ...
- Git从库中移除已删除大文件
写在前面大家一定遇到过在使用Git时,不小心将一个很大的文件添加到库中,即使删除,记录中还是保存了这个文件.以后不管是拷贝,还是push/pull都比较麻烦.今天在上传工程到github上,发现最大只 ...
- HTML 块级元素 行内元素
块级元素 - block level element 总是在新行上开始: 高度,行高以及外边距和内边距都可控制: 宽度缺省是它的容器的100%,除非设定一个宽度: 它可以容纳内联元素和其他块元素 如: ...