zoukankan      html  css  js  c++  java
  • WCF 创建步骤

    app server

    1. 建立宿主项目,添加一个svc文件

    image

    这个svc文件的内容为:

    <%@ ServiceHost Language="C#" Debug="true" Service="bc.WorkBc"%>

    bc.workBc 中的bc表示项目名称,workBc表示类名,如下图:

    image

    2.配置wcf,最简单的模式

    <system.serviceModel>
        <services>
            <service name="contract.IWork">
                <endpoint address="http://localhost:9789/TestService.svc" binding="basicHttpBinding"
                    bindingConfiguration="" contract="contract.IWork" />
            </service>
        </services>
    </system.serviceModel>

    3.实现bc中workBc类的方法

       1:  [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
       2:  class WorkBc : IWork
       3:  {
       4:      public Student GetName()
       5:      {
       6:          return new Student { Name = "qqq" };
       7:      }
       8:  }

    接口类:

       1: [ServiceContract]
       2: public interface IWork
       3: {
       4:     [OperationContract]
       5:     Student GetName();
       6: }

    实体类:

       1: public class Student
       2: {
       3:     public string Name { get; set; }
       4: }

    web server

    方法1 手动生成

    1.配置文件

    <system.serviceModel>
           <client>
               <endpoint address="http://localhost:9789/TestService.svc" binding="basicHttpBinding"
                   bindingConfiguration="" contract="contract.IWork" name="contract.IWork"
                   kind="" endpointConfiguration="" />
           </client>
       </system.serviceModel>

    2.调用app server中定义好的方法

       1: protected void Page_Load(object sender, EventArgs e)
       2: {
       3:     string endpointName = "contract.IWork";
       4:     IWork proxy = GetService<IWork>(endpointName);
       5:     Student stu=proxy.GetName();
       6:     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "showName", "alert('" + stu.Name + "')", true);
       7: }
       8:  
       9: private T GetService<T>(string endpointName)
      10: {
      11:     ChannelFactory<T> channel = new ChannelFactory<T>(endpointName);
      12:     return channel.CreateChannel();
      13: }

    2.通过添加服务引用来自动生成

    image

    image

  • 相关阅读:
    flink-cdc读取postgres报异常,没有发布表
    yum 安装高版本Git
    分布式存储FastDFS搭建
    ElasticSearch6.5.1集群部署
    CentOS7 OpenSSH编译安装升级
    K8S使用ceph实现持久化存储
    ceph分布式集群的搭建
    canal服务搭建
    MySQL-5.7.31的搭建
    基于CentOS7.6使用KubeOperator安装Kubernetes集群
  • 原文地址:https://www.cnblogs.com/lavenvsxiaoye/p/3534480.html
Copyright © 2011-2022 走看看