首页 > 其他 > 详细

WCF 创建步骤

时间:2014-01-27 00:22:20      阅读:487      评论:0      收藏:0      [点我收藏+]

app server

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

bubuko.com,布布扣

这个svc文件的内容为:

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

 

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

bubuko.com,布布扣

 

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.通过添加服务引用来自动生成

bubuko.com,布布扣

bubuko.com,布布扣

WCF 创建步骤

原文:http://www.cnblogs.com/lavenvsxiaoye/p/3534480.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!