app server

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

WCF 创建步骤

这个svc文件的内容为:

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

 

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

WCF 创建步骤

 

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]
interface IWork
   3: {
   4:     [OperationContract]
   5:     Student GetName();
   6: }

实体类:

class Student
   2: {
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中定义好的方法

 

object sender, EventArgs e)
   2: {
;
   4:     IWork proxy = GetService<IWork>(endpointName);
   5:     Student stu=proxy.GetName();
true);
   7: }
   8:  
string endpointName)
  10: {
new ChannelFactory<T>(endpointName);
return channel.CreateChannel();
  13: }

 

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

WCF 创建步骤

WCF 创建步骤

相关文章: