【问题标题】:WCF how to bind multiple service contracts?WCF如何绑定多个服务契约?
【发布时间】:2012-12-29 11:59:59
【问题描述】:

首先我要道歉,因为这可能是重复的,但我读到的所有内容似乎都不完整或令人困惑,因为我对 WCF 很陌生。

我基本上希望在 IIS 中部署一个 WCF 服务,可以访问 2 个端点,并且整天都在绕圈子:(

我有一个具有以下结构的服务库 WCF dll

App.config
TestSvc1.cs
ITestSvc1.cs
TestSvc2.cs
ITestSvc2.cs

这在 VS WCF 测试客户端中运行良好,现在我正在部署到 IIS,所以我创建了一个 WCF 服务应用程序并引用了 dll。该项目具有以下结构

Service1.svc
Web.config

Service1.svc 包含这个

<%@ ServiceHost Language="C#" Debug="true" 
    Service="WCFServices.TestServices.ITestSvc1" CodeBehind="Service1.svc.cs" %>

我的web.config 有以下内容

<services>
   <service name="WCFServices.TestServices.TestSvc2">
      <endpoint 
          address="" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc2">
          <identity>
            <dns value="localhost" />
          </identity>
      </endpoint>
      <endpoint 
           address="mex" 
           binding="mexHttpBinding"  
           contract="IMetadataExchange" />
      <host>
         <baseAddresses>
            <add baseAddress="http://localhost:8080/wcf2/TestServices/TestSvc2/" />
         </baseAddresses>
      </host>
   </service>
   <service name="WCFServices.TestServices.TestSvc1">
      <endpoint 
          address="" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc1"  
          listenUri="http://localhost:8080/wcf2/service1.svc">
          <identity>
            <dns value="" />
          </identity>
      </endpoint>
      <endpoint 
          address="" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc2" 
          listenUri="http://localhost:8080/wcf2/service1.svc">
          <identity>
            <dns value="" />
          </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding"  contract="IMetadataExchange" />
      <host>
         <baseAddresses>
            <add baseAddress="http://localhost:8080/wcf2/TestServices/TestSvc1/" />
         </baseAddresses>
      </host>
   </service>     
</services>

任何帮助将不胜感激。如您所见,我尝试在 web.config 中添加一个额外的端点,但这不起作用,我收到一条错误消息,指出在 TestSvc1 中找不到 TestSvc2 调用,我猜这与 @987654328 中的条目有关@

我还阅读了有关创建继承这些接口的类的信息,但我不确定如何根据上面的内容来实现它。需要修改Service1.svc吗?

public interface MultipleSvc : ITestSvc1, ITestSvc2
{
   // What exactly do I put here? I have no idea, do I leave it blank? This didn't work for me
}

任何帮助将不胜感激,谢谢:)

【问题讨论】:

  • 我看到的一件小事是您为服务 1 和 2 指定了不同的命名空间,即 WCFServices.TestServices.ITestSvc1 和 WCFServices.TestService.ITestSvc2。注意 TestServices.ITestSvc2 中缺少的 s。
  • 是的,抱歉,这只是一个错字,只是为了更好地说明问题,请编辑:)

标签: wcf c#-4.0 wcf-binding


【解决方案1】:

黄金法则:一个.svc = 一个服务(或者更具体地说:一个服务实现类)

因此,如果您确实有两个独立的不同服务(在 WCFServices.TestServices.TestSvc1WCFServices.TestServices.TestSvc2 类中),那么您需要两个 .svc 文件(每个服务一个,每个服务一个)

可以做的是拥有一个服务实现类来实现两种服务契约:

public class MultipleServices : ITestSvc1, ITestSvc2
{
   // implement the service logic here, for both contracts
}

在这种情况下,一个 svc 文件就足够了(.svc 文件是每个实现类,并且可以托管多个服务合同)。但是你也需要改变你的配置——因为你真的只有一个服务类(因此:一个&lt;service&gt;标签)和其中托管的多个合同:

<services>
   <service name="WCFServices.TestServices.MultipleServices">
      <endpoint 
          address="Service1" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc1">
          <identity>
            <dns value="" />
          </identity>
      </endpoint>
      <endpoint 
          address="Service2" 
          binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" 
          contract="WCFServices.TestServices.ITestSvc2">
          <identity>
            <dns value="localhost" />
          </identity>
      </endpoint>
      <endpoint 
           address="mex" 
           binding="mexHttpBinding"  
           contract="IMetadataExchange" />
   </service>     
</services>

另外:由于您似乎在 IIS 中托管 WCF 服务,因此定义任何 &lt;baseAddress&gt; 值是没有意义的 - 您的服务的“基”地址是 *.svc 文件所在的位置 (URI)。

【讨论】:

  • 为简单起见,我将使用此处建议的 2 个 svc 文件。
  • 谢谢大家,尝试了 singl svc 并让它工作了,但是由于某种原因,它在部署时放弃了 wshttpbinding 并通过 basichttp 实现 - 奇怪的一个!我会用两个svc文件测试,看看结果是否一样
  • 是的,当它的两个单独文件绑定仍然是 wshttp 时,有什么想法为什么在它们组合时它会丢失?
  • +1 另外:由于您似乎在 IIS 中托管 WCF 服务,因此定义任何 值没有意义 - 您的服务的“基”地址是位置( URI) *.svc 文件所在的位置。.
  • @marc_s 我在这里问了同样的问题stackoverflow.com/questions/44155744/…,但有点不同。你能看看吗?
【解决方案2】:

你不需要两个 svc 文件。选择 ServiceReference1.Service1Client 或 ServiceReference1.Service2Client。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2012-01-11
    • 1970-01-01
    • 2011-02-08
    • 2019-07-12
    • 1970-01-01
    • 2013-06-16
    相关资源
    最近更新 更多