消息(6)——WCF,构建简单的WCF服务,MTOM编码

简介: 构建一个简单的WCF服务。 以Web服务类似的步骤由IIS进行宿主服务。建立的步骤: 1 新建3.5网站 2 添加WCF服务,自动生成契约接口与实现,这里改动一下,添加个字串参数: [ServiceContract] public interface IFirstService {     [OperationContract]     void DoWork(string strContent); }   服务中的方法什么都不用做。

构建一个简单的WCF服务

Web服务类似的步骤由IIS进行宿主服务。建立的步骤:

1 新建3.5网站

2 添加WCF服务,自动生成契约接口与实现,这里改动一下,添加个字串参数:

[ServiceContract]

public interface IFirstService

{

    [OperationContract]

    void DoWork(string strContent);

}

 

服务中的方法什么都不用做。

public class FirstService : IFirstService

{

    public void DoWork(string strContent)

    {

    }

}

在添加WCF服务时,会自动在配置文件中添加必要的章节,例如绑定和元数据发布。

 

<system.serviceModel>

  <behaviors>

    <serviceBehaviors>

      <behavior name="FirstServiceBehavior">

        <serviceMetadata httpGetEnabled="true" />

        <serviceDebug includeExceptionDetailInFaults="false" />

      </behavior>

    </serviceBehaviors>

  </behaviors>

  <services>

    <service behaviorConfiguration="FirstServiceBehavior"

             name="FirstService">

      <endpoint address=""

                binding="basicHttpBinding"

                contract="IFirstService">

        <identity>

          <dns value="localhost" />

        </identity>

      </endpoint>

      <endpoint address="mex"

                binding="mexHttpBinding"

                contract="IMetadataExchange" />

    </service>

  </services>

</system.serviceModel>

 

这里把绑定改一下,改为basicHttpBinding

 

然后在测试端:新建立类库项目,由发布的元数据生成代理,然后进行服务请求:

[Test]

public void Test()

{

FirstInstance.FirstServiceClient client =

New FirstInstance.FirstServiceClient();

    client.DoWork("this is a test!");

}

 

现在看一下消息包的情况:

这是客户端请求的信息:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

  <s:Body>

    <DoWork xmlns="http://tempuri.org/">

      <strContent>this is a test!</strContent>

    </DoWork>

  </s:Body>

</s:Envelope>

这是服务端回应的信息:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

  <s:Body>

    <DoWorkResponse xmlns="http://tempuri.org/"/>

  </s:Body>

</s:Envelope>

 

对于BasicHttpBinding来说,它通过http来发送soap1.1的消息。这个绑定用于配置和公开能够与基于asmxweb service和客户端进行通信的终结点,以及符合ws-i basic profile 1.1标准的其它服务。

 

通过设置WCF绑定的消息编码格式来设置传输过程中所使用的编码:

<basicHttpBinding>

<binding name="firstBinding" messageEncoding="Text">

</binding>

</basicHttpBinding>

 

现设置BasicHttp绑定的消息编码为文本,当传输二进制附件时,会怎么用base64编码:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

  <s:Body>

    <SaveImage xmlns="www.self001.com">

      <bb>GMDggAOw==</bb>

    </SaveImage>

  </s:Body>

</s:Envelope>

 

其中附件部分我省略了大部分,只留一小段。

当使用MTOM编码格式时:

--uuid:deef670a-dfd7-4a71-8d89-face6ac975dd+id=1

Content-ID: <http://tempuri.org/0>

Content-Transfer-Encoding: 8bit

Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

 

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

  <s:Body>

    <SaveImage xmlns="www.self001.com">

      <bb>

        <xop:Include

          href="cid:http%3A%2F%2Ftempuri.org%2F1%2F634057273450156250"

          xmlns:xop="http://www.w3.org/2004/08/xop/include"/>

      </bb>

    </SaveImage>

  </s:Body>

</s:Envelope>

--uuid:deef670a-dfd7-4a71-8d89-face6ac975dd+id=1

Content-ID: <http://tempuri.org/1/634057273450156250>

Content-Transfer-Encoding: binary

Content-Type: application/octet-stream

GIF89ad……省略

--uuid:deef670a-dfd7-4a71-8d89-face6ac975dd+id=1--

 

这与WSE3中使用的MTOM是相同的。

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园

目录
相关文章
|
9月前
|
前端开发
WCF更新服务引用报错的原因之一
WCF更新服务引用报错的原因之一
|
8月前
|
C# 数据安全/隐私保护
c#如何创建WCF服务到发布(SqlServer版已经验证)
c#如何创建WCF服务到发布(SqlServer版已经验证)
38 0
|
8月前
|
安全 数据库连接 数据库
WCF服务创建到发布(SqlServer版)
在本示例开始之前,让我们先来了解一下什么是wcf? wcf有哪些特点? wcf是一个面向服务编程的综合分层架构。该架构的项层为服务模型层。 使用户用最少的时间和精力建立自己的软件产品和外界通信的模型。它使得开发者能够建立一个跨平台的安全、可信赖、事务性的解决方案。且能与已有系统兼容写作。 简单概括就是:一组数据通信的应用程序开发接口。
57 0
Visual Studio 2022 创建 WCF服务 找不到
Visual Studio 2022 创建 WCF服务 找不到
|
C++
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
102 0
WCF基础教程(二)——解析iis8和iis8.5+VS2013发布wcf服务问题
WCF使用纯代码的方式进行服务寄宿
服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境。通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用,除去上面的两种寄宿方式,还可以以纯代码的方式实现服务的寄宿工作。
850 0
|
Windows
WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台。整合了原有的windows通讯的 .net Remoting,WebService,Socket的机制,并融合有HTTP和FTP的相关技术。
1046 0
WCF服务自我寄宿
WCF服务的寄宿方式 WCF寄宿方式是一种非常灵活的操作,可以寄宿在各种进程之中,常见的寄宿有: IIS服务、Windows服务、Winform程序、控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便、高效提供服务调用。
996 0
|
网络架构
(纯代码)快速创建wcf rest 服务
因为有一个小工具需要和其它的业务对接数据,所以就试一下看能不能弄一个无需配置快速对接的方法出来,百(以)度(讹)过(传)后(讹),最后还是对照wcf配置对象调试出来了: 1.创建WebHttpBinding 2.
979 0