WcF生成客户端遇到的问题/

来源:互联网 发布:kindle阅读器 mac 编辑:程序博客网 时间:2024/04/29 06:57

 

WcF生成客户端遇到的问题/

我建立的WCF服务器端程序,用svcutil命令一直就生成不了客户

提示错误:

 元数据包含无法解析的引用:"net.tcp://127.0.0.1:6987/do"

 没有终结点在侦听可以接受消息的net.tcp://127.0.0.1:6987/do

解决法办1:

在程序中添加如下代码

            if (host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceMetadataBehavior>() == null)
            {
                BindingElement elemnt = new TcpTransportBindingElement();
                CustomBinding bind = new CustomBinding(elemnt);
                host.Description.Behaviors.Add(new System.ServiceModel.Description.ServiceMetadataBehavior());
                host.AddServiceEndpoint(typeof(System.ServiceModel.Description.IMetadataExchange), bind, "Mex");
            }

解决办法2

 服务端配置文件做如何更改如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.serviceModel>
  <services>

   <service name="ConsoleApplication1.Do"  behaviorConfiguration="IDoBehavior"> 
    <endpoint address="duplex" binding="netTcpBinding" contract="ConsoleApplication1.IDo">

     <identity>
      <dns value="localhost"/>
     </identity>

    </endpoint>


        <!-- Metadata Endpoints -->
        <!-- 元数据交换终结点由服务用于向客户端做自我描述。-->
        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
    <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange"/>//要想使用svcutil生成客户端文件必须包括此句代码

   </service>
  </services>

 //要想使用svcutil生成客户端文件必须包括以下代码

  <behaviors>
   <serviceBehaviors>
    <behavior name="IDoBehavior">
     <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false 并删除上面的元数据终结点  -->

    <!-- 指定service元数据发布和相关信息
                     属性说明:
                     httpGetEnabled - bool类型的值,表示是否允许通过HTTP的get方法获取sevice的WSDL元数据
                     httpGetUrl - 如果httpGetEnabled为true,这个属性指示使用哪个URL地址发布服务的WSDL,
                                  如果这个属性没有设置,则使用服务的HTTP类型的baseAddress后面加上?WSDL
--> 
     <serviceMetadata httpGetEnabled="True" httpGetUrl="http://127.0.0.1:64987/Do"/>
     <!-- 要接收故障异常详细信息以进行调试,
          请将下值设置为 true。在部署前
            设置为 false 以避免泄漏异常信息-->
     <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
   </serviceBehaviors>
  </behaviors>

 </system.serviceModel>
</configuration>

 

  

   此时svcutil 后面写的地址应该是:serviceMetadata 配置节中设置的httpGetUrl中的地址

原创粉丝点击