消息(6)——WCF,构建简单的WCF服务,MTOM编码
構建一個簡單的WCF服務。
以Web服務類似的步驟由IIS進行宿主服務。建立的步驟:
1 新建3.5網(wǎng)站
2 添加WCF服務,自動生成契約接口與實現(xiàn),這里改動一下,添加個字串參數(shù):
[ServiceContract]
public interface IFirstService
{
??? [OperationContract]
??? void DoWork(string strContent);
}
?
服務中的方法什么都不用做。
public class FirstService : IFirstService
{
??? public void DoWork(string strContent)
??? {
??? }
}
在添加WCF服務時,會自動在配置文件中添加必要的章節(jié),例如綁定和元數(shù)據(jù)發(fā)布。
?
<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。
?
然后在測試端:新建立類庫項目,由發(fā)布的元數(shù)據(jù)生成代理,然后進行服務請求:
[Test]
public void Test()
{
FirstInstance.FirstServiceClient client =
New FirstInstance.FirstServiceClient();
??? client.DoWork("this is a test!");
}
?
現(xiàn)在看一下消息包的情況:
這是客戶端請求的信息:
<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來發(fā)送soap1.1的消息。這個綁定用于配置和公開能夠與基于asmx的web service和客戶端進行通信的終結點,以及符合ws-i basic profile 1.1標準的其它服務。
?
通過設置WCF綁定的消息編碼格式來設置傳輸過程中所使用的編碼:
<basicHttpBinding>
<binding name="firstBinding" messageEncoding="Text">
</binding>
</basicHttpBinding>
?
現(xiàn)設置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是相同的。
轉載于:https://www.cnblogs.com/jams742003/archive/2010/04/01/1702277.html
總結
以上是生活随笔為你收集整理的消息(6)——WCF,构建简单的WCF服务,MTOM编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]在资源管理器中使鼠标右键增加一个命
- 下一篇: (转)详解Vs2008下打包安装程序的一