1.新建VS2017 控制台程序,添加项:wcf 服务
把Iservice1.cs 写成这样:
[ServiceContract]
public interface IService1
{
[OperationContract]
int DoWork();
}
在 service1.cs 的实现中返回个2:
namespace WCF
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
public class Service1 : IService1
{
public int DoWork()
{
return 2;
}
}
}
可以看到app.config 的变化:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WCF.Service1">
<endpoint address="" binding="basicHttpBinding" contract="WCF.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/WCF/Service1/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
在mian 函数中启动服务:
static void Main(string[] args)
{
new ServiceHost(typeof(Service1)).Open(); //WcfDemo.Service1 为引用的dll中的服务
Console.ReadKey();
}
2.客户端:
管理员启动那个控制台程序。
新建一个别的项目,这里建个网站。
添加服务引用:
这样既可以调用了
webconfig中多了这样的配置:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8733/Design_Time_Addresses/WCF/Service1/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
在这个web 工程下面生成一个 Connected Services\ServiceReference1文件夹,这个下面有一些文件
这其中有wsdl 文件,reference.cs。Dowork 就是这里reference.cs中定义的。
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class Service1Client : System.ServiceModel.ClientBase<WebApplication1.ServiceReference1.IService1>, WebApplication1.ServiceReference1.IService1 {
public Service1Client() {}
public Service1Client(string endpointConfigurationName) : base(endpointConfigurationName) { }
public Service1Client(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { }
public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}
public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}
public int DoWork() { return base.Channel.DoWork(); }
public System.Threading.Tasks.Task<int> DoWorkAsync() {
return base.Channel.DoWorkAsync();
}
}
可以看到最后调用的是System.ServiceModel.ClientBase<WebApplication1.ServiceReference1.IService1>的Dowork();
这个是WebApplication1.ServiceReference1.IService1的dowork();这个会发送 请求给服务器,进行下一步动作