快速访问WCF服务--ServiceModel 元数据实用工具 (Svcutil.exe)

基本定义

  ServiceModel 元数据实用工具用于依据元数据文档生成服务模型代码,以及依据服务模型代码生成元数据文档。 

SvcUtil.exe

ServiceModel 元数据实用工具可在 Windows SDK 安装位置中找到,具体位置为 C:Program FilesMicrosoft SDKsWindowsv6.0Bin

在这里你可以有多个选择。

  

  这里对应的不同的系统版本以及内部的.net版本,可以调用针对不同的.net framework 版本的SvcUtil.exe.

根据服务元数据生成 WCF 客户端

  进入SvcUtil.exe所存放的路径:

  执行>svcutil.exe http://172.168.0.112:8081/test.svc /language:cs /out:ssss.cs  (SvcUtil.exe wcf路径 )

  这里要注意的是SvcUtil.exe后面是服务的地址,会在工具所在的目录下生成代理类文件、web.config文件;

  把这个代理类拷贝到客户端程序就可以对WCF进行调用了。

  程序中调用:
            IServiceTest ist = new ServiceTestClient();//这里可以使用不同的构造方法,以方便WCF入口点
            string retValue=ist.TestParas1("测试");

  但是要增加一下wcf入口点

    1.直接使用构造方法(找到合适入口点构造方法)

          public ServiceTestClient(string endpointConfigurationName) : base(endpointConfigurationName)
          {    }
   
          public ServiceTestClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress)
          {    }
   
          public ServiceTestClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress)
          {    }
   
          public ServiceTestClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress)
          {    }

    2.配置web.config

      根据生成的配置文件修改<basicHttpBinding>、<endpoint>节点。

元数据模型还有很多应用,详细见:http://msdn.microsoft.com/zh-cn/library/aa347733(v=vs.100).aspx

 

原文地址:https://www.cnblogs.com/bjlhx/p/3519503.html