类库 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 ;
} 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();
}
}
}

源码下载

最新文章

  1. javascript-style-guide
  2. [CodeWars][JS]实现链式加法
  3. null、undefined、false、0相等性比较
  4. MVC开发经验总结
  5. WebHeaderCollection 类
  6. WinForm------点击Control弹出MessageBox
  7. Asp.net Core WebApi 支持json/xml格式的数据返回
  8. Koajs原理
  9. javascript中substring和substr方法
  10. Swift—Cocoa Touch设计模式-备
  11. IE下的bug
  12. Chrome设计文档-多进程架构
  13. C++ typedef
  14. Stub和Mock的理解
  15. 为什么选择.NETCore?
  16. Linux管道编程实例
  17. resnet代码分析
  18. C++ 断言
  19. Spring MVC 使用介绍(十)—— 编码
  20. [Java核心技术笔记]并发

热门文章

  1. CDW数学小笔记
  2. python去掉字符串中重复字符的方法
  3. geometry_msgs/PoseStamped 类型的变量的构造
  4. 怎么样修改小程序分享的title/onShareAppMessage
  5. Java核心复习——CompletableFuture
  6. 导入项目后,http://schemas.android.com/apk/res/android报错
  7. 解决问题:OSError: mysql_config not found
  8. ST (Sparse Table:稀疏表)算法
  9. Ionic4.x 中自定义公共模块
  10. IntelliJ IDEA中构建spring-boot项目