WCF如何绑定多个服务合同?

首先我会道歉,因为这可能是重复的,但我读到的所有内容似乎都不完整或令人困惑,因为我对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
}

任何帮助将非常感谢家伙谢谢:)

解决方法

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

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

你可以做的是有一个服务实现类,它实现了两个服务契约:

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

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

<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>都没有意义. values – 服务的“基本”地址是* .svc文件所在的位置(URI).

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...