wcf 学习程序

(一)创建WCF Service

(1)创建WCF Service类库

创建一个Class Library的项目:

删除掉默认的Class1.cs文件,然后添加一个WCF Service项目:

Visual Studio会自动帮助你生成两个文件:HelloService.cs 和 IHelloService.cs,另外还自动添加了System.ServiceModel引用,它是WCF的核心。

修改IHelloService.cs和HelloService.cs文件。

IHelloService.cs:

namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IHelloService" in both code and config file together.
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        string GetMessage(string name);
    }
}

HelloService.cs:

namespace HelloService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HelloService" in both code and config file together.
    public class HelloService : IHelloService
    {

        public string GetMessage(string name)
        {
            return "Hello " + name;
        }
    }
}

(2)创建WCF的Host

添加一个新的ASP.NET Empty Web Application:

添加一个新Item WCF Service

删除HelloService.svc.cs和IHelloService.cs文件。

添加HelloService Class Library的项目引用:

修改HelloService.svc为:

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

Web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService" behaviorConfiguration="metaBehavior">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metaBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

其中,service name=”命名空间.类名”,behaviorConfiguration是用来关联下面behavior的定义的。

endpoint address中定义的是相对地址,与baseAddress结合起来为完整地址

endpoint contract=”命名空间.接口名”

两个endpoint,第一个binding是basicHttpBinding,用于HTTP协议;第二个endpoint用于交换metadata,binding为mexHttpBinding。

其中behavior的定义是用来允许交换metadata的。

Build解决方案,如果没有错误就进行到下一步,部署WCF Service到IIS

(二)部署WCF Service到IIS

(1)Publish HelloServiceIISHost项目

(2)部署到IIS

浏览HelloService.svc

(三)创建一个Windows Form来调用WCF Service

添加一个服务引用:

private void button1_Click(object sender, EventArgs e)
{
    HelloService.HelloServiceClient client = new HelloService.HelloServiceClient();
    label1.Text = client.GetMessage(textBox1.Text);
} 

运行代码,效果如下:

(四)总结

svc文件中,包含着服务指令,Service属性指明文件指向的是哪个服务

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

service的代码可以在

(1) XXX.svc.cs的文件中

(2) 一个独立的Assembly(如同本文)

(3) App_Code文件夹下

时间: 2024-08-26 19:06:58

wcf 学习程序的相关文章

.NET4.5中WCF中默认生成的basicHttpsBinding的研究

起因: 使用.net4.5建立了一个空白的WCF服务.默认使用的绑定配置是basicHttpsBinding. 问题发现: 1.用客户端进行服务引用,生成了默认的配置文件,其中绑定配置是basicHttpBinding. 2.因为需要单次传递大批量数据,所以修改绑定配置,如下: <basicHttpBinding> <binding name="BasicHttpBinding_IService" closeTimeout="00:10:00" r

PHP调用WCF提供的方法

一.准备工作 1.安装wampserver:过程略 2.配置wampserver: 2.1打开php.ini文件,去掉 ;extension=php_soap.dll 这里那个分号. 也有说把这个 ;extension=php_openssl.dll前面的分号也去掉的. 2.2 如上图,将php_soap打上√. 2.3 如上图,打开httpd.conf文件,找到Listen 80 ,将80端口改成一个较大的端口,如8000.因为80端口也能别的程序用着. 在这个directory里面有php页

WCF服务部署到IIS7.5

下面介绍如何把WCF服务部署到IIS: 为WCF服务创建.svc文件 我们知道,每一个ASP.NET Web服务都具有一个.asmx文本文件,客户端通过访问.asmx文件实现对相应Web服务的调用.与之类似,每个WCF服务也具有一个对应的文本文 件,其文件扩展名为.svc.基于IIS的服务寄宿要求相应的WCF服务具有相应的.svc文件,.svc文件部署于IIS站点中,对WCF服务的调用体 现在对.svc文件的访问上. .svc文件的内容很简单,仅仅包含一个ServiceHost指令(Direct

WCF学习之旅----正式篇之基础框架

服务类包括服务契约IWCFService.操作契约OperationContract.和数据契约DataContract. using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.ServiceModel.Description; using System.Runtime.Serialization;

WCF入门(一)--Request Entity Too large 传输的数据量过大

通过WCF进行数据的查询或者添加的时候,如果数据量过大,一般会报出如下的错误: 1.已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的MaxReceivedMessageSize 属性. 2.远程服务器返回了意外反应(413)Request Entity too large. 3.远程服务器返回了意外反应(400)Bad Request. 具体的解决方案: 服务端返回数据给客户端报错 在客户端的配置文件中,主要修改maxReceivedMessageSize <

Wcf体现Restful风格

Wcf体现Restful风格 概述 含状态传输(Representational State Transfer)的软件架构风格.主要特点 1.  资源是由URI来指定: 例如http://example.com/resources/ 2.  对资源的操作 包括获取.创建.修改和删除资源,这些操作正好对应HTTP协议提供的GET.POST.PUT和DELETE方法 3.  传输的资源:Web服务接受与返回的互联网媒体类型,比如:JSON,XML ,YAML 等. 下面通过一个简单的例子逐个问题解决

转 WCF WebService区别

下面我们来详细讨论一下二者的区别.Web Service和WCF的到底有什么区别. [1]Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XML扩展标记语言来表示数据(这个是夸语言和平台的关键).微软的Web服务实现称为ASP.NET Web Service.它使用Soap简单对象访问协议来实现分布式环境里应用程序之间的数据交互.WSDL来实现服务接口相关

WCF中数据契约之已知类型的几种公开方式

WCF中传输的数据不想传统的面向对象编程,它只传递了一些对象的属性,但是自身并不知道自己属于什么对象,所以,他没有子类和父类的概念,因而也就没有Is-a的关系,所以在WCF中,如果想维持这种继承关系,就需要做一些特殊的处理了. 假设有如下定义, namespace KnownTypeExampleInterface{    [DataContract]    public class Employee    {        [DataMember]        public string N

跟我一起学WCF(3)——利用Web Services开发分布式应用

一.引言 在前面文章中分别介绍了MSMQ和.NET Remoting技术,今天继续分享.NET 平台下另一种分布式技术——Web Services 二.Web Services 详细介绍 2.1 Web Services 概述 Web Services是支持客户端与服务器通过网络互操作的一种软件系统,是一组可以通过网络调用的应用程序API.在Web Services中主要到SOAP/UDDI/WSDL这三个核心概念,下面分别介绍下这三个概念的定义. SOAP:SOAP(Simple Object