标签:
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的代码如下:
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下,
我们可以通过命令行运行该工具生成客户端代理类
在运行中输入cmd打开命令行,输入 cd C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
输入svcutil.exe /out:f:\ SampleMethodClient.cs /config:f:\App.config http://localhost:1234/SampleMethod
3. Client:控制台应用程序,客户端调用程序。将生成的SampleMethodClient.cs和App.config复制到Client的工程目录下,完成客户端调用代码。
为验证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"/>
运行结果如下:
标签:
原文地址:http://www.cnblogs.com/wangweimutou/p/4516224.html