WCF - 将HTTP转换为命名管道

时间:2014-03-20 18:29:46

标签: wcf named-pipes

大家我本周一直在关注大量博客,SO帖子,我仍然不确定如何将我的WCF服务从HTTP绑定转换为使用命名管道。 我认为有不同的方法,但我使用web.configs并在我的代码中使用服务引用。

我试过这个问题,而不是详细介绍这里的所有内容?

  

从HTTP绑定到命名管道需要采取哪些步骤?

我是否需要在(某些)博客/ SO帖子中提到的这个MEX? 我知道我需要将IIS设置为启用的协议:net.pipe ...并且IIS Express不支持此功能(这需要一个下午!)

一些相关的代码,我现在所拥有的:

IEmployeeData中的

namespace Mobile.ServiceLayer {
[ServiceContract]
public interface IEmployeeData
{ ... }

调用WCF服务:

string endpointConfigName = "BasicHttpBinding_IEmployeeData";
        EmployeeSvcRef.EmployeeDataClient edc = new EmployeeSvcRef.EmployeeDataClient(endpointConfigName);
        EmployeeSvcRef.EmployeeListResponse emp = edc.EmployeeList();

WCF服务web.config:

 <services>
  <service name="Mobile.ServiceLayer.EmployeeData">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:62734/EmployeeData" />
      </baseAddresses>
    </host>
  </service>

...

<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

客户端web.config:

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IEmployeeData" />

...

<client>
  <endpoint address="http://localhost:62734/EmployeeData.svc" binding="basicHttpBinding"
    bindingConfiguration="BasicHttpBinding_IEmployeeData" contract="EmployeeSvcRef.IEmployeeData"
    name="BasicHttpBinding_IEmployeeData" />

就像我说的那样,我已经看过SO帖子和博客,但似乎总有一块拼图错过了!

编辑:向导后的客户端web.config:

<endpoint address="net.pipe://localhost/EmployeeData.svc/" binding="netNamedPipeBinding"
    bindingConfiguration="NewBinding0" contract="IEmployeeData"
    name="" kind="" endpointConfiguration="">
    <identity>
      <certificateReference storeName="My" storeLocation="LocalMachine"
        x509FindType="FindBySubjectDistinguishedName" />
    </identity>
  </endpoint>

1 个答案:

答案 0 :(得分:3)

好的,这就是我做的。您可能会发现使用Max的评论中提到的内置工具更容易。右键单击web.config,然后选择“编辑WCF配置”。首先完成WCF服务,并设置端点,在客户端上运行此端点(右键单击它的web.config)将显示向导。

SERVER WEB.CONFIG

服务名称是接口的完全限定名称,例如Mobile.ServiceLayer.IEmployeeData

基地址变为

  

net.pipe://localhost/EmployeeData.svc

。请注意,端口号已删除,并且存在.svc(!)

创建一个端点,该合同是您的接口和netNamedPipeBinding类型的绑定。

为MEX添加第二个端点,即MetadataEXchange。

将ServiceMetaData httpGetEnabled设置为false。

<system.serviceModel>
<services>
  <service name="Mobile.ServiceLayer.IEmployeeData">
    <host>
      <baseAddresses>
        <add baseAddress="net.pipe://localhost/EmployeeData.svc" />
      </baseAddresses>
    </host>
    <!-- NetPipe -->
    <endpoint
      address=""
      binding="netNamedPipeBinding"
      contract="IEmployeeData" name="MyNetPipe" />
    <!-- Mex (Net.Tcp / Net.Pipe ) -->
    <endpoint name="EmployeeDataNetPipeMex" address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

CLIENT WEB.CONFIG

从&#39; basicHttpBinding&#39;

中删除绑定条目

添加名为NetNamedPipeBinding_IEmployeeData的条目 内部&#39;客户&#39;添加地址为

的端点
  

net.pipe://localhost/EmployeeData.svc

合同是&#39;参考名称&#39;。&#39;界面&#39;

    <bindings>
  <basicHttpBinding>
  </basicHttpBinding>
  <netNamedPipeBinding>
    <binding name="NetNamedPipeBinding_IEmployeeData" />
  </netNamedPipeBinding>
</bindings>
<client>
  <endpoint address="net.pipe://localhost/EmployeeData.svc" binding="netNamedPipeBinding"
    bindingConfiguration="NetNamedPipeBinding_IEmployeeData" contract="EmployeeSvcRef.IEmployeeData"
    name="NetNamedPipeBinding_IEmployeeData" />
    </client>