WCF如何绑定多个服务合同? [英] WCF how to bind multiple service contracts?

查看:229
本文介绍了WCF如何绑定多个服务合同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我会道歉,因为这可能是一个重复,但我读到的一切似乎是不完整或混乱,因为我是新的WCF。

First off I'll apologise as this is probably a duplicate but everything I read seems to be either incomplete or confusing as I'm very new to WCF.

我基本上希望在IIS中部署一个WCF服务,有两个端点可访问,并且一直围绕着整个圈子:(

I basically am looking to deploy a WCF service in IIS with 2 endpoints accessible and have been going around in circles all day :(

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

I have a service library WCF dll with the following structure

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

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

This works fine in the VS WCF test client and now im deploying to IIS so I created a WCF Service Application and referenced the dll. This project has the following structure

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 中添加一个额外的端点,但这不工作,我得到一个错误,说TestSvc2调用不能发现在TestSvc1,我猜这与 Service1.svc

Any help would be greatly appreciated. As you can see I've tried to add an additional endpoint in the web.config but this doesn't work, I get an error saying that TestSvc2 calls can't be found in TestSvc1 which I guess relates to the entry in Service1.svc

的条目有关我还读了关于创建一个类继承这些接口,但我不知道如何实现它基于我上面的内容。我需要修改 Service1.svc

I also read about creating a class that inherits these interfaces but I am not sure exactly how to implement it based on what I have above. Do I need to modify the 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
}

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

Any help would be greatly appreciated guys thanks :)

推荐答案

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

Golden rule: one .svc = one service (or more specifically: one service implementation class)

所以如果你有两个单独的, WCFServices.TestServices.TestSvc1 WCFServices.TestServices.TestSvc2 ),那么您需要两个 .svc 文件(每个服务各一个)

So if you do have two separate, distinct services (in classes WCFServices.TestServices.TestSvc1 and WCFServices.TestServices.TestSvc2), then you need two .svc files (one each, for each service)

是具有实现这两个服务合同的一个服务实现类

What you could do is have one service implementation class that implements both service contracts:

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


$ b b

在这种情况下,一个svc文件就足够了( .svc 文件并可以托管多个服务合同)。但是,您还需要更改您的配置,因为您只有一个服务类(因此:一个< service> )和其中托管的多个合同:

In that case, one svc file will be enough (the .svc file is per implementation class and can host multiple service contracts). But then you'd need to change your config, too - since you really only have one service class (therefore: one <service> tag) and multiple contracts hosted inside it:

<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服务, code>< baseAddress> values - 您的服务的基本地址是 *。svc 文件生命。

Also: since you seem to be hosting your WCF service in IIS, there's no point in defining any <baseAddress> values - the "base" address of your service is the location (URI) where the *.svc file lives.

这篇关于WCF如何绑定多个服务合同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆