using System.ServiceModel; namespace Service { [ServiceContract(SessionMode=SessionMode.Required)] public interface ISampleMethod { [OperationContract(IsTerminating=true)] string MethodOne(string msg); [OperationContract] string MethodTwo(string msg); } }
SampleMethod.cs的代碼以下:html
namespace Service { public class SampleMethod:ISampleMethod { public string MethodOne(string msg) { return "You called MethodOne return message is: " + msg; } public string MethodTwo(string msg) { return "You called MethodTwo return message is: " + msg; } } }
2. Host:控制檯應用程序,服務承載程序。添加對程序集Service的引用,完成如下代碼,寄宿服務。Program.cs代碼以下:安全
using System; using System.ServiceModel; using Service; namespace Host { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(SampleMethod))) { host.Opened += delegate { Console.WriteLine("服務已經啓動,按任意鍵終止!"); }; host.Open(); Console.Read(); } } } }
App.config代碼以下:服務器
<?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service name="Service.SampleMethod" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1234/SampleMethod/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Service.ISampleMethod" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
咱們經過svcutil.exe工具生成客戶端代理類和客戶端的配置文件網絡
svcutil.exe是一個命令行工具,位於路徑C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin下,ide
咱們能夠經過命令行運行該工具生成客戶端代理類工具
在運行中輸入cmd打開命令行,輸入 cd C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Binui
輸入svcutil.exe /out:f:\ SampleMethodClient.cs /config:f:\App.config http://localhost:1234/SampleMethod加密
3. Client:控制檯應用程序,客戶端調用程序。將生成的SampleMethodClient.cs和App.config複製到Client的工程目錄下,完成客戶端調用代碼。spa
爲驗證WCF的會話特色,咱們將客戶端的調用分爲如下幾種狀況:命令行
using System; namespace Client{ class Program{ static void Main(string[] args){ try{ SampleMethodClient client1 = new SampleMethodClient(); Console.WriteLine(client1.MethodOne("MethodOne")); Console.WriteLine(client1.MethodTwo("MethodTwo")); }
catch (Exception ex){ Console.WriteLine(ex.Message); } finally{ Console.Read(); } }}}
運行結果以下:
try { SampleMethodClient client1 = new SampleMethodClient(); Console.WriteLine(client1.MethodOne("First Called MethodOne")); SampleMethodClient client2 = new SampleMethodClient(); Console.WriteLine(client2.MethodOne("Second Called MethodOne")); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.Read(); }
運行結果以下:
<security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" realm="" /> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security>
替換爲
<security mode="None"/>
運行結果以下: