博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
纯手写wcf代码,wcf入门,wcf基础教程
阅读量:4555 次
发布时间:2019-06-08

本文共 2483 字,大约阅读时间需要 8 分钟。

1、定义服务协定

    =>定义接口

using System.ServiceModel;namespace WcfConsole{    ///     /// 定义服务协定    ///     [ServiceContract]    interface IW    {        [OperationContract]        string HelloWorld();    }}

2、实现服务协定

    =>实现接口

namespace WcfConsole{    ///     /// 实现服务协定    ///     public class W : IW    {        public string HelloWorld()        {            return "HelloWorld";        }    }}

3、承载和执行服务

    =>打开服务

using System;using System.ServiceModel;using System.ServiceModel.Description;namespace WcfConsole{    ///     /// 承载和执行主要的 WCF 服务    ///     class Program    {        static void Main(string[] args)        {            //创建服务网址            Uri url = new Uri("http://localhost:5210/W/");            //创建server主机            ServiceHost host = new ServiceHost(typeof(W), url);            try            {                //加入服务端点                host.AddServiceEndpoint(typeof(IW), new WSHttpBinding(), "serviceName");                //启用元素交换                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();                smb.HttpGetEnabled = true;                host.Description.Behaviors.Add(smb);                //打开服务                host.Open();                Console.WriteLine("服务打开成功……");                Console.ReadKey();                //关闭服务                host.Close();            }            catch (CommunicationException e)            {                Console.WriteLine(e.Message);                //关闭服务                host.Close();            }        }    }}

4、创建client

    =>须要先打开匃

    =>新建项目

    =>加入服务引用

5、配置client

    =>加入时微软自己主动配置

> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="WSHttpBinding_IW" /> </wsHttpBinding> </bindings> <client> <!--指定用于调用服务时,端点--> <endpoint address="http://localhost:5210/W/serviceName" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IW" contract="WService.IW" name="WSHttpBinding_IW"> <identity> <userPrincipalName value="DUHUIFENG\liman" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>

6、使用client

    =>

using System;namespace ConsoleApplication{    class Program    {        static void Main(string[] args)        {            WService.WClient w = new WService.WClient();            string result = w.HelloWorld();            Console.WriteLine(result);            w.Close();            Console.ReadKey();        }    }}

转载于:https://www.cnblogs.com/ldxsuanfa/p/10676216.html

你可能感兴趣的文章
UI设计
查看>>
androidtab
查看>>
Windows Phone 自定义弹出框和 Toast 通知
查看>>
如何生成静态页面的五种方案
查看>>
php 事件驱动 消息机制 共享内存
查看>>
剑指offer 二叉树的bfs
查看>>
LeetCode Maximum Subarray
查看>>
让我们再聊聊浏览器资源加载优化
查看>>
underscore demo
查看>>
CSS hack
查看>>
C# Enum Name String Description之间的相互转换
查看>>
PHP wamp server问题
查看>>
Spring Data Redis学习
查看>>
js闭包理解案例-解决for循环为元素注册事件的问题
查看>>
2015.04.23,外语,读书笔记-《Word Power Made Easy》 12 “如何奉承朋友” SESSION 33
查看>>
Spring+SpringMVC+JDBC实现登录
查看>>
生与死之间
查看>>
NEFU 109
查看>>
HDU 5435
查看>>
git从已有分支拉新分支开发
查看>>