.net WCF簡單實例詳解(5)
本文為大家分享了.net WCF簡單實例,供大家參考,具體內(nèi)容如下
1.創(chuàng)建WCF項目

2.系統(tǒng)自動生成IWcfService
// 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IWcfService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: 在此添加您的服務(wù)操作
}
// 使用下面示例中說明的數(shù)據(jù)約定將復(fù)合類型添加到服務(wù)操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
(1)服務(wù)契約:ServiceContract(服務(wù))和OperationContract (方法)
(2)數(shù)據(jù)契約:DataContract(類)和DataMember(屬性) 用于類和結(jié)構(gòu)上
(3)消息契約:MessageContract 用于soap消息
3.WCF服務(wù)類
public class WcfService : IWcfService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
4.服務(wù)配置文件
<system.serviceModel>
<!--配置綁定節(jié)點Start-->
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding0" maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647"/>
<security mode="None" />
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="netTcpBinding0" maxReceivedMessageSize="2147483647">
<readerQuotas maxStringContentLength="2147483647"/>
<security mode="None" />
</binding>
</netTcpBinding>
<wsHttpBinding></wsHttpBinding>
</bindings>
<!--配置綁定節(jié)點End-->
<!--配置服務(wù)節(jié)點Start-->
<services>
<!--配置某一服務(wù),在這里可以指定服務(wù)名稱-->
<service name="WcfServiceTest.WcfService">
<endpoint address="aaa" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding0"
name="BasicHttpBinding_WcfService" contract="WcfServiceTest.IWcfService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBinding0"
name="NetTcpBinding_WcfService" contract="WcfServiceTest.IWcfService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
</services>
<!--配置服務(wù)節(jié)點End-->
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 為避免泄漏元數(shù)據(jù)信息,請在部署前將以下值設(shè)置為 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障異常詳細(xì)信息以進(jìn)行調(diào)試,請將以下值設(shè)置為 true。在部署前設(shè)置為 false 以避免泄漏異常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
5.iis部署WCF服務(wù)


6.添加客戶端項目并添加服務(wù)引用

7.Main程序中添加wcf服務(wù)并調(diào)用方法
class Program
{
static void Main(string[] args)
{
var client = new WcfService.WcfServiceClient();
try
{
var str = client.GetData(2046);
Console.WriteLine(string.Format("內(nèi)容:{0}", str));
client.Close();
}
catch (Exception ex)
{
Console.WriteLine("出現(xiàn)異常!");
client.Abort();
}
Console.ReadLine();
}
}
8.客戶端配置文件
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_WcfService" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_WcfService">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<!--<endpoint address="http://localhost/WcfServiceTest/WcfService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"
contract="WcfService.IWcfService" name="BasicHttpBinding_WcfService" />-->
<endpoint address="net.tcp://localhost/WcfServiceTest/WcfService.svc"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_WcfService"
contract="WcfService.IWcfService" name="NetTcpBinding_WcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET(C#)中操作SQLite數(shù)據(jù)庫實例
最近項目中有使用到SQLite數(shù)據(jù)庫,于是查找資料,編寫了一個ASP.NET基于C#語言的SQLite數(shù)據(jù)庫操作實例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫類似.2009-12-12
.Net使用SuperSocket框架實現(xiàn)WebSocket前端
這篇文章介紹了.Net使用SuperSocket框架實現(xiàn)WebSocket前端,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
aspx實現(xiàn)的 jquery ui 的 flexgrid demo
這幾天沒事研究著jquery,真是個好東西,慢慢的知道了有jquery ui,一開始就被華麗的界面和簡單的操作給吸引了,尤其是里面的flexgrid,對我而言可以說是非常寶貴的東西2009-12-12
.NET Core結(jié)合Nacos實現(xiàn)配置加解密的方法
當(dāng)我們把應(yīng)用的配置都放到配置中心后,很多人會想到這樣一個問題,配置里面有敏感的信息要怎么處理呢?本文就詳細(xì)的介紹了.NET Core Nacos配置加解密,感興趣的可以了解一下2021-06-06
Asp.Net Core利用文件監(jiān)視進(jìn)行快速測試開發(fā)詳解
這篇文章主要給大家介紹了關(guān)于Asp.Net Core利用文件監(jiān)視進(jìn)行快速測試開發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
關(guān)于.NET Framework中的設(shè)計模式--應(yīng)用策略模式為List排序
本篇文章,小編將為大家介紹關(guān)于.NET Framework中的設(shè)計模式--應(yīng)用策略模式為List排序,有需要的朋友可以參考一下2013-04-04
ASP.Net MVC+Data Table實現(xiàn)分頁+排序功能的方法
這篇文章主要介紹了ASP.Net MVC+Data Table實現(xiàn)分頁+排序功能的方法,結(jié)合實例形式分析了asp.net基于mvc架構(gòu)實現(xiàn)的數(shù)據(jù)查詢、排序、分頁顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06

