• 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

  • 相关阅读:
    主席树学习记录
    P1072 Hanson 的趣味题 题解
    好文章收集
    计算几何专题
    小问题
    CSP-S2020题解
    上下界网络流
    想到的无法解决的点子
    省选联考2020组合数问题
    省选数学复习
  • 原文地址:https://www.cnblogs.com/lavenvsxiaoye/p/3534480.html
Copyright © 2020-2023  润新知