1 新建一个控制台程序
2 新建一个IBLL库,新建一个接口,注意引入System.ServiceModel,对接口标记ServiceContract,并将方法标记为OperatinContract
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace IBLL
{
[ServiceContract]
public interface IUserInfoService
{
[OperationContract]
int Add(int a, int b);
}
}
3 新建一个BLL库,新建一个类引用上面的接口
using IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
public class UserInfoService : IUserInfoService
{
public int Add(int a, int b)
{
return a + b;
}
}
}
4 修改app.config文件,配置服务
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="behaviorConfiguration">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
5 修改Program.cs,引入System.ServiceModel,此时表示WCF寄宿于该控制台程序中,
using BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace WcfDemo
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost host = new ServiceHost(typeof(UserInfoService)))
{
host.Open();
Console.WriteLine("服务已经启动");
Console.ReadLine();
}
}
}
}
6.在bin目录中,以管理员身份运行服务端程序,启动服务
以下分两种建立客户端程序,一种是直接引用服务,类似web service,另外一种是使用代理类
方法1 直接服务引用
1).新建一控制台程序,添加服务引用
2)输入服务端app.config里面的baseaddress配置
3)修改Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WcfClient2
{
class Program
{
static void Main(string[] args)
{
ServiceReference2.UserInfoServiceClient client = new ServiceReference2.UserInfoServiceClient();
int result = client.Add(2, 3);
Console.WriteLine(result.ToString());
Console.ReadLine();
}
}
}
方法2 使用代理类:
1.在项目中再建一个客户端控制台程序,在该客户端项目,右键,在文件资源管理器打开文件夹
2 拷贝上面的目录
3 在开始 ,VS里面打开开发者命令提示
4 切换到client目录下面
5 输入svcutil http://localhost:8000/?wsdl /o:UserInfoServiceClient.cs,则会在该目录生成这个类
6 在解决方案管理器中,点击显示所有文件,将output.config和UserInfoServiceClient.cs包含到项目中
7 删除项目本身带的App.config,将output.config改名为App.config,并将client的终结点配置中的契约改为IBLL.IUserInfoService ,
因为我们已经有了这个接口,不用代理里面的这个接口
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUserInfoService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUserInfoService" contract="IBLL.IUserInfoService"
name="BasicHttpBinding_IUserInfoService" />
</client>
</system.serviceModel>
</configuration>
8 屏蔽或者删除UserInfoServiceClient里的IUserInfoService接口的代码和最后一个异步代码
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:4.0.30319.42000
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
//[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
//[System.ServiceModel.ServiceContractAttribute(ConfigurationName="IUserInfoService")]
//public interface IUserInfoService
//{
// [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IUserInfoService/Add", ReplyAction="http://tempuri.org/IUserInfoService/AddResponse")]
// int Add(int a, int b);
// [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IUserInfoService/Add", ReplyAction="http://tempuri.org/IUserInfoService/AddResponse")]
// System.Threading.Tasks.Task<int> AddAsync(int a, int b);
//}
using IBLL;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IUserInfoServiceChannel : IUserInfoService, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class UserInfoServiceClient : System.ServiceModel.ClientBase<IUserInfoService>, IUserInfoService
{
public UserInfoServiceClient()
{
}
public UserInfoServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public UserInfoServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public UserInfoServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public UserInfoServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public int Add(int a, int b)
{
return base.Channel.Add(a, b);
}
//public System.Threading.Tasks.Task<int> AddAsync(int a, int b)
//{
// return base.Channel.AddAsync(a, b);
//}
}
9 修改客户端的主程序,调用代理类,实现wcf调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WcfClient
{
class Program
{
static void Main(string[] args)
{
UserInfoServiceClient client = new UserInfoServiceClient();
int result = client.Add(2, 3);
Console.WriteLine(result.ToString());
Console.ReadLine();
}
}
}