c# WCF初学笔记(1)---项目的创建以及简单配置

1什么是WCF?
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,具体的一些概念可以去微软官网查找,
这有篇文章讲的不错可以看看,原文链接: http://www.cnblogs.com/jiekzou/
下面就以项目为驱动的方式去讲解,实现一个简单的聊天小项目
2.WCF项目的创建:本项目使用VS2017开发
首先创建服务器端
在这里插入图片描述

在这里插入图片描述

2.修改生成的App.config配置文件:实现双工通道(客户端可以调用服务器端发送请求,服务器端也可以回调客户客户端中的方法)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- 部署服务库项目时,必须将配置文件的内容添加到
 主机的 app.config 文件中。System.Configuration 不支持库的配置文件。 -->
  <system.serviceModel>
    <services>
      <service name="ChatServer.ChatService">
        <!--设置双工通道:wsDualHttpBinding 大小写敏感-->
        <endpoint address="" binding="wsDualHttpBinding" contract="ChatServer.IChatService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/ChatServer/ChatService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
          以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

注意:默认创建的WCF项目会生成WCF文件(如:Server1.cs和IServer1.cs),并生成server配置信息,所以如果自定义文件删除掉 … 内的配置信息
2.自定义创建WCF服务类
在这里插入图片描述
App.config配置文件中会自动增加这个服务,根据需求去修改
2.创建客户端(WinForm项目:未使用DevExpress去开发,因为本身功能也很简单),
用于登录的页面和显示好友列表的页面

在这里插入图片描述
好友页面只放了一个ListView控件来显示在线登录的好友列表
在这里插入图片描述
3.测试在客户端调用服务器端
首先启动服务器端,如果配置正常:会显示如图的服务主机,然后复制元数据地址
在这里插入图片描述
然后在客户端的引用中,添加服务引用.在地址输入框中复制元数据地址(实际就是将此客户端和指定的服务器端建立起联系),可修改命名空间
在这里插入图片描述
添加成功后:
在这里插入图片描述服务端项目重新生成,
客户端项目,右键ChatService–>更新服务引用,获取服务端最新的元数据.
在这里插入图片描述
Debugger调试演示调用过程:
这里的代码没有任何业务逻辑,只是为了测试使用
在这里插入图片描述

 ChatServiceClient chatService = new ChatServiceClient();
 chatService.DoWork();

进入到服务器端,从而实现客户端调用服务器端
ChatServiceClient 是在客户端添加服务引用后,生成的客户端代理对象:
添加服务时输入的 命名空间名+Client,可以双击添加的服务对象,在对象浏览器对话框里,可以查看位于的命名空间。

底层的实现:

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace ChatClient.ChatService {
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ChatService.IChatService")]
    public interface IChatService {
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IChatService/DoWork", ReplyAction="http://tempuri.org/IChatService/DoWorkResponse")]
        void DoWork();
        
        [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IChatService/DoWork", ReplyAction="http://tempuri.org/IChatService/DoWorkResponse")]
        System.Threading.Tasks.Task DoWorkAsync();
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface IChatServiceChannel : ChatClient.ChatService.IChatService, System.ServiceModel.IClientChannel {
    }
    
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class ChatServiceClient : System.ServiceModel.ClientBase<ChatClient.ChatService.IChatService>, ChatClient.ChatService.IChatService {
        
        public ChatServiceClient() {
        }
        
        public ChatServiceClient(string endpointConfigurationName) : 
                base(endpointConfigurationName) {
        }
        
        public ChatServiceClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public ChatServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress) {
        }
        
        public ChatServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress) {
        }
        
        public void DoWork() {
            base.Channel.DoWork();
        }
        
        public System.Threading.Tasks.Task DoWorkAsync() {
            return base.Channel.DoWorkAsync();
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值