helloworld:一个完整的WCF案例
服務(wù)端
?
1.創(chuàng)建一個(gè)空的解決方案:WCFDemo:
?
2.創(chuàng)建一個(gè)宿主控制臺(tái)程序:Host
?
3.右擊Host項(xiàng)目,選擇“添加”--“新建項(xiàng)”,選擇“WCF服務(wù)”創(chuàng)建名為“Service1.cs”的服務(wù)
?
?
如此:VS2010已經(jīng)為我們創(chuàng)建了?IService1.cs?? Service1.cs?? app.config? 三個(gè)文件,其中IService1.cs和Service1.cs?創(chuàng)建了同屬于Host命名空間的類(lèi),我們可以修改這三個(gè)文件:
?
IService1.cs?:
using System.ServiceModel;
namespace Host
{
??? // 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時(shí)更改代碼和配置文件中的接口名“IService1”。
??? [ServiceContract]
??? public interface IService1
??? {
??????? [OperationContract]
??????? string DoWork(int value);
??? }
}
?
Service1.cs:
using System.ServiceModel;
namespace Host
{
??? // 注意: 使用“重構(gòu)”菜單上的“重命名”命令,可以同時(shí)更改代碼和配置文件中的類(lèi)名“Service1”。
??? public class Service1 : IService1
??? {
??????? public string? DoWork(int value)
??????? {
??????????? int iPingfang = value * value;
??????????? return string.Format("經(jīng)過(guò)平方后的值為:{0}",iPingfang );
??????? }
??? }
}
?
App.config:
文件原則上可以不用改,但是address太長(zhǎng)了(默認(rèn)的為baseAddress="http://localhost:8732/Design_Time_Addresses/Host/Service1/")縮短為baseAddress=“http://localhost:8732/Service1/”
?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
??? <system.serviceModel>
??????? <behaviors>
??????????? <serviceBehaviors>
??????????????? <behavior name="">
??????????????????? <serviceMetadata httpGetEnabled="true" />
??????????????????? <serviceDebug includeExceptionDetailInFaults="false" />
??????????????? </behavior>
??????????? </serviceBehaviors>
??????? </behaviors>
??????? <services>
??????????? <service name="Host.Service1">
??????????????? <endpoint address="" binding="wsHttpBinding" contract="Host.IService1">
??????????????????? <identity>
??????????????????????? <dns value="localhost" />
??????????????????? </identity>
??????????????? </endpoint>
??????????????? <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
??????????????? <host>
??????????????????? <baseAddresses>
??????????????????????? <add baseAddress="http://localhost:8732/Service1/" />
??????????????????? </baseAddresses>
??????????????? </host>
??????????? </service>
??????? </services>
??? </system.serviceModel>
</configuration>
?
?
?
4.此外WCF服務(wù)必須在宿主進(jìn)程中運(yùn)行,我們可以修改 Program.cs 文件,創(chuàng)建宿主進(jìn)程:
using System.ServiceModel;
namespace Host
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? using (ServiceHost host = new ServiceHost(typeof(Host.Service1)))
??????????? {
??????????????? host.Open();
??????????????? Console.WriteLine("服務(wù)已經(jīng)啟動(dòng)......");
??????????????? Console.ReadLine();
??????????????? host.Close();
??????????? }
??????? }
??? }
}
?
?
?
5.編譯運(yùn)行程序,生成Host.exe文件
?
?
?
?客戶(hù)端
?
1.啟動(dòng)剛創(chuàng)建的WCF服務(wù)宿主進(jìn)程Host.exe
?
?
2.創(chuàng)建一個(gè)客戶(hù)端控制臺(tái)程序:Client
?
3.右擊“引用”--“添加服務(wù)引用”,在“地址”的TextBox里面輸入服務(wù)器的地址(就是咱們前面設(shè)置的baseaddress地址),并點(diǎn)擊“前往”將得到目標(biāo)服務(wù)器上面的Services,如下圖所示:
?
?
?
?
如此,這一步將在客戶(hù)端間接借助SvcUtil.exe文件創(chuàng)建客戶(hù)端代理(命名空間為:using Client.ServiceReference1;)以及配置文件app.config,具體如下:
?
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
??? <system.serviceModel>
??????? <bindings>
??????????? <wsHttpBinding>
??????????????? <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
??????????????????? openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
??????????????????? bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
??????????????????? maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
??????????????????? messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
??????????????????? allowCookies="false">
??????????????????? <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
??????????????????????? maxBytesPerRead="4096" maxNameTableCharCount="16384" />
??????????????????? <reliableSession ordered="true" inactivityTimeout="00:10:00"
??????????????????????? enabled="false" />
??????????????????? <security mode="Message">
??????????????????????? <transport clientCredentialType="Windows" proxyCredentialType="None"
??????????????????????????? realm="" />
??????????????????????? <message clientCredentialType="Windows" negotiateServiceCredential="true"
??????????????????????????? algorithmSuite="Default" />
??????????????????? </security>
??????????????? </binding>
??????????? </wsHttpBinding>
??????? </bindings>
??????? <client>
??????????? <endpoint address="http://localhost:8732/Service1/" binding="wsHttpBinding"
??????????????? bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
??????????????? name="WSHttpBinding_IService1">
??????????????? <identity>
??????????????????? <dns value="localhost" />
??????????????? </identity>
??????????? </endpoint>
??????? </client>
??? </system.serviceModel>
</configuration>
?
?
4.修改客戶(hù)端程序?Program.cs?,并使用代理訪(fǎng)問(wèn)服務(wù)契約:
using System.ServiceModel;
using Client.ServiceReference1;//引用命名空間
namespace Client
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? ServiceReference1.Service1Client proxy = new Service1Client();
??????????? string str = proxy.DoWork(2);//運(yùn)行服務(wù)端方法
??????????? Console.WriteLine(str);
??????????? Console.ReadLine();
??????? }
??? }
}
?
?
5.編譯運(yùn)行程序,生成 Client.exe
轉(zhuǎn)載于:https://www.cnblogs.com/jara/p/3659896.html
總結(jié)
以上是生活随笔為你收集整理的helloworld:一个完整的WCF案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux去掉某一字符开头的行
- 下一篇: 携程编程大赛 (预赛第二场)第一题【剪刀