WCF如何绑定多个服务契约?

7

首先我要道歉,因为这可能是重复的内容,但是我读到的所有东西似乎都不完整或令人困惑,因为我对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调用,我想这与Service1.svc中的条目有关。
我还阅读了有关创建继承这些接口的类的内容,但我不确定如何根据上述内容来实现它。我需要修改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。 - GTG
是的,抱歉那只是一个打字错误,这里只是为了更好地说明问题,我会进行编辑的 :) - JazziJeff
2个回答

11

黄金法则:一个 .svc 文件对应一个服务 (或更具体地说,一个服务实现类)

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

可以拥有一个实现了两个服务契约的服务实现类

public class MultipleServices : ITestSvc1, ITestSvc2
{
   // implement the service logic here, for both contracts
}
在这种情况下,一个svc文件就足够了(.svc文件是每个实现类的,可以托管多个服务契约)。但是你也需要更改配置 - 因为你只有一个服务类(因此:一个标签)和多个托管在其中的契约。
<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服务,因此定义任何<baseAddress>值都没有意义 - 您的服务的“基础”地址是*.svc文件所在的位置(URI)。


我会选择这里建议的两个svc文件,以保持简单。 - Matt Roberts
谢谢大家,我尝试了单个svc并使其工作,但在部署时它却不知何故丢失了wshttpbinding,而是以basichttp形式出现 - 这很奇怪!我将使用两个svc文件进行测试,看看结果是否相同。 - JazziJeff
当它们是两个单独的文件时,绑定保持为wshttp。有任何想法为什么当它们合并时会丢失? - JazziJeff
另外,由于您似乎是在IIS中托管WCF服务,因此没有必要定义任何<baseAddress>值 - 您的服务的“基本”地址是*.svc文件所在的位置(URI)。 - Jim Aho
@marc_s 我在这里问了同样的问题 https://stackoverflow.com/questions/44155744/multi-service-contract-in-one-endpoint-c-sharp-wcf 但是有一点不同。你能看一下吗? - Ehsan Akbar

0

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接