app server
1. 建立宿主项目,添加一个svc文件
这个svc文件的内容为:
<%@ ServiceHost Language="C#" Debug="true" Service="bc.WorkBc"%>
bc.workBc 中的bc表示项目名称,workBc表示类名,如下图:
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.通过添加服务引用来自动生成