WCF入门程序设计与实现

1、首先我们先新建一个空白的解决方案MyFirstWCF(如下图)

 图一

2、在解决方案下创建一个类库MyWCF_Server(如下图)

 2

(1)、删除Class1.cs

(2)、添加引用System.ServiceModel

(3)、添加一个接口IMyMathod.cs,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

 

namespace MyWCF_Server

{

    [ServiceContract]

    public interface IMyMathod

    {

        [OperationContract]

        string ShowHelloWorld();

    }

}

  

(4)、添加一个类MyMathod.cs并实现接口IMyMathod.cs,代码如下

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace MyWCF_Server

{

    public class MyMathod : IMyMathod

    {

        #region IMyMathod 成员

 

        public string ShowHelloWorld()

        {

            return "Hello,World!";

        }

 

        #endregion

    }

}

3、添加一个新网站项目MyWeb(如下图)

 3

(1)、添加引用项目MyFirstWCF

(2)、新建一个文件夹MyWCFsvc

(3)、删除在项目MyWeb下所有东西(文件夹MyWCFsvc,Web除外)

(4)、在文件夹MyWCFsvc下添加新项WCF服务MyMathod (如下图)

 55

(5)、双击MyMathod.svc,直接最上面行替换为:

<%@ ServiceHost Language="C#" Debug="true" Service="MyWCF_Server.MyMathod" %>

(6)、右键点击Web.config,选择Edict WCF Configuration

ss

(7)、右键点击Services,选择New Services菜单进行配置,配置文件中的一段如下

<system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior name="WEB.DemoServiceBehavior">

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <bindings>

      <wsHttpBinding>

        <binding name="WSHttpBinding_IMyMathod">

          <security mode="None">

          </security>

        </binding>

      </wsHttpBinding>

    </bindings>

    <services>

      <service behaviorConfiguration="WEB.DemoServiceBehavior" name="MyWCF_Server.MyMathod">

        <endpoint address="MyWCF_Server" binding="wsHttpBinding" contract="MyWCF_Server.IMyMathod" bindingConfiguration="WSHttpBinding_IMyMathod" name="MyWCF_Server.IMyMathod"/>

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

      </service>

    </services>

  </system.serviceModel>

4、打开http://localhost/mywcf/MyWCFsvc/MyMathod.svc时,会发现页面上有一个提示:

若要测试此服务,需要创建一个客户端,并将其用于调用该服务。可以使用下列语法,从命令行中使用 svcutil.exe 工具来进行此操作:

svcutil.exe http://sl-20091120isfn/mywcf/MyWCFsvc/MyMathod.svc?wsdl

(注:这里的sl-20091120isfn就是主机名)

 淡淡的

(1)、复制这一行命令,然后打开windows的开始菜单-->Microsoft Visual Studio 2008-->Visual Studio Tools-->Visual Studio 2008 Command Prompt进到vs2008的命令行

(2)、输入刚才的命令,并加一个参数/D:D:\MyWCF\ 即输出文件保存在D:\ MyWCF目录中

svcutil.exe http://sl-20091120isfn/mywcf/MyWCFsvc/MyMathod.svc?wsdl  /D:D:\MyWCF\ 

(3)、完成后,查看D:\ MyWCF目录,会生成二个文件MymathodClient.cs,output.config

5、新建一个Client的winform测试项目,把MymathodClient.cs加到Client项目中,同时在Client项目中,增加一个App.Config,然后把output.Config的内容复制到App.Config中

其中页面代码如下

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace test

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            MymathodClient client = new MymathodClient ();

            MessageBox.Show(client. ShowHelloWorld());

        }

    }

}

运行结果:

 算是

原文地址:https://www.cnblogs.com/tryzi/p/2593083.html