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的更多相关文章
随机推荐
- one-hot编码
1 get_dummies函数 有多少种不同类就会产生多少位的编码,生成的age_df 实际上是一个变量,其存储着dataframe数据类型 完全可按dataframe对其操作 age_df = pd ...
- Android 异步请求通用类
package com.example.demo1; import java.util.EventListener; public interface MyAsyncTaskListener exte ...
- R并行计算
# 参考文献: https://cosx.org/2016/09/r-and-parallel-computinghttps://blog.csdn.net/sinat_26917383/articl ...
- servlet_3
ServletContext 介绍 提供的功能 servlet中获取servletcontext实例 servletcontext接口的方法 package com.fgy; import java. ...
- day03 文件操作
目录 1.文件操作实例 2.文件常用操作 3.with模块操作文件 常用实例,把文件里面的内容读出来做成字典的形式在做成字列表展示. 1.精简版. lst = []f = open("fil ...
- python selenium-webdriver 元素操作之键盘操作(五)
上节介绍了模拟鼠标对元素的操作,本节主要介绍键盘对元素的操作,实际过程中鼠标对元素的操作比键盘对元素的操作更经常使用,但是键盘对元素的操作也很重要,本节主要介绍一下键盘对元素的操作. selenium ...
- RK3399 友善NanoPC-T4开发板使用sysfs方法控制status LED状态灯-【申嵌视频-RK3399篇】
实验1:sysfs 操作方法控制NanoPC-T4开发板上LED灯 (status LED状态灯:GPIO0_B5/LED1_OUT)root@NanoPC-T4: cd /sys/class/led ...
- 浅谈C#在网络波动时防重复提交
前几天,公司数据库出现了两条相同的数据,而且时间相同(毫秒也相同).排查原因,发现是网络波动造成了重复提交. 由于网络波动而重复提交的例子也比较多: 网络上,防重复提交的方法也很多,使用redis锁, ...
- openFileOutput和openFileInput还有FileOutStream与openFileOutput
FileOutputStream fos = openFileOutput("",MODE_PRIVATE); fos.write("private".getB ...
- (最详细)小米Note 2的usb调试模式在哪里开启的教程
当我们使用安卓手机链接Pc的时候,或者使用的有些工具比如我们学院营销团队当使用的工具引号精灵,之前使用的老版本就需要开启USB开发者调试模式下使用,现当新版本不需要了,如果手机没有开启USB开发者调试 ...