类库 System.ServiceModle
WCF类库
契约IUser1,实现User1
[ServiceContract] public interface IUser1 { [OperationContract] string GetUser1Name(string name); } public class User1 : IUser1 { public string GetUser1Name(string name) { return "我是usre1" + name; } }
契约IUser2 ,实现User2
[ServiceContract] public interface IUser2 { [OperationContract] string GetUser1Name(string name); } public class User2 : IUser2 { public string GetUser1Name(string name) { return "我是user2" + name; } }
契约IUnity1和IUnity2,实现Unity(一个实现继承了两个契约,主意看一下配置文件如何配置)
[ServiceContract] public interface IUnity1 { [OperationContract] int GetUnityCount(); } [ServiceContract] public interface IUnity2 { [OperationContract] string GetUnityString(); } public class Unity : IUnity1, IUnity2 { public int GetUnityCount() { return 10; } public string GetUnityString() { return "Unity"; } }
宿主
static void Main(string[] args) { ServiceHost sh1 = new ServiceHost(typeof(WcfLib.User1)); sh1.Open(); Console.WriteLine("服务1开启"); ServiceHost sh2 = new ServiceHost(typeof(WcfLib.User2)); sh2.Open(); Console.WriteLine("服务2开启"); ServiceHost sh3 = new ServiceHost(typeof(WcfLib.Unity.Unity)); sh3.Open(); Console.WriteLine("服务3开启"); Console.ReadKey(); }
服务配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <system.serviceModel> <services> <!--name="命名空间名称.实现类名称"--> <service name="WcfLib.User1" behaviorConfiguration="mexBehaviour"> <endpoint address="MyServices1" binding="basicHttpBinding" contract="WcfLib.IUser1"> </endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:9999/"/> </baseAddresses> </host> </service> <service name="WcfLib.User2" behaviorConfiguration="mexBehaviour"> <host> <baseAddresses> <add baseAddress="http://localhost:6666/"/> </baseAddresses> </host> <endpoint address="MyServices2" binding="basicHttpBinding" contract="WcfLib.IUser2"></endpoint> </service> <service name="WcfLib.Unity.Unity" behaviorConfiguration="mexBehaviour"> <host> <baseAddresses> <add baseAddress="http://localhost:7777/"/> <add baseAddress="net.tcp://localhost:7776/"/> </baseAddresses> </host> <endpoint address="MyServices3" binding="basicHttpBinding" contract="WcfLib.Unity.IUnity1" ></endpoint> <!--错误:找不到具有绑定 NetTcpBinding 的终结点的与方案 net.tcp 匹配的基址。注 册的基址方案是 [http]。 解决:TCP通讯 地址必须是TCP的(TCP不能宿主在IIS上) net.tcp://localhost:7776/--> <endpoint address="MyServices4" binding="netTcpBinding" contract="WcfLib.Unity.IUnity2"></endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="mexBehaviour"> <!--设置未false被人不能发现,一般当客户端已经加载好服务代理就可以设置为false了。配置修改,服务要重启,才能生效--> <serviceMetadata httpGetEnabled="true" /> </behavior> <behavior> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
客户端
右键添加服务
配置文件自动生成
static void Main(string[] args) { WCFServiceUser1.User1Client cli1 = new WCFServiceUser1.User1Client(); WCFServiceUser2.User2Client cli2 = new WCFServiceUser2.User2Client(); WCFServiceUnity.Unity1Client cli3 = new WCFServiceUnity.Unity1Client(); WCFServiceUnity.Unity2Client cli4 = new WCFServiceUnity.Unity2Client(); Console.WriteLine(cli1.GetUser1Name("name1")); Console.WriteLine(cli2.GetUser1Name("name2")); Console.WriteLine(cli3.GetUnityCount()); Console.WriteLine(cli4.GetUnityString()); }
WCF 调用服务标准写法
static void Main(string[] args) { //不适用using,原因using在网络中断时,wcf不能关闭。websevice可以是用using释放,websevice标准写法是用using CustomService.UserServiceSoapClient ucli = null; try { ucli = new CustomService.UserServiceSoapClient(); ucli.GetStr(""); //手动释放, ucli.Close(); } catch (Exception) { if (ucli != null) { ucli.Abort(); } } }