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

2.系統(tǒng)自動生成IWcfService
// 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IService1”。
[ServiceContract]
public interface IWcfService
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: 在此添加您的服務操作
}
// 使用下面示例中說明的數(shù)據(jù)約定將復合類型添加到服務操作。
[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)服務契約:ServiceContract(服務)和OperationContract (方法)
(2)數(shù)據(jù)契約:DataContract(類)和DataMember(屬性) 用于類和結構上
(3)消息契約:MessageContract 用于soap消息
3.WCF服務類
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.服務配置文件
<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-->
<!--配置服務節(jié)點Start-->
<services>
<!--配置某一服務,在這里可以指定服務名稱-->
<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>
<!--配置服務節(jié)點End-->
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 為避免泄漏元數(shù)據(jù)信息,請在部署前將以下值設置為 false -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- 要接收故障異常詳細信息以進行調(diào)試,請將以下值設置為 true。在部署前設置為 false 以避免泄漏異常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
5.iis部署WCF服務


6.添加客戶端項目并添加服務引用

7.Main程序中添加wcf服務并調(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
asp.net gridview中用checkbox全選的幾種實現(xiàn)的區(qū)別
這幾天為了改變客戶端grid的全選效率問題,詳細研究了ext中grid的全選和gridview中通過腳本實現(xiàn)的全選效率,總結一下,供大家參考,有錯誤的地方,希望大俠指正,小弟獻丑了。2009-06-06
System.Runtime.InteropServices.COMException的解決方法
完美解決“換另一臺電腦上用VS2008繼續(xù)開發(fā)web項目時出現(xiàn)“System.Runtime.InteropServices.COMException”,然后是加載不了項目?!?2009-03-03
Web.Config文件配置之限制上傳文件大小和時間的屬性配置
在Web.Config文件中配置限制上傳文件大小與時間字符串時,是在httpRuntime httpRuntime節(jié)中完成的,需要設置以下2個屬性:maxRequestLength屬性與ExecutionTimeout屬性,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02
如何在ASP.NET Core中給上傳圖片功能添加水印實例代碼
這篇文章主要給大家介紹了關于如何在ASP.NET Core中給上傳圖片功能添加水印的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-02-02
Asp.net MVC scheduler的實現(xiàn)方法詳解
這篇文章主要介紹了Asp.net MVC scheduler的實現(xiàn)方法詳解的相關資料,希望通過本文大家能夠?qū)崿F(xiàn)這樣的方法,需要的朋友可以參考下2017-10-10
.Net使用分表分庫框架ShardingCore實現(xiàn)多字段分片
本文詳細講解了.Net使用分表分庫框架ShardingCore實現(xiàn)多字段分片的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
ASP.NET 實現(xiàn)驗證碼以及刷新驗證碼的小例子
這篇文章介紹了ASP.NET 實現(xiàn)驗證碼以及刷新驗證碼的小例子,有需要的朋友可以參考一下2013-10-10

