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的更多相关文章
随机推荐
- VBA随机地牢生成
无聊啊--于是,我想做一个随机地图. 但是我很懒,不想做. 但是身体很诚实. 这次是直接在Excel中制作的地图,但是,VB的执行效率很慢,我代码的效率也很慢,导致,一旦地图长宽稍大,就会出现好几分钟 ...
- [转]在ASP.NET Core中使用百度在线编辑器UEditor
原文地址:https://www.cnblogs.com/durow/p/6116393.html 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服 ...
- 使用excel 数据透视表画图
① 打开Excel,选中需要制表的数据,点击“插入”->“数据透视表” ② 出现下列对话框,点击“确定” ③ 再新的“sheet”表内对“数 ...
- JavaScript的定时器如何先触发一次再延时
var data3=0; (function count3(){ console.log("count3:",data3++); setTimeout(count3,1000); ...
- JS将/Date(1446704778000)/转换成str
JS将/Date(1446704778000)/转换成str:var dateStr = eval(ele.add_time.replace(/\/Date\((\d+)\)\//gi, " ...
- MVC5 方法扩展
public static MvcHtmlString DataDictionaryDropDownList(this HtmlHelper htmlHelper, string name, obje ...
- 用matalb、python画聚类结果图
用matlab %读入聚类后的数据, 已经分好级别了,例如前4行是亚洲一流, %-13是亚洲二流,-24是亚洲三流 a=xlsread('C:\Users\Liugengxin\Desktop\1.x ...
- Xorboot-UEFI新手入门教程
Xorboot-UEFI新手入门教程 Xorboot-UEFI是一款UEFI下轻量级的图形化多系统引导程序,pauly于2014年国庆节期间发布了预览版.搜了下论坛,关于Xorboot- ...
- Opencv + opencv_contrib + Tesseract 之Qt开发环境搭建
1.软件包准备 opencv源码包地址: 官网 github opencv_contrib源码包地址: github Tesseract源码包地址: ...
- 使用docker加载已有镜像安装Hyperledger Fabric v1.1.0
背景 每次在新的服务器上安装Hyperledger Fabric网络时,通过fabric官方提供的脚本安装时,需要从网络上down下近10G的fabric相关镜像,这个过程是漫长及痛苦的,有时因网络问 ...